机器学习——超参数搜索

https://www.cnblogs.com/wkslearner/p/9302044.html
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():
#1.获取数据集
iris = load_iris()
#print(iris)
#2.划分数据集
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=6)

#3.特征工程:标准化
transfor = StandardScaler()
x_train = transfor.fit_transform(x_train)
x_test = transfor.transform(x_test)

#KNN算法预估器
estimator = KNeighborsClassifier(n_neighbors=3)
estimator.fit(x_train,y_train)

#5.模型评估
#方法1:直接比对真实值和预估值
y_predict = estimator.predict(x_test)
print("y_predict:\n",y_predict)
print("直接对比真实值和预测值:\n",y_test == y_predict)

#方法2:计算准确率
score = estimator.score(x_test,y_test)
print("准确率为:\n",score)
return None

def knn_iris_gsch():
#1.获取数据集
iris = load_iris()
#print(iris)
#2.划分数据集
x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,random_state=6)

#3.特征工程:标准化
transfor = StandardScaler()
x_train = transfor.fit_transform(x_train)
x_test = transfor.transform(x_test)

#KNN算法预估器
estimator = KNeighborsClassifier()
##超参数网格搜索
#参数准备
#para_dict = {'param_grid ':[1,3,5,7]}
param_dict=[1,2,3]
estimator = GridSearchCV(estimator,param_grid=param_dict,cv=10)
estimator.fit(x_train,y_train)

#5.模型评估
#方法1:直接比对真实值和预估值
y_predict = estimator.predict(x_test)
print("y_predict:\n",y_predict)
print("直接对比真实值和预测值:\n",y_test == y_predict)

#方法2:计算准确率
score = estimator.score(x_test,y_test)
print("准确率为:\n",score)
#最佳参数best_params
print("最佳参数:\n",estimator.best_params_)
# 最佳结果best_score
print("最佳结果:\n", estimator.best_score_)
#最佳估计器best_estimator_
print("最佳估计器:\n",estimator.best_estimator_)
return None

if name == ‘main’:
#代码1,用KNN算法对鸢尾花进行分类
#knn_iris()
#代码2,用KNN算法超参数网格搜索对鸢尾花进行分类
knn_iris_gsch()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值