ionosphere.data下载地址:http://archive.ics.uci.edu/ml/machine-learning-databases/ionosphere/
import numpy as np
import csv
data_filename="D:\\python27\\study\\code\\Chapter2\\ionosphere.data"
#初始化接受数据的数组
X = np.zeros( (351, 34),dtype='float')
y = np.zeros((351,),dtype='bool')
#读取文件信息
with open(data_filename,'r') as data:
reader = csv.reader(data)
for i, row in enumerate(reader): # 通过枚举函数获得每行的索引号
X[i] = [ float(datum) for datum in row[:-1] ] # 获取每一个个体的前34个值
y[i] = row[-1] == 'g' #把g转换为0,1
from sklearn.cross_validation import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=14)
from sklearn.neighbors import KNeighborsClassifier # 导入K近邻分类器,并初始化一个实例
estimator = KNeighborsClassifier()
estimator.fit(X_train,y_train)
y_preditcted = estimator.predict(X_test)
accuracy = np.mean(y_test == y_preditcted) * 100
print("准确率:",accuracy)
from sklearn.cross_validation import cross_val_score # 交叉检验
scores = cross_val_score(estimator,X,y,scoring='accuracy')
avg_accuracy = np.mean(scores) * 100
print("平均准确率:",avg_accuracy)
# 设置参数,增强算法的泛化能力,调整近邻数量
avg_scores = []
all_scores = []
num_size = list(range(1,21)) # 包括20
for n_neighbors in num_size:
esimator = KNeighborsClassifier(n_neighbors=n_neighbors)
scores = cross_val_score(esimator,X,y,scoring='accuracy')
avg_scores.append(np.mean(scores))
all_scores.append(scores)
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(num_size,avg_scores,'-o',linewidth=5, markersize=12)
#随着近邻的增加 准确率不断的下降