Feature importances - Bagging, scikit-learn

后端 未结 2 1451
忘了有多久
忘了有多久 2021-01-04 21:14

For a project I am comparing a number of decision trees, using the regression algorithms (Random Forest, Extra Trees, Adaboost and Bagging) of scikit-learn. To compare and i

2条回答
  •  心在旅途
    2021-01-04 22:00

    Are you talking about BaggingClassifier? It can be used with many base estimators, so there is no feature importances implemented. There are model-independent methods for computing feature importances (see e.g. https://github.com/scikit-learn/scikit-learn/issues/8898), scikit-learn doesn't use them.

    In case of decision trees as base estimators you can compute feature importances yourselves: it'd be just an average of tree.feature_importances_ among all trees in bagging.estimators_:

    import numpy as np
    from sklearn.ensemble import BaggingClassifier
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.datasets import load_iris
    
    X, y = load_iris(return_X_y=True)
    clf = BaggingClassifier(DecisionTreeClassifier())
    clf.fit(X, y)
    
    feature_importances = np.mean([
        tree.feature_importances_ for tree in clf.estimators_
    ], axis=0)
    

    RandomForestClassifer does the same computation internally.

提交回复
热议问题