编程语言
首页 > 编程语言> > python-如何在sklearn中的每个交叉验证模型中计算特征重要性

python-如何在sklearn中的每个交叉验证模型中计算特征重要性

作者:互联网

我使用RandomForestClassifier()与10倍交叉验证,如下所示.

clf=RandomForestClassifier(random_state = 42, class_weight="balanced")
k_fold = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
accuracy = cross_val_score(clf, X, y, cv=k_fold, scoring = 'accuracy')
print(accuracy.mean())

我想确定特征空间中的重要特征.如下所示,对于单个分类而言,获得功能重要性似乎很简单.

print("Features sorted by their score:")
feature_importances = pd.DataFrame(clf.feature_importances_,
                                   index = X_train.columns,
                                    columns=['importance']).sort_values('importance', ascending=False)
print(feature_importances)

但是,我找不到如何在sklearn中执行交叉验证的功能重要性.

总而言之,我想确定10倍交叉验证中最有效的功能(例如,通过使用平均重要性得分).

如果需要,我很乐意提供更多详细信息.

解决方法:

对于火车测试折叠的每种组合,cross_val_score()均不会返回估算器.

您需要使用cross_validate()并设置return_estimator = True.

这是一个工作示例:

from sklearn import datasets
from sklearn.model_selection import cross_validate
from sklearn.svm import LinearSVC
from sklearn.ensemble import  RandomForestClassifier
import pandas as pd

diabetes = datasets.load_diabetes()
X, y = diabetes.data, diabetes.target

clf=RandomForestClassifier(n_estimators =10, random_state = 42, class_weight="balanced")
output = cross_validate(clf, X, y, cv=2, scoring = 'accuracy', return_estimator =True)
for idx,estimator in enumerate(output['estimator']):
    print("Features sorted by their score for estimator {}:".format(idx))
    feature_importances = pd.DataFrame(estimator.feature_importances_,
                                       index = diabetes.feature_names,
                                        columns=['importance']).sort_values('importance', ascending=False)
    print(feature_importances)

输出:

Features sorted by their score for estimator 0:
     importance
s6     0.137735
age    0.130152
s5     0.114561
s2     0.113683
s3     0.112952
bmi    0.111057
bp     0.108682
s1     0.090763
s4     0.056805
sex    0.023609
Features sorted by their score for estimator 1:
     importance
age    0.129671
bmi    0.125706
s2     0.125304
s1     0.113903
bp     0.111979
s6     0.110505
s5     0.106099
s3     0.098392
s4     0.054542
sex    0.023900

标签:cross-validation,python,scikit-learn,machine-learning,classification
来源: https://codeday.me/bug/20191011/1895828.html