from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV
def knn_iris_gscv():
#1、获取数据
iris = load_iris()
#2、划分数据集
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=6)
#3、特征工程标准化
transfer = StandardScaler()
x_trains = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)#只用transform保持数据平均值和方差一致性,如果用fit那么是测试机的平均值和方差
#4、knn算法预估器
estimate = KNeighborsClassifier()
#加入网格搜索和交叉验证
param_dict={"n_neighbors":[1,3,5,7,11]}
estimater = GridSearchCV(estimate,param_grid=param_dict,cv=10)
estimate.fit(x_train,y_train)
#5、模型评估
#方法一:直接比对真实值和预测值
y_predict = estimate.predict(x_test)
print(y_predict)
print(y_predict==y_test)
#方法二:计算准确率
score = estimate.score(x_test,y_test)
print("准确率为"+score)
# 最佳参数
print("最佳的k" + estimater.best_params_)
#最佳结果
print("最佳结果"+estimater.best_score_)
#最佳估计器
print("最佳估计器"+estimater.best_estimator_)
return None
KNN改进
最新推荐文章于 2024-02-23 10:15:35 发布