python - 在 Pipeline 中的特定列上使用 StandardScaler 并连接到原始数据
问题描述
我有一个包含 4 个数字列的数据框,我试图StandardScaler
在Pipeline
. 我使用下面的代码来缩放和转换我的列。
num_feat = ['Quantity']
num_trans = Pipeline([('scale', StandardScaler())])
preprocessor = ColumnTransformer(transformers = ['num', num_trans, num_feat])
pipe = Pipeline([('preproc', preprocessor),
('rf', RandomForestRegressor(random_state = 0))
])
完成此操作后,我将拆分数据并训练我的模型,如下所示。
y = df1['target']
x = df1.drop(['target','ID'], axis = 1)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.2)
pipe.fit(x_train, y_train)
这给了我错误 ValueError: not enough values to unpack (expected 3, got 1)
。我知道这可能是因为我的数据框中的其他 3 个数字列。那么如何将缩放数据连接到剩余的数据帧并在整个数据上训练我的模型。或者有没有更好的方法来做到这一点。
解决方案
请在初始化变压器时添加一个括号。
preprocessor = ColumnTransformer(transformers = [('num', num_trans, num_feat)],remainder='passthrough')
推荐阅读
- react-native - 在 ViroReact 中转换坐标需要帮助
- bash - 如何在 Google Cloud Build 步骤中使用之前从 bash 脚本文件中删除所有 CR (\r) 字符?
- jestjs - 如何重置 dynamodb 本地数据以进行单元测试
- opengl - openGl中是否有任何功能增加线
- python - matplotlib qt5agg 后端分段错误在使用 pyinstaller 构建可分发后发生
- html - 下拉菜单不会出现在 elementor Wordpress 中
- javascript - 我可以为自定义 Material-ui 主题定义断点特定间距吗?
- c# - 在我的 asp.net 2.2 Web 应用程序中遇到身份用户问题,收到“无效的对象名称 Aspnetusers”错误
- azerothcore - 低级错误的普遍性,以及与 TrinityCore 相比易于开发?
- mysql - MySQL 在 Mac 上看起来不一样?