Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在这个python代码中会出现名称错误?_Python_Machine Learning_Nameerror - Fatal编程技术网

为什么在这个python代码中会出现名称错误?

为什么在这个python代码中会出现名称错误?,python,machine-learning,nameerror,Python,Machine Learning,Nameerror,我目前正在读《机器学习Python》一书的第3章。当我在实现书中的算法时,我遇到了一个命名错误。当我在第54行清楚地定义了X_组合_标准时,我不确定为什么会出现这样的名称错误“NameError:name‘X_组合_标准’未定义”。有人能告诉我为什么我会有一个名字错误,并帮助我修复它吗 错误: C:\Python27\lib\site-packages\sklearn\cross_validation.py:44: DeprecationWarning: This module was depr

我目前正在读《机器学习Python》一书的第3章。当我在实现书中的算法时,我遇到了一个命名错误。当我在第54行清楚地定义了X_组合_标准时,我不确定为什么会出现这样的名称错误“NameError:name‘X_组合_标准’未定义”。有人能告诉我为什么我会有一个名字错误,并帮助我修复它吗

错误:

C:\Python27\lib\site-packages\sklearn\cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
  "This module will be removed in 0.20.", DeprecationWarning)
Misclassified samples: 4
Accuracy: 0.91
Traceback (most recent call last):
  File "C:\Users\qasim\Documents\python_machine_learning\scilearn.py", line 65, in <module>
    plot_decision_regions(X_combined_std, y_combined, classifier = lr, test_idx = range(105,150))
NameError: name 'X_combined_std' is not defined
[Finished in 2.2s with exit code 1]

这是因为您没有定义
X\u combined\u std
y\u combined
。如果您在其他地方实现了它,请确保在执行该代码之前运行该部分代码。

在运行绘图决策区域之前运行该代码。它应该会起作用

from sklearn.preprocessing import StandardScaler

sc = StandardScaler()    
sc.fit(X_train)    

X_train_std = sc.transform(X_train)    
X_test_std = sc.transform(X_test)    
X_combined_std = np.vstack((X_train_std, X_test_std))    
y_combined = np.hstack((y_train, y_test))

您没有标准化
X-train
数据集,请先这样做

sc = StandardScaler()

sc.fit(X_train)

X_train_std = sc.transform(X_train)

X_test_std = sc.transform(X_test)

您没有定义
X\u combined\u std
y\u combined
。对不起,我没有在这里说明我的完整代码。见鬼,您也没有在您向我们展示的内容中定义
X\u train\u std
y\u train
。我们需要一个密码,你缺少密码。为什么在
X\u train\u std
上没有出现错误?如果代码不具有代表性,我们如何理解错误?我们看不到行号。请回答你的问题。。。这肯定不能解决问题。请参阅问题下方的评论。我已解决了该问题。现在我得到了这个错误。。。回溯(最后一次调用):文件“C:\Users\qasim\Documents\python\u machine\u learning\scilearn.py”,第56行,在绘图决策区域(X=X\u combined\u std,y=y\u combined,classifier=ppn,test\u idx=range(105150))文件“C:\Users\qasim\Documents\python\u machine\u learning\scilearn.py”,第47行,在绘图决策区域plt.scatter(X=X=X)中[y==cl,0],y=X[y==cl,1],alpha=0.8,c=cmap(idx),marker=markers[idx],label=cl)类型错误:scatter()至少接受2个参数(给定4个)[在2.6s内完成]。有人能帮忙吗?你是说“标准化”吗?
sc = StandardScaler()

sc.fit(X_train)

X_train_std = sc.transform(X_train)

X_test_std = sc.transform(X_test)