DataFrameMapper scikit-learn ValueError:除了连接轴之外的所有输入数组维度必须完全匹配

时间:2017-05-20 23:31:43

标签: python pandas scikit-learn sklearn-pandas

我一直在尝试使用DataFrameMapper将我的数据框上的多个预处理转换添加到我的scikit-learn Pipeline中。

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/abalone/abalone.data"
names = ['Sex', 'Length', 'Diameter', 'Height', 'Whole weight', 'Schuked weight', 'Viscera weight', 'Shell weight', 'Rings']

df = pd.read_csv(url, names=names)

mapper = DataFrameMapper(
    [('Height', Normalizer()), ('Sex', LabelBinarizer())]
)

stages = []

stages += [("mapper", mapper)]

estimator = DecisionTreeClassifier()

stages += [("dtree", estimator)]

pipeline = Pipeline(stages)

labelCol = 'Rings'
target = df[labelCol]
data = df.drop(labelCol, axis=1)

train_data, test_data, train_target, expected = train_test_split(data, target, test_size=0.25, random_state=33)

model = pipeline.fit(train_data, train_target)

但是,我收到以下错误:

Traceback (most recent call last):
  File "app/experimenter/sklearn/transformations.py", line 65, in <module>
    model = pipeline.fit(train_data, train_target)
  File "/Library/Python/2.7/site-packages/sklearn/pipeline.py", line 268, in fit
    Xt, fit_params = self._fit(X, y, **fit_params)
  File "/Library/Python/2.7/site-packages/sklearn/pipeline.py", line 234, in _fit
    Xt = transform.fit_transform(Xt, y, **fit_params_steps[name])
  File "/Library/Python/2.7/site-packages/sklearn/base.py", line 497, in fit_transform
    return self.fit(X, y, **fit_params).transform(X)
  File "/Library/Python/2.7/site-packages/sklearn_pandas/dataframe_mapper.py", line 225, in transform
    stacked = np.hstack(extracted)
  File "/Library/Python/2.7/site-packages/numpy/core/shape_base.py", line 288, in hstack
    return _nx.concatenate(arrs, 1)
ValueError: all the input array dimensions except for the concatenation axis must match exactly

我错过了什么?

谢谢:)

1 个答案:

答案 0 :(得分:2)

您必须改变DataFrameMapper

的构造
mapper = DataFrameMapper(
    [(['Height'], Normalizer()), ('Sex', LabelBinarizer())]
)

这是一个细微的细节,可以在sklearn_pandas的文档中找到:

  

将列映射到转换

     

将列选择器指定为'column'(作为简单字符串)和['column'](作为具有一个元素的列表)之间的区别在于传递给转换器的数组的形状。在第一种情况下,将传递一维数组,而在第二种情况下,它将是具有一列的二维数组,即列向量。

     

[...]

     

请注意,某些变换器需要一维输入(面向标签的输入),而其他一些变换器(如OneHotEncoderImputer)需要二维输入,形状为{{1 }}