首页 > 解决方案 > NotFittedError:使用管道拟合和转换数据仍然出现未拟合错误

问题描述

在使用管道进行单热编码后,我试图获取功能名称。我可以看到我的数据已按指定进行转换,但是当我尝试获取功能名称时,我得到了这个 NotFittedError。这些是我采取的步骤。

# full pipeline for data preprocessing

# seperate the fetures and the target
X_train = train.drop("target", axis=1).copy()
y_train = train["target"].copy()
X_test = test.copy()

# create preprocessing pipeline
from sklearn.pipeline import Pipeline, make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer

# select numerical and categorical columns
num_cols = X_train.select_dtypes(exclude="object").columns.tolist()
cat_cols = X_train.select_dtypes(include="object").columns.tolist()

# numerical pipeline
num_pipe = make_pipeline(SimpleImputer(strategy="mean"), StandardScaler())

# categorical pipeline
cat_pipe = make_pipeline(
    SimpleImputer(strategy="constant", fill_value="NA"),
    OneHotEncoder(handle_unknown='ignore', sparse=False),
)

# full pipeline for data preprocessing
full_pipe = ColumnTransformer(
    [("num", num_pipe, num_cols), ("cat", cat_pipe, cat_cols)]
)

full_pipe 输出- 在此处输入图像描述

# fit and transform the training and test set
X_train_tr = full_pipe.fit_transform(X_train)
X_test_tr = full_pipe.transform(X_test)

在我尝试获取功能名称时安装管道后,我得到了这个 NotFittedError。

full_pipe.transformers[1][1].steps[1][1]
output - OneHotEncoder(handle_unknown='ignore', sparse=False)
# get one-hot encoded feature names
full_pipe.transformers[1][1].steps[1][1].get_feature_names()

NotFittedError: This OneHotEncoder instance is not fitted yet. Call 'fit' with appropriate arguments before using this estimator.

谁能告诉我为什么会这样?

标签: pythonscikit-learn

解决方案


推荐阅读