PYTHON数据挖掘入门与实践3-k近邻算法

[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])

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值