Python Forum

Full Version: Variables not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone, I am copying the below code out of a text book and am getting an error.

y_train_pred = cross_val_predict(sgd_clf, X_train_scaled, y_train, cv=3)
conf_mx = confusion_matrix(y_train, y_train_pred)
conf_mx
plt.matshow(conf_mx, cmap=plt.cm.gray)
plt.show()

row_sums = conf_mx.sum(axis=1, keepdims=True)
norm_conf_mx = conf_mx / row_sums
np.fill_diagonal(norm_conf_mx, 0)
plt.matshow(norm_conf_mx, cmap=plt.cm.gray)
plt.show()

cl_a, cl_b = 3, 5
X_aa = X_train[(y_train == cl_a) & (y_train_pred == cl_a)]
X_ab = X_train[(y_train == cl_a) & (y_train_pred == cl_b)]
X_ba = X_train[(y_train == cl_b) & (y_train_pred == cl_a)]
X_bb = X_train[(y_train == cl_b) & (y_train_pred == cl_b)]
plt.figure(figsize=(8,8))
plt.subplot(221); plot_digits(X_aa[:25], images_per_row=5)
plt.subplot(222); plot_digits(X_ab[:25], images_per_row=5)
plt.subplot(223); plot_digits(X_ba[:25], images_per_row=5)
plt.subplot(224); plot_digits(X_bb[:25], images_per_row=5)
plt.show()
my error is:
plt.subplot(221); plot_digits(X_aa[:25], images_per_row=5)

NameError: name 'plot_digits' is not defined

Can anyone help me solve this error?

Thanks
This can not be the whole code since the imports are missing. The main problem is, that you have not defined the function plot_digits. There are two possibilities now. The first one would be that somewhere in the book they introduced this function so that you have to find that part and use it :)
The second would be that it is a function of some module like skilearn or so and you missed that import.
In both ways your compiler is missing this function and since the name of the function is not defined it throws that error :)
You are right it was defined else where in the code. Not in the book tho on the online code version. The book seems to have a lot of gaps in it -_-
Yeah, unfortunately that is often the case. I like books where the complete code (maybe divided by chapters) can be downloaded and used more easily :)