467,926 Members | 1,473 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

How to correct the NameError: name 'xx' is not defined?






May I know how to correct the NameError: name 'xx' is not defined ?







Expand|Select|Wrap|Line Numbers
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from sklearn import svm, datasets
  4. # import some data to play with
  5. iris = datasets.load_iris()
  6. X = iris.data[:,[2,3]]
  7. y = iris.target
  8.  
  9. def plotSVC(title):
  10. # create a mesh to plot in
  11.     x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
  12.     y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
  13.     h = (x_max / x_min)/100
  14.     xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
  15.              np.arange(y_min, y_max, h))
  16.     plt.subplot(1, 1, 1)
  17.     Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
  18.     Z = Z.reshape(xx.shape)
  19.  
  20. cs = [0.1, 1, 10, 100]
  21. for c in cs:
  22.     svc = svm.SVC(kernel='rbf', C=c).fit(X, y)
  23.     plotSVC('C=' + str(c))
  24. plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
  25. plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired)
  26. plt.xlabel('petal length')
  27. plt.ylabel('petal width')
  28. plt.xlim(xx.min(), xx.max())
  29. plt.show()
  30.  



The error message is as below - NameError: name 'xx' is not defined



Expand|Select|Wrap|Line Numbers
  1.  
  2. runfile('C:/Users/HSIPL/Desktop/New f/a i/Homework 5 6 Solution draft.py', wdir='C:/Users/HSIPL/Desktop/New f/a i')
  3. C:/Users/HSIPL/Desktop/New f/a i/Homework 5 6 Solution draft.py:13: RuntimeWarning: divide by zero encountered in double_scalars
  4.   h = (x_max / x_min)/100
  5. C:\Users\HSIPL\Anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py:107: MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
  6.   warnings.warn(message, mplDeprecation, stacklevel=1)
  7. Traceback (most recent call last):
  8.  
  9.   File "<ipython-input-2-9e53960ad79d>", line 1, in <module>
  10.     runfile('C:/Users/HSIPL/Desktop/New f/a i/Homework 5 6 Solution draft.py', wdir='C:/Users/HSIPL/Desktop/New f/a i')
  11.  
  12.   File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
  13.     execfile(filename, namespace)
  14.  
  15.   File "C:\Users\HSIPL\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
  16.     exec(compile(f.read(), filename, 'exec'), namespace)
  17.  
  18.   File "C:/Users/HSIPL/Desktop/New f/a i/Homework 5 6 Solution draft.py", line 24, in <module>
  19.     plt.contourf(xx, yy, Z, cmap=plt.cm.Paired, alpha=0.8)
  20.  
  21. NameError: name 'xx' is not defined
  22.  
  23.  






Please see the attached files -




Please help me so that I can improve my computing skills








Attached Images
File Type: jpg SVM.jpg (59.1 KB, 122 views)
File Type: jpg Kernel SVM.jpg (48.4 KB, 119 views)
Attached Files
File Type: pdf SVM.pdf (178.4 KB, 48 views)
File Type: pdf Kernel SVM.pdf (938.6 KB, 61 views)
Apr 2 '19 #1
  • viewed: 2940
Share:
2 Replies
Expert 512MB
We have to be able to locate the line the error is on among other things. Post the complete error message with trackback.
Please see the attached files
No one is going to open an unknown file on a public website.
Apr 2 '19 #2
Bit
In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn't know what it's for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as "True". Before you use the global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it.
2 Weeks Ago #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by NetKev | last post: by
2 posts views Thread by jolly | last post: by
3 posts views Thread by willkab6 | last post: by
3 posts views Thread by Mathieu Prevot | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.