python数据挖掘入门与实践--------电离层(Ionosphere), scikit-learn估计器,K近邻分类器,交叉检验,设置参数

ionosphere.data下载地址:http://archive.ics.uci.edu/ml/machine-learning-databases/ionosphere/

源码及相关资料下载  https://github.com/xxg1413/MachineLearning/tree/master/Learning%20Data%20Mining%20with%20Python/Chapter2

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)

#随着近邻的增加  准确率不断的下降

猜你喜欢

转载自blog.csdn.net/qq_39065788/article/details/82286241