我尝试通过以下方法获得功能排名:
1. Standardscaler
2. RandomForestClassifier
3. Recursive feature selection
from sklearn.feature_selection import RFECV
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_predict, KFold
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.datasets import load_iris
data = load_iris()
X = data.data
Y = data.target
clf = RandomForestClassifier()
estimators = [('standardize' , StandardScaler()),
('rfecv', RFECV(estimator=clf, scoring='accuracy'))]
pipeline = Pipeline(estimators)
ranking_features = pipeline.named_steps['rfecv'].ranking_
print (ranking_features)
AttributeError:“ RFECV”对象没有属性“ ranking _”
欢迎任何实现此目的的最佳实践。
答案 0 :(得分:0)
我们首先使用rfecev
来拟合数据,然后再调用ranking_
属性。尝试运行以下代码:
from sklearn.feature_selection import RFECV
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_predict, KFold
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.datasets import load_iris
data = load_iris()
X = data.data
Y = data.target
clf = RandomForestClassifier()
estimators = [('standardize' , StandardScaler()),
('rfecv', RFECV(estimator=clf, scoring='accuracy'))]
# create pipeline
pipeline = Pipeline(estimators)
# fit rfecv to data
rfecv_data = pipeline.named_steps['rfecv'].fit(X, Y)
# get the feature ranking
ranking_features = rfecv_data.ranking_
print (ranking_features)
'Output':
[2 3 1 1]