【发布时间】:2026-02-07 02:25:01
【问题描述】:
我正在尝试打印出与SimpleImputer 匹配后的所有插补值。当单独使用SimpleImputer 时,我可以从实例的statistics_ 属性中检索这些。
这很好用:
s = SimpleImputer(strategy='mean')
s.fit(df[['feature_1', 'feature_2']])
print(s.statistics_)
但是,在管道中使用 SimpleImputer 时,我无法这样做。
这不起作用:
numeric_transformer = Pipeline(steps=[
('simple_imputer', SimpleImputer(strategy='mean')),
('scaler', StandardScaler())])
categorical_features = ['feature_3']
categorical_transformer = Pipeline(steps=[
('simple_imputer', SimpleImputer(strategy='most_frequent')),
('one_hot', OneHotEncoder(handle_unknown='ignore'))])
preprocessor = ColumnTransformer(
transformers=[
('num', numeric_transformer, numeric_features),
('cat', categorical_transformer, categorical_features)])
clf = Pipeline(steps=[('preprocessor', preprocessor),
('classifier', RandomForestClassifier(n_estimators=100))])
clf.fit(df[numeric_features + categorical_features], df['target'])
print(clf.named_steps['preprocessor'].transformers[0][1].named_steps['simple_imputer'].statistics_)
我收到以下错误:
AttributeError Traceback (most recent call last)
<ipython-input-523-7390eac0d9d6> in <module>
19 clf.fit(df[numeric_features + categorical_features], df['target'])
20
---> 21 print(clf.named_steps['preprocessor'].transformers[0][1].named_steps['simple_imputer'].statistics_)
AttributeError: 'SimpleImputer' object has no attribute 'statistics_
我相信我正在抓取合适的 SimpleImputer 对象的正确实例。为什么我无法检索其statistics_ 属性以打印出插补值?
【问题讨论】:
标签: python scikit-learn pipeline sklearn-pandas imputation