1、cross_val_score
from sklearn.model_selection import cross_val_score
knn_clf = KNeighborsClassifier()
# 返回的是一个数组,有五个元素,说明cross_val_score方法默认将我们的数据集分成了五份
# 这五份数据集进行交叉验证后产生了这五个结果
# cv默认为5,可以修改改参数,修改修改不同分数的数据集
cross_val_score(knn_clf,X_train,y_train,cv=5)
# 使用交叉验证的方式来进行调参的过程
best_score,best_k,best_p = 0,0,0
# k为k近邻中的寻找k个最近元素
for k in range(2,11):
# p为明科夫斯基距离的p
for p in range(1,6):
knn_clf = KNeighborsClassifier(weights='distance',n_neighbors=k,p=p)
scores = cross_val_score(knn_clf,X_train,y_train)
score = np.mean(scores)
if score > best_score:
best_score,best_k,best_p = score,k,p
print("Best_score = ",best_score)
print("Best_k = ",best_k)
print("Best_p = ",best_p)
2、网格搜索GridSearchCV
from sklearn.model_selection import GridSearchCV
param_grid = [
{
'weights':['distance'],
'n_neighbors':[i for i in range(2,11)],
'p':[i for i in range(1,6)]
}
]
grid_search = GridSearchCV(knn_clf,param_grid,verbose=1,cv=5)
grid_search.fit(X_train,y_train)
#最好的分数
grid_search.best_score_
#最好的参数
grid_search.best_params_
#最好的模型
best_knn_clf = grid_search.best_estimator_
实验结果表明,两种方法得到的结果一致。