Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variables not defined
#1
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
Reply
#2
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 :)
Reply
#3
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 -_-
Reply
#4
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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  User-defined function to reset variables? Mark17 3 1,590 May-25-2022, 07:22 PM
Last Post: Gribouillis
  python library not defined in user defined function johnEmScott 2 3,773 May-30-2020, 04:14 AM
Last Post: DT2000
  help (variables) - NameError: name 'Payment' is not defined? Vqlk 5 3,798 Feb-27-2019, 04:57 PM
Last Post: buran
  MySQLdb, problem with query with user-defined variables buran 6 6,319 Feb-03-2017, 06:16 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020