美文网首页
Python sklearn 模型参数调优

Python sklearn 模型参数调优

作者: 克里斯托弗的梦想 | 来源:发表于2019-06-06 17:55 被阅读0次
1、网格搜索法-GridSearchCV
# 导入需要的函数库
from sklearn.ensemble import RandomForestClassifier
from sklearn import datasets
from sklearn.model_selection import GridSearchCV


# 加载iris数据集
iris = datasets.load_iris()
iris_feature = iris['data']
iris_target = iris['target']


# 实例化随机森林分类函数类
forest_clf=RandomForestClassifier(random_state=42)

# 建立需要搜索的参数的范围
param_grid =[{'n_estimators':[10,30,50,100],
             'max_depth':[5,10,20]}]
# 初始化网格搜索的方法
grid_search = GridSearchCV(forest_clf,param_grid,cv=3)
#用网格搜索方法进行拟合数据
grid_search.fit(iris_feature,iris_target)
# 输出最优的参数组合
print(grid_search.best_params_)
best_model = grid_search.best_estimator_

2、随机搜索法-RandomizedSearchCV
#导入相应的函数库
from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
import numpy as np

#加载iris数据集
iris = datasets.load_iris()
iris_feature = iris['data']
iris_target = iris['target']


#建模分析
forest_clf = RandomForestClassifier(random_state=42)
param_distribs = {'n_estimators':range(10,100),'max_depth':range(5,20)}
random_search = RandomizedSearchCV(forest_clf,param_distribs,n_iter=50,cv=3)
random_search.fit(iris_feature,iris_target)
print(random_search.best_params_)
best_model = random_search.best_estimator_

相关文章

网友评论

      本文标题:Python sklearn 模型参数调优

      本文链接:https://www.haomeiwen.com/subject/vniuxctx.html