交叉验证
导包
import numpy as npfrom sklearn.neighbors import KNeighborsClassifierfrom sklearn import datasets#model_selection :模型选择
# cross_val_score: 交叉 ,validation:验证(测试)
#交叉验证
from sklearn.model_selection import cross_val_score
读取datasets中鸢尾花(yuan1wei3hua)数据
X,y= datasets.load_iris(True)
X.shape
(150, 4)
一般情况不会超过数据的开方数
#参考
150**0.5
#K 选择 1~13
12.24744871391589
knn = KNeighborsClassifier()score = cross_val_score(knn,X,y,scoring='balanced_accuracy',cv=11)
score.mean()
0.968181818181818
应用cross_val_score筛选 n_neighbors k值
errors =[]
for k in range(1,14):knn = KNeighborsClassifier(n_neighbors=k)score = cross_val_score(knn,X,y, scoring='accuracy',cv = 6).mean()#误差越小 说明K选择越合适 越好errors.append(1-score)import matplotlib.pyplot as plt
%matplotlib inline#k = 11时 误差最小 说明最合适的k值
plt.plot(np.arange(1,14),errors)
[<matplotlib.lines.Line2D at 0x17ece9ff0b8>]
应用cross_val_score筛选 weights
weights =['uniform','distance']for w in weights:knn = KNeighborsClassifier(n_neighbors = 11,weights= w)print(w,cross_val_score(knn,X,y, scoring='accuracy',cv = 6).mean())
uniform 0.98070987654321
distance 0.9799382716049383
模型如何调参的,参数调节
result = {}
for k in range(1,14):for w in weights:knn = KNeighborsClassifier(n_neighbors=k,weights=w)sm = cross_val_score(knn,X,y,scoring='accuracy',cv=6).mean()result[w+str(k)] =sma =result.values()
list(a)np.array(list(a)).argmax()
20
list(result)[20]
‘uniform11’