python - Sklearn 管道转换特定列 - ValueError : too many values to unpack (expected 2)

标签 python python-3.x machine-learning scikit-learn pipeline

我正在尝试使用缩放器、onhotencoder、多项式特征和最后的线性回归模型制作管道

from sklearn.pipeline import Pipeline
pipeline = Pipeline([
                    ('scaler', StandardScaler(), num_cols),
                    ('polynom', PolynomialFeatures(3), num_cols), 
                    ('encoder', OneHotEncoder(), cat_cols),
                   ('linear_regression', LinearRegression() )
])

但是当我安装管道时,我遇到了 ValueError: 太多值无法解压(预期为 2)

pipeline.fit(x_train,y_train)
pipeline.score(x_test, y_test)

最佳答案

如果我理解正确,您希望将管道的某些步骤应用于特定列。您必须使用 ColumnTransformer ,而不是通过在管道阶段末尾添加列名(这是不正确的并导致错误)来完成此操作。 。 Here你可以找到另一个类似的例子。

就您而言,您可以执行以下操作:

import pandas as pd

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import OneHotEncoder
from sklearn.linear_model import LinearRegression
from sklearn.compose import ColumnTransformer

# Fake data.
train_data = pd.DataFrame({'n1': range(10), 'n2': range(10)})
train_data['c1'] = 0
train_data['c1'][5:] = 1
y_train = [0]*10
y_train[5:] = [1]*5

# Here I assumed you are using a DataFrame. If not, use integer indices instead of column names.
num_cols = ['n1', 'n2']
cat_cols = ['c1']


# Pipeline to transform the numerical features.
numerical_transformer = Pipeline([('scaler', StandardScaler()),
                                  ('polynom', PolynomialFeatures(3))
    
])

# Apply the numerical transformer only on the numerical columns.
# Spearately, apply the OneHotEncoder.
ct = ColumnTransformer([('num_transformer', numerical_transformer, num_cols),
                        ('encoder', OneHotEncoder(), cat_cols)])

# Main pipeline for fitting.
pipeline = Pipeline([
                   ('column_transformer', ct),
                   ('linear_regression', LinearRegression() )
])

pipeline.fit(train_data, y_train)

大致上,管道的布局如下所示:

enter image description here

关于python - Sklearn 管道转换特定列 - ValueError : too many values to unpack (expected 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72283912/

相关文章:

python-3.x - 在困惑的字符串中查找子字符串

python - 如何将字符串列与 Null 合并

algorithm - 我正在寻找可以接受文本字符串并将其转换为数字的算法或函数

python - 如何在 sklearn Logistic 回归的 one-vs-rest 方案中对概率进行归一化?

python - 我们如何使用循环从数组创建复选按钮并打印所选复选按钮的值?

python - django 中的 syncdb 问题

python - 使用值接近 0 的 math.isclose 函数

r - 如何使用路径爬升rpart对象的树结构以手动清除某些节点?

python - 在 Pandas DataFrame 中定位第一个和最后一个非 NaN 值

python - 读取输出日志文件,并使用 bash/python 脚本打印所有唯一的文件路径