我目前正在“Python for Machine Learning”一书中的第3章。虽然,我正在实现本书中的算法,但我得到了一个NameError。我不知道为什么我得到这个NameError,'NameError:name'X_combined_std'未定义',当我在第54行明确定义了X_combined_std时。有人能告诉我为什么我得到一个NameError并帮我修复它?
错误:
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]
代码:
from sklearn import datasets
import numpy as np
iris = datasets.load_iris()
X = iris.data[:, [2,3]]
y = iris.target
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 0)
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)
from sklearn.linear_model import Perceptron
ppn = Perceptron(n_iter = 40, eta0=0.1, random_state = 0)
ppn.fit(X_train_std, y_train)
y_pred = ppn.predict(X_test_std)
print('Misclassified samples: %d' % (y_test != y_pred).sum())
from sklearn.metrics import accuracy_score
print('Accuracy: %.2f' % accuracy_score(y_test, y_pred))
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
def plot_decision_regions(X, y, classifier, test_idx=None, resolution = 0.02):
#setup marker generator and color map
markers = ('s', 'x', 'o', '^', 'v')
colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
cmap = ListedColormap(colors[:len(np.unique(y))])
#plot the decision surface
x1_min, x1_max = X[:, 0].min() -1, X[:,0].max() + 1
x2_min, x2_max = X[:,1].min() - 1, X[:,1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution), np.arange(x2_min, x2_max,resolution))
Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
Z = Z.reshape(xx1.shape)
plt.contourf(xx1, xx2, Z, alpha = 0.4, cmap = cmap)
plt.xlim(xx1.min(), xx1.max())
#plot all samples
for idx, cl in enumerate(np.unique(y)):
plt.scatter(x=X[y==cl,0], y = X[y == cl, l], alpha = 0.8, c=cmap(idx), marker = markers[idx], label = cl)
#highlight test samples
if test_idx:
X_test,y_test = X[test_idx,:], y[test_idx]
plt.scatter(x_test[:,0], X_test[:, 1], c='', alpha = 1.0, linewidths = 1, marker = 'o', s = 55, label = 'test set')
X_combined_std = np.vstack((X_train_std, X_test_std))
y_combined = np.hstack((y_train, y_test))
plot_decision_regions(x=X_combined_std, y = combined, classifier = ppn, test_idx = range(105,150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized]')
plt.legend(loc = 'upper left')
plt.show()
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(C=1000.0, random_state = 0)
lr.fit(X_train_std, y_train)
plot_decision_regions(X_combined_std, y_combined, classifier = lr, test_idx = range(105,150))
plt.xlabel('petal length [standardized]')
plt.ylabel('petal width [standardized')
plt.legend(loc = 'upper left')
plt.show()
weights, params = [], []
for c in np.arange(-5,5):
lr = LogisticRegression(C=10**c, random_state = 0)
lr.fit(X_train_std, y_train)
weights.append(lr.coef_[1])
params.append(10**c)
weights = np.array(weights)
plt.plot(params, weights[:,0], label = 'petal length')
plt.plot(params, weights[:,1], linestyle = '--', label = 'petal width')
plt.ylabel('weight coefficient')
plt.xlabel('C')
plt.legend(loc = 'upper left')
plt.xscale('log')
plt.show()
答案 0 :(得分:0)
在运行plot_decision_region之前运行此代码。它应该工作。
var isEven = function(n) {
if (n < 0){
n = Math.abs(n);
}
if(n === 1){
return false;
} else if (n === 0){
return true;
} else {
return isEven(n-2); // here is my question - can you explain why does this need the 'return' in there?
}
};
答案 1 :(得分:0)
您没有对X-train
数据集进行标准化,请先执行此操作。
sc = StandardScaler()
sc.fit(X_train)
X_train_std = sc.transform(X_train)
X_test_std = sc.transform(X_test)
答案 2 :(得分:-1)
这是因为您尚未定义X_combined_std
或y_combined
。如果你已经在其他地方实现了它,请确保在执行此代码之前运行该部分代码。