[英]Extracting Feature Importance with Feature Names from a Sklearn Pipeline
我想知道當在帶有預處理的管道中使用分類器時,如何從scikit-learn中的隨機森林中提取具有特征名稱的特征重要性。
這里的問題僅涉及提取要素重要性: 如何從Sklearn管道提取要素重要性
從我所做的簡短研究來看,這在scikit-learn中似乎是不可能的,但我希望我是錯的。
我還找到了一個名為ELI5的軟件包( https://eli5.readthedocs.io/en/latest/overview.html ),該軟件包可以通過scikit-learn解決該問題,但由於解決方案的名稱而無法解決我的問題為我輸出的功能是x1,x2等,而不是實際的功能名稱。
作為一種解決方法,我在管道之外進行了所有預處理,但是很想知道如何在管道中進行預處理。
如果我可以提供任何有用的代碼,請在評論中告訴我。
Xgboost有一個獲取功能重要性的示例:
num_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
('scaler', preprocessing.RobustScaler())])
cat_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='most_frequent')),
('onehot', preprocessing.OneHotEncoder(categories='auto',
sparse=False,
handle_unknown='ignore'))])
from sklearn.compose import ColumnTransformer
numerical_columns = X.columns[X.dtypes != 'category'].tolist()
categorical_columns = X.columns[X.dtypes == 'category'].tolist()
pipeline_procesado = ColumnTransformer(transformers=[
('numerical_preprocessing', num_transformer, numerical_columns),
('categorical_preprocessing', cat_transformer, categorical_columns)],
remainder='passthrough',
verbose=True)
# Create the classifier
classifier = XGBClassifier()
# Create the overall model as a single pipeline
pipeline = Pipeline([("transform_inputs", pipeline_procesado), ("classifier",
classifier)])
pipeline.fit(X_train, y_train)
onehot_columns = pipeline.named_steps['transform_inputs'].named_transformers_['categorical_preprocessing'].named_steps['onehot'].get_feature_names(input_features=categorical_columns)
#you can get the values transformed with your pipeline
X_values = pipeline_procesado.fit_transform(X_train)
df_from_array_pipeline = pd.DataFrame(X_values, columns = numerical_columns + list(onehot_columns) )
feature_importance = pd.Series(data= pipeline.named_steps['classifier'].feature_importances_, index = np.array(numerical_columns + list(onehot_columns)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.