从一个热编码列创建差异列

2024-04-20 14:16:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在数据集上创建一些额外的特性。我想从我已经有一个热编码的特征中得到一个空间上下文。举个例子,我有:

    F1    F2    F3    F4
1   0     1     1     0
2   1     0     1     1
3   1     0     0     0
4   0     0     0     1

我想根据此处的值创建一些新列:

    F1    F2    F3    F4    S1    S2    S3    S4
1   0     1     1     0     0     2     1     0
2   1     0     0     1     1     0     0     3
3   1     0     0     0     1     0     0     0
4   0     0     0     1     0     0     0     4

我希望有一种简单的方法可以做到这一点,从列的最后一个值计算更改并将其输出到相应的列。谢谢您的帮助


Tags: 数据编码s3空间特征特性例子s4
1条回答
网友
1楼 · 发布于 2024-04-20 14:16:34

你可以做:

def func(x):
    # create result array
    result = np.zeros(x.shape, dtype=np.int)

    # get indices of array distinct of zero
    w = np.argwhere(x).ravel()

    # compute the difference between consecutive indices and add the first index + 1
    array = np.hstack(([w[0] + 1], np.ediff1d(w)))

    # set the values on result
    np.put(result, w, array)

    return result


columns = ['S{}'.format(i) for i in range(1, 5)]
s = pd.DataFrame(df.ne(0).apply(func, axis=1).values.tolist(),
                 columns=columns)

result = pd.concat([df, s], axis=1)
print(result)

输出

   F1  F2  F3  F4  S1  S2  S3  S4
0   0   1   1   0   0   2   1   0
1   1   0   0   1   1   0   0   3
2   1   0   0   0   1   0   0   0
3   0   0   0   1   0   0   0   4

注意,您需要导入numpy(import numpy as np),以便func工作。其思想是找到不同于零的索引计算连续值之间的差异,将第一个值设置为index + 1,并对每一行执行此操作

相关问题 更多 >