zoukankan      html  css  js  c++  java
  • StratifiedKFold与GridSearchCV版本前后使用方法

    首先在sklearn官网上你可以看到:

    所以,旧版本import时:

    from sklearn.cross_validation import GridSearchCV

    新版本import时:
    from sklearn.model_selection import GridSearchCV

    StratifiedKFold同样是这个问题,我用的是pycharm,IDE会自动提示这一点。

    <----------------------------------分割线------------------------------------------->

    之前版本StratifiedKFold与GridSearchCV的结合使用代码如下:
    比如我用的是决策树
    from sklearn.grid_search import GridSearchCV
    from sklearn.cross_validation import StratifiedKFold
    
    decision_tree_classifier = DecisionTreeClassifier()
    
    parameter_grid = {'max_depth': [1, 2, 3, 4, 5],
                      'max_features': [1, 2, 3, 4]}
    
    cross_validation = StratifiedKFold(all_classes, n_folds=10)
    
    grid_search = GridSearchCV(decision_tree_classifier,
                               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_))
    

      版本升级后,StratifiedKFold与GridSearchCV的结合使用代码如下:

    from sklearn.model_selection import GridSearchCV
    from sklearn.model_selection import StratifiedKFold
    
    decision_tree_classifier = DecisionTreeClassifier()
    
    parameter_grid = {'max_depth': [1, 2, 3, 4, 5],
                      'max_features': [1, 2, 3, 4]}
    
    skf = StratifiedKFold(n_splits=10)
    cross_validation = skf.get_n_splits(all_inputs, all_classes)
    grid_search = GridSearchCV(decision_tree_classifier, param_grid=parameter_grid,cv=cross_validation)
    grid_search.fit(all_inputs, all_classes)
    print("Best score:", grid_search.best_score_)
    print("Best param:", grid_search.best_params_)
    

      

    对比代码,你会发现 StratifiedKFold()参数不同了,更多信息请参考sklearn官网文档。

  • 相关阅读:
    web访问权限实现方法-探面向对象的编码设计
    解析二进制反码算数求和
    可以把erp当做一个分支-找自己的方向
    电脑开机是怎样自动加载进程
    球管模型和Java
    发明和发现
    漂浮
    js中的一些循环
    ES5中新增的一些方法
    git的一些操作
  • 原文地址:https://www.cnblogs.com/shanlizi/p/9239621.html
Copyright © 2011-2022 走看看