Python sklearn 模型参数调优

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 模型参数调优)