首页 > 解决方案 > AttributeError: Estimator ordinalencoder 不提供 get_feature_names_out。你的意思是调用 pipeline[:-1].get_feature_names_out() 吗?

问题描述

我已经使用 Ordinal Encoder 为我的分类特征 (cat_transformer_ordinal) 实现了一个管道,但是当我想要获取特征名称时,方法 (get_feature_names_out()) 不起作用。我找不到原因。

SimpleImputer.get_feature_names_out = (lambda self, names=None:self.feature_names_in_)
    
encoder_ordinal = OrdinalEncoder(
    categories=feat_ordinal_values_sorted,
    dtype= np.int64,
    handle_unknown="use_encoded_value",
    unknown_value=-1 # Considers unknown values as worse than "missing"
)

num_transformer = make_pipeline(SimpleImputer(), StandardScaler())
num_col = make_column_selector(dtype_include=['float64', 'int64'])

cat_transformer_ordinal = make_pipeline(SimpleImputer(strategy="constant", fill_value="missing"),
                                        encoder_ordinal,
                                        MinMaxScaler())

cat_col_onehot = list(set(features_categorical_small) - set(feat_ordinal))

preproc_pipeline = make_column_transformer(
    (num_transformer, num_col),
    (cat_transformer_ordinal, feat_ordinal),
    (OneHotEncoder(), cat_col_onehot)
)

X = preproc_pipeline.fit_transform(df_train)


preproc_pipeline.get_feature_names_out()

我收到此错误:

    749         for _, name, transform in self._iter():
    750             if not hasattr(transform, "get_feature_names_out"):
--> 751                 raise AttributeError(
    752                     "Estimator {} does not provide get_feature_names_out. "
    753                     "Did you mean to call pipeline[:-1].get_feature_names_out"

AttributeError: Estimator ordinalencoder does not provide get_feature_names_out. Did you mean to call pipeline[:-1].get_feature_names_out()?

我不明白为什么......因为现在 get_feature_names_out() 已为所有 sklearn.preprocessing 模块实现

标签: pythonscikit-learn

解决方案


推荐阅读