如何在Python中创建4d数组

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

[协助我创建4D数组。下面是我的3维数组的代码。如何获得形状为(4,3,2,3)

的输出
Three_d_array - numpy.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]])

上面的代码提供了(3,2,3)形状的输出,但是我如何获得该形状的4d

python
1个回答
1
投票
import numpy as np    
a = np.arange(72)
a = np.reshape(a, (4,3,2,3))

或:

a = np.array([[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]], [[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]])

或:

a = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]])
a = np.expand_dims(a, axis=0)
a = np.repeat(a, 4, axis=0)
© www.soinside.com 2019 - 2024. All rights reserved.