组合两个numpy数组

问题描述 投票:0回答:1

我有两个numpy数组。

一个是形状 (753,8,1) 表示客户的8个连续动作和其他形状的动作。(753,10) 表示一个训练样本的10个特征。

如何将这两个组合起来,使。

所有的10个特征都被附加到一个训练样本的8个连续动作上,也就是说,组合后的最终数组的形状应该是 (753,8,11).

python pandas numpy
1个回答
0
投票

也许是这样的。

import numpy as np

# create dummy arrays
a = np.zeros((753, 8, 1))
b = np.arange(753*10).reshape(753, 10)

# make a new axis for b and repeat the values along axis 1
c = np.repeat(b[:, np.newaxis, :], 8, axis=1)
c.shape
>>> (753, 8, 10) 

# now the first two axes of a and c have the same shape
# append the values in c to a along the last axis
result = np.append(a, c, axis=2)

result.shape
>>> (753, 8, 11)

result[0]
>>> array([[0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.],
           [0., 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]])

# values from b (0-9) have been appended to a (0)
© www.soinside.com 2019 - 2024. All rights reserved.