【问题标题】:Pruning and Boosting in Decision Trees决策树中的修剪和提升
【发布时间】:2015-09-22 18:04:13
【问题描述】:

如何在基于决策树的分类方法中使用 Pruning 和 Boosting?

I have 10 features and 3000 samples.

【问题讨论】:

    标签: scikit-learn


    【解决方案1】:

    这是一个演示如何使用 Boosting 的示例。

    from sklearn.datasets import make_classification
    from sklearn.ensemble import GradientBoostingClassifier
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.cross_validation import StratifiedShuffleSplit
    from sklearn.metrics import classification_report
    
    # generate some artificial data
    X, y = make_classification(n_samples=3000, n_features=10, n_informative=2, flip_y=0.1, weights=[0.15, 0.85], random_state=0)
    
    # train/test split
    split = StratifiedShuffleSplit(y, n_iter=1, test_size=0.2, random_state=0)
    train_index, test_index = list(split)[0]
    X_train, y_train = X[train_index], y[train_index]
    X_test, y_test = X[test_index], y[test_index]
    
    # boosting: many many weak classifiers (max_depth=1) refine themselves sequentially
    # tree is the default the base classifier
    estimator = GradientBoostingClassifier(n_estimators=200, learning_rate=0.1, max_depth=1, random_state=0)
    estimator.fit(X_train, y_train)
    y_pred = estimator.predict(X_test)
    print(classification_report(y_test, y_pred))
    
                 precision    recall  f1-score   support
    
              0       0.88      0.80      0.84       109
              1       0.96      0.98      0.97       491
    
    avg / total       0.94      0.94      0.94       600
    
    # benchmark: a standard tree
    tree_benchmark = DecisionTreeClassifier(max_depth=3, class_weight='auto')
    tree_benchmark.fit(X_train, y_train)
    y_pred_benchmark = tree_benchmark.predict(X_test)
    print(classification_report(y_test, y_pred_benchmark))
    
                 precision    recall  f1-score   support
    
              0       0.63      0.84      0.72       109
              1       0.96      0.89      0.92       491
    
    avg / total       0.90      0.88      0.89       600
    

    【讨论】:

    • 谢谢,已接受。我也可以将提升应用于随机森林分类器吗?
    • @jean Random Forestbagging 而不是 boosting。在 boosting 中,我们允许许多弱分类器(高偏差和低方差)按顺序从错误中学习,目的是在保持低方差属性的同时纠正他们的high bias 问题。在 bagging 中,我们使用许多overfitted classifiers(低偏差但高方差)并进行引导以减少方差。如果你想将 boosting 和 bagging 结合起来,可以考虑Stochastic Gradient Boosting 方法。
    • 谢谢!所以修剪是通过选择特征来完成的,即'feature_importances_'?
    猜你喜欢
    • 2012-02-09
    • 2014-05-22
    • 2019-06-14
    • 2011-04-28
    • 2011-05-02
    • 1970-01-01
    • 2021-10-01
    • 2015-03-20
    相关资源
    最近更新 更多