python - 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)]
)
# 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.
谁能告诉我为什么会这样?
解决方案
推荐阅读
- node.js - 如何使用节点的 child_process 库在 mac 上启动交互式终端窗口?
- python - 如何逐行(逐行)从磁盘读取numpy数组?
- rx-java2 - onErrorResumeNext 类型推断失败
- c++ - 声明一个额外的变量导致运行时出现异常
- azure - 使用客户端凭据的 Azure AD 服务到服务调用 - 共享密钥
- javascript - javascript中的数组问题。不打印值
- angular - 使用 Angular 路由,为什么路由模块的模板不包装 router-outlet 的内容?
- java - ActivityNotFoundException:未找到处理 Intent {(有额外内容)} 的活动
- junit - 在 JUNIT 中添加测试步骤以获得更好的报告
- graph-theory - 在 Gremlin 中,我如何将所有边缘重定向到一个顶点,以连接到另一个顶点?