超参数选择方法
交叉验证、网格搜索、手写数字识别案例
交叉验证
1.定义:是一种数据集的分割方法,将训练集划分为n份,拿一份做验证集(测试集)、其他n-1份做训练集。
2.原理:将数据集划分为cv=4 份
3.目的:为了得到更加准确可信的模型评分
网格搜索
网格搜索是模型调参的有力工具。寻找最优超参数的工具!
只需要将若干参数传递给网格搜索对象,它自动帮我们完成不同超参数的组合、模型训练、模型评估,最终返回一组最优的超参数。
利用KNN算法对鸢尾花分类—交叉验证网格搜索
from sklearn.datasets import load_iris
iris_data = load_iris()
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(iris_data.data,iris_data.target,train_size=0.3,random_state=22)
from sklearn.preprocessing import StandardScaler
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
from sklearn.neighbors import KNeighborsClassifier
estimator = KNeighborsClassifier()
from sklearn.model_selection import GridSearchCV
estimator = GridSearchCV(estimator=estimator,param_grid={'n_neighbors':[1,3,5,7]},cv = 5)
estimator.fit(x_train,y_train)
print(f'estimator.best_params_:{estimator.best_params_}')
import pandas as pd
cvresults = pd.DataFrame(estimator.cv_results_)
cvresults.to_csv(path_or_buf='./cvresult.csv')
score = estimator.score(x_test,y_test)
print(f'score-->{score}')
利用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
def show_digit(a):
data = pd.read_csv('data/手写数字识别.csv')
if a >= 0 and a <= len(data) - 1:
pass
else:
return
x = data.iloc[:, 1:]
y = data.iloc[:, 0:]
print('数据基本信息:', x.shape)
print('类别数据比例:', Counter(y))
print('当前数字的标签为:', y[a])
# 显示指定的图片
# data修改为ndarray类型
data_ = x.iloc[a].values
# 将数据形状修改为28*28
data_ = data_.reshape(28, 28)
# 关闭坐标轴标签
plt.axis('off')
# 显示图像
plt.imshow(data_, cmap='gary')
plt.show()
KNN案例预测乳腺癌良性和恶性
# 导入所需的库
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
# 加载乳腺癌数据集
data = load_breast_cancer()
x = data.data # 特征值
y = data.target # 目标值
# 划分训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=42)
# 创建KNN分类器对象
knn = KNeighborsClassifier(n_neighbors=6)
# 在训练集上训练KNN模型
knn.fit(x_train, y_train)
# 在测试集上进行预测
y_pred = knn.predict(x_test)
# 查看乳腺癌数据集基本信息和特征
breast_cancer = load_breast_cancer()
print(breast_cancer.DESCR)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("准确率:", accuracy)
以上为我的分享~