1.交叉验证定义:为了使得训练模型更加准确
2.网格搜索:
超参数:就是手动设置的参数
网格搜索:不同的K值下进行交叉验证,得出最优解
第二个参数以字典形式传进来,设置K值,注意一定要设置成param_dict = {“n_neighbors”: [1, 3, 5, 7, 9, 11]}模式!只用字典传入会报错,因为n_neighbors是一个K参数!
第三个:相当于几次交叉验证
代码:
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV
#1.调用花的值变成4组集合,并设置随机数种子
#步骤1:先实例化花类:
iris = load_iris()
print('iris:\n',iris)
print('iris的特征值:\n',iris.data)
#步骤二进行划分
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=22)
#2.对训练集和测试集的特征进行标准化
transfer = StandardScaler()
#训练集数据因为担心会有异常数据影响,所以才fit进行标准差,而transform只是起一个转换模式的作用,此时transform储藏的就是训练集的标准差和平均值
x_train = transfer.fit_transform(x_train)
#测试集若再fit则会改变平均值标准差,导致两个标准不同,因此应该用训练集的,因此直接转换成和训练集相同的格式即可
x_test = transfer.transform(x_test)
#3.对训练集进行训练,产生模型
estimator = KNeighborsClassifier()
#加入网格搜索与交叉验证
param_dict = {"n_neighbors": [1, 3, 5, 7, 9, 11]}
estimator = GridSearchCV(estimator,param_grid=param_dict,cv=10)
estimator.fit(x_train,y_train)
#4.拿测试集进行测试对比
#步骤1:对测试集进行预测
y_pridict = estimator.predict(x_test)
print("y_predict:\n", y_pridict)
#步骤二:对测试集结果进行比对
print('对比预测值和正确值:\n',y_test == y_pridict)
#步骤三:计算准确率
score = estimator.score(x_test,y_test)
print('准确率为:\n',score)
#最佳参数
print('最佳参数:\n',estimator.best_params_)
# 最佳结果:best_score_
print("最佳结果:\n", estimator.best_score_)
# 最佳估计器:best_estimator_
print("最佳估计器:\n", estimator.best_estimator_)
# 交叉验证结果:cv_results_
print("交叉验证结果:\n", estimator.cv_results_)
注意:先网格搜索交叉验证再estimator.fit(x_train,y_train)的原因可以思考为只是在预估器中增加了一些参数,然后在传入x,y从而能够在训练模型的时候得出最优解
结果:
y_predict:
[0 2 1 2 1 1 1 1 1 0 2 1 2 2 0 2 1 1 1 1 0 2 0 1 2 0 2 2 2 2 0 0 1 1 1 0 0
0]
对比预测值和正确值:
[ True True True True True True True False True True True True
True True True True True True False True True True True True
True True True True True True True True True True True True
True True]
准确率为:
0.9473684210526315
最佳参数:
{'n_neighbors': 7}
最佳结果:
0.9642857142857143
最佳估计器:
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
metric_params=None, n_jobs=None, n_neighbors=7, p=2,
weights='uniform')
交叉验证结果:
{'mean_fit_time': array([1.96337700e-04, 1.99437141e-04, 2.99358368e-04, 9.69409943e-05,
1.99770927e-04, 2.99811363e-04]), 'std_fit_time': array([0.00039269, 0.00039888, 0.00045728, 0.00029082, 0.00039954,
0.00045798]), 'mean_score_time': array([0.00080109, 0.00069785, 0.00049617, 0.0007977 , 0.00080121,
0.00059845]), 'std_score_time': array([0.00040083, 0.00045728, 0.00049635, 0.00039886, 0.00040068,
0.00048864]), 'param_n_neighbors': masked_array(data=[1, 3, 5, 7, 9, 11],
mask=[False, False, False, False, False, False],
fill_value='?',
dtype=object), 'params': [{'n_neighbors': 1}, {'n_neighbors': 3}, {'n_neighbors': 5}, {'n_neighbors': 7}, {'n_neighbors': 9}, {'n_neighbors': 11}], 'split0_test_score': array([0.91666667, 0.91666667, 1. , 1. , 0.91666667,
0.91666667]), 'split1_test_score': array([1., 1., 1., 1., 1., 1.]), 'split2_test_score': array([0.91666667, 0.91666667, 0.91666667, 0.91666667, 0.91666667,
0.91666667]), 'split3_test_score': array([0.91666667, 1. , 0.91666667, 1. , 1. ,
1. ]), 'split4_test_score': array([1., 1., 1., 1., 1., 1.]), 'split5_test_score': array([0.91666667, 0.91666667, 0.91666667, 0.91666667, 0.91666667,
0.91666667]), 'split6_test_score': array([0.81818182, 0.81818182, 0.90909091, 0.90909091, 0.90909091,
0.90909091]), 'split7_test_score': array([0.9, 0.9, 0.9, 0.9, 0.9, 0.9]), 'split8_test_score': array([1., 1., 1., 1., 1., 1.]), 'split9_test_score': array([1., 1., 1., 1., 1., 1.]), 'mean_test_score': array([0.9375 , 0.94642857, 0.95535714, 0.96428571, 0.95535714,
0.95535714]), 'std_test_score': array([0.0566529 , 0.05917648, 0.04408745, 0.04378505, 0.04408745,
0.04408745]), 'rank_test_score': array([6, 5, 2, 1, 2, 2])}