我有以下格式的numpy数组
Show
如何在第二维中复制第一个尺寸并将其转换为以下3D数组?
(1440, 40)
答案 0 :(得分:1)
x是np.array:
print(x.shape == (1440, 40)) #True
expected_output = np.repeat(x[:, :, np.newaxis], 40, axis=2)
print(expected_output.shape == (1440, 40, 40)) #True
答案 1 :(得分:1)
您可以创建具有所需尺寸的新数组,然后根据需要复制数据。
类似这样的东西:
import numpy as np
a = np.array([[1, 2, 3], [1, 2, 3]])
b = np.zeros((a.shape[0], a.shape[0], a.shape[1]))
for i in range(a.shape[0]):
b[i] = a[i]
print(a.shape) # (2,3)
print(b.shape) # (2,2,3)
######Sample Output########
[[1 2 3]
[1 2 3]] #a
[[[1. 2. 3.]
[1. 2. 3.]]
[[1. 2. 3.]
[1. 2. 3.]]] #b
我不确定复制数据的真正含义。我希望这可以解决您的疑问。
答案 2 :(得分:1)
如果只想将2d数组平铺到3d数组中,则可以使用numpy.tile命令:
>>> import numpy as np
>>> x = np.array([[1, 2, 3], [4, 5, 6]])
>>> print(x.shape)
(2, 3)
>>> print(x)
[[1 2 3]
[4 5 6]]
>>> x_3d = np.tile(x, (2, 1, 1))
>>> print(x_3d.shape)
(2, 2, 3)
>>> print(x_3d)
[[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]]