[i :j: s ]的描述
Python enumerate() 函数
scikit-learn K最近邻分类器 KNeighborsClassifier 使用
scikit-learn K近邻法类库使用小结
cross_val_score
cross_val-score中的参数
score 中可选择的计算指标
with open(data_filename, 'r') as input_file:
reader = csv.reader(input_file)
# pprint(reader)
for (i, row) in enumerate(reader)
# from sklearn.cross_validation import train_test_split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=14)
print("There are {} samples in the training dataset".format(X_train.shape[0]))
print("There are {} samples in the testing dataset".format(X_test.shape[0]))
print("Each sample has {} features".format(X_train.shape[1]))
from sklearn.neighbors import KNeighborsClassifier
# 导入k-近邻分类器类,为其初始化一个实例
estimator = KNeighborsClassifier()
estimator.fit(X_train, y_train)
y_predicted = estimator.predict(X_test)
accuracy = np.mean(y_test == y_predicted) * 100
print("The accuracy is {0:.2f}%".format(accuracy))
from sklearn.model_selection import cross_val_score
scores = cross_val_score(estimator, X, y, scoring='accuracy')
average_accuracy = np.mean(scores) * 100
print("The average accuracy is {0:.1f}%".format(average_accuracy))
avg_scores = []
all_scores = []
parameter_values = list(range(1, 21)) # Including 20
for n_neighbors in parameter_values:
estimator = KNeighborsClassifier(n_neighbors=n_neighbors)
scores = cross_val_score(estimator, X, y, scoring='accuracy',cv = 3)
print(scores)
# break
avg_scores.append(np.mean(scores))
all_scores.append(scores)
from matplotlib import pyplot as plt
plt.figure(figsize=(32,20))# 画布的大小
print(len(parameter_values), len(avg_scores),len(all_scores))
plt.plot(parameter_values, avg_scores, '-o', linewidth=5, markersize=24)
plt.plot(parameter_values, all_scores, '-v', linewidth=5, markersize=24)
#plt.axis([0, max(parameter_values), 0, 1.0])