如何更正NameError:名称"xx"未定义?

我可以知道如何更正NameError:没有定义名称'xx'吗?

选择 | 换行 | 行号
  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.  

错误消息如下-NameError:未定义名称'xx'

选择 | 换行 | 行号
  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.  

请参阅所附文件-
请帮帮我,这样我才能提高我的计算能力
附加图像
File Type: jpg
SVM.jpg
(59.1KB,236次浏览)
File Type: jpg
内核SVM.jpg
(48.4KB,188次浏览)
附加的文件
File Type: pdf
SVM.pdf
(178.4 KB,89次浏览)
File Type: pdf
内核SVM.pdf
(938.6 KB,92次浏览)

# 回答1


我们必须能够定位错误所在的行,以及其他事情。使用trackback发布完整的错误消息。
没有人会在公共网站上打开一个未知的文件。
# 回答2


在大多数情况下,当Python看到变量名(Global或Local)但不知道它的用途时,就会触发此错误。如果忘记初始化变量、错误拼写变量或错误拼写保留字(如"True"),则可能会发生这些错误。在使用函数中的全局变量进行读取之前,必须首先在某个地方对其进行初始化:在函数外部或在函数内部。

标签: python

添加新评论