sklearn pipeline ValueError: all the input array dimensions except for the concatenation axis must match exactly

sklearn pipeline ValueError: all the input array dimensions except for the concatenation axis must match exactly

我有一个 sklearn 管道,它提取三个不同的特征。

manual_feats = Pipeline([
        ('FeatureUnion', FeatureUnion([
            ('segmenting_pip1', Pipeline([
                ('A_features', A_features()),
                ('segmentation', segmentation())
            ])),
            ('segmenting_pip2', Pipeline([
                ('B_features', B_features(),
                ('segmentation', segmentation())
            ])),
            ('segmenting_pip3', Pipeline([
                ('Z_features', Z_features()),
                ('segmentation', segmentation())
            ])),

        ])),
    ])

鉴于特征 AB 每个 returns 一个暗淡的数组(# of records, 10, 20), while Z returns (# of records, 10, 15).

当我为管道安装所有功能时,出现此错误:

 File "C:\Python35\lib\site-packages\sklearn\pipeline.py", line 451, in _transform
    Xt = transform.transform(Xt)
  File "C:\Python35\lib\site-packages\sklearn\pipeline.py", line 829, in transform
    Xs = np.hstack(Xs)
  File "C:\Python35\lib\site-packages\numpy\core\shape_base.py", line 340, in hstack
    return _nx.concatenate(arrs, 1)
ValueError: all the input array dimensions except for the concatenation axis must match exactly

但是,如果我排除功能 Z,则管道可以工作,但在轴上应用的串联 = 1 dim(记录数,20、20)。我想要的是获得一个(记录数,10、40)维度的数组,其中串联过程应用于 axis=2.

如何在不编辑库源代码的情况下使用 Pipeline 获得我想要的内容?

编辑: 我提到 AB 的串联产生了一个 (# of records, 10, 40) DIM 数组。这是不正确的;它生成一个 DIM 数组(# of records,20、20)。我会编辑问题。

我通过创建一个处理串联过程的转换器解决了这个问题。

class append_split_3D(BaseEstimator, TransformerMixin):
    def __init__(self, segments_number=20, max_len=50, mode='append'):
        self.segments_number = segments_number
        self.max_len = max_len
        self.mode = mode
        self.appending_value = -5.123

    def fit(self, X, y=None):
        return self

    def transform(self, data):
        if self.mode == 'append':
            self.max_len = self.max_len - data.shape[2]
            appending = np.full((data.shape[0], data.shape[1], self.max_len), self.appending_value)
            new = np.concatenate([data, appending], axis=2)
            return new
        elif self.mode == 'split':
            tmp = []
            for item in range(0, data.shape[1], self.segments_number):
                tmp.append(data[:, item:(item + self.segments_number), :])
            tmp = [item[item != self.appending_value].reshape(data.shape[0], self.segments_number, -1) for item in tmp]
            new = np.concatenate(tmp, axis=2)
            return new
        else:
            print('Error: Mode value is not defined')
            exit(1)

完整的管道变成这样:

manual_feats = Pipeline([
        ('FeatureUnion', FeatureUnion([
            ('segmenting_pip1', Pipeline([
                ('A_features', A_features()),
                ('segmentation', segmentation()),
                ('append', append_split_3D(max_len=50, mode='append')),
            ])),
            ('segmenting_pip2', Pipeline([
                ('B_features', B_features(),
                ('segmentation', segmentation())
                ('append', append_split_3D(max_len=50, mode='append')),
            ])),
            ('segmenting_pip3', Pipeline([
                ('Z_features', Z_features()),
                ('segmentation', segmentation())
                ('append', append_split_3D(max_len=50, mode='append')),
            ])),

        ])),
        ('split', append_split_3D(segments_number=10, mode='split')),
    ])

我在这个转换器中所做的如下: 例如,特征 ABZ 我有 return 以下数组:

  • A: (# of records, 10, 20)
  • B: (# of records, 10, 20)
  • Z: (# of records, 10, 15)

mode='append' 中,我附加了所有具有最大长度值 50 的额外固定值的数组(作为示例)以具有相同的 axis=2 暗淡并允许函数 Xs = np.hstack(Xs) 起作用。

因此,管道将 return 一个数组:(# of records, 30, 50)

然后,在 mode=split' 中,我将其添加到管道的末尾,我将最终数组拆分为它们附加的形状:(# of records, 30, 50) 到 3 个 dim [=25= 特征数组]

然后我删除额外的固定值,并在最后一个 dim 上应用连接。

最终数组的dim为:(# of records, 10, 55)。 55 是数组的第 3 维 (20+20+15) 的串联,这就是我想要的。