交叉验证与网格搜索

交叉验证

交叉验证是一种数据集分割方式,将数据集划分为n份,取一份做测试集,其他n-1份做训练集。
目的:为了得到更加准确可信的模型评分
原理:将数据集划分为cv=n份

  1. 将第一份做验证集,其他数据进行训练
  2. 将第二份做验证…
  3. 以此类推,总共训练n次,评估n次
  4. 使用训练集+验证集多次评估,取平均值做交叉验证为模型得分。
  5. 若k=5模型得分最好,在使用全部训练集对k=5模型再训练并使用测试集对k=5模型做评估。

网格搜索

•模型有很多超参数,其能力也存在很大的差异。需要手动产生很多超参数组合,来训练模型
•每组超参数都采用交叉验证评估,最后选出最优参数组合建立模型。
目的:模型调参的工具,寻找最优超参数的工具。
原理:只需要将若干参数传递给网格搜索对象,它自动帮我们完成不同超参数的组合、模型训练、模型评估,最终返回一组最优的超参数。
•网格搜索+交叉验证的强力组合(模型选择和调优)
•交叉验证解决模型的数据输入问题(数据集划分)得到更可靠的模型
•网格搜索解决超参数的组合
•两个组合再一起形成一个模型参数调优的解决方案

利用KNN算法对鸢尾花分类–交叉验证网格搜索

from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing
import StandardScaler
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.neighbors import KNeighborsClassifier

# 1
获取数据集
mydataset= load_iris()
# 2数据基本处理-划分数据集
x_train, x_test, y_train,y_test= train_test_split(mydataset.data, mydataset.target,test_size=0.2,random_state=22)
# 3数据集预处理-数据标准化
transfer = StandardScaler()
x_train= transfer.fit_transform(x_train)
x_test= transfer.transform(x_test)
# 4机器学习(模型训练)
estimator = KNeighborsClassifier()
print('estimator-->', estimator)
# 4-2使用校验验证网格搜索
param_grid= {'n_neighbors':[1,3,5,7]}
#输入一个estimator,出来一个estimator(功能变的强大)
estimator = GridSearchCV(estimator=estimator, param_grid=param_grid, cv=5)
estimator.fit(x_train, y_train)
# 4个模型每个模型进行网格搜素找到做好的模型
# 4-3交叉验证网格搜索结果查看
# estimator.best_score_ .best_estimator_ .best_params_ .cv_results_
print('estimator.best_score_---', estimator.best_score_)
print('estimator.best_estimator_---', estimator.best_estimator_)
print('estimator.best_params_---', estimator.best_params_)
print('estimator.cv_results_---', estimator.cv_results_)
# 4-4保存交叉验证结果
myret= pd.DataFrame(estimator.cv_results_)
myret.to_csv(path_or_buf='./mygridsearchcv.csv')
# 5
模型评估
myscore= estimator.score(x_test, y_test)
print('myscore-->', myscore)

利用KNN算法实现手写数字识别


import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection
import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import joblib
from collections import Counter
# 1加载数据
data = pd.read_csv(‘data/手写数字识别.csv’)
if idx< 0 or idx> len(data) -1:
	return
# 2打印数据基本信息
x = data.iloc[:, 1:]
y = data.iloc[:, 0]
print(‘数据基本信息:, x.shape)
print(‘类别数据比例:, Counter(y))
print(‘当前数字的标签为:,y[idx])
# 3显示指定的图片
# data修改为ndarray类型
data_ = x.iloc[idx].values
#将数据形状修改为28*28
data_ = data_.reshape(28, 28)
#关闭坐标轴标签
plt.axis(‘off’)
#显示图像
plt.imshow(data_,cmap=‘gray’)
plt.show()

KNN实现乳腺癌检测

from sklearn.datasets import load_breast_cancer
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.neighbors import KNeighborsClassifier
#获取数据集
breast_cancer=load_breast_cancer()
#数据集划分
x_train,x_test,y_train,y_test=train_test_split(breast_cancer.data,breast_cancer.target,test_size=0.2,random_state=22)
#数据标准化
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)
#模型测试
estimator = KNeighborsClassifier()
#使用网格搜索
param_grid= {'n_neighbors':[1,3,5,7]}
estimator = GridSearchCV(estimator=estimator, param_grid=param_grid, cv=5)
estimator.fit(x_train, y_train)
#模型评估
myscore= estimator.score(x_test, y_test)
print('myscore-->', myscore)
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值