python [Python] GridSearchCV示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python [Python] GridSearchCV示例相关的知识,希望对你有一定的参考价值。

'''
To search to tune parameter is to use Grid Search. Basically, it explores a 
range of parameters and finds the best combination of parameters. 
Then repeat the process several times until the best parameters are discovered. 
We will also use Stratified k-fold cross-validation that will prevent a certain
class only split them to the same subset.
'''

from sklearn.grid_search import GridSearchCV

dtc = DecisionTreeClassifier()

parameter_grid = {'criterion': ['gini', 'entropy'],
                  'splitter': ['best', 'random'],
                  'max_depth': [1, 2, 3, 4, 5],
                  'max_features': [1, 2, 3, 4]}

cross_validation = StratifiedKFold(all_classes, n_folds=10)

grid_search = GridSearchCV(dct, param_grid=parameter_grid, cv=cross_validation)

grid_search.fit(all_inputs, all_classes)
print('Best score: {}'.format(grid_search.best_score_))
print('Best parameters: {}'.format(grid_search.best_params_))

dtc = grid_search.best_estimator_
dtc

以上是关于python [Python] GridSearchCV示例的主要内容,如果未能解决你的问题,请参考以下文章

代写python,代写python编程,python代写,python编程代写,留学生python代写

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍