如何从numpy数组中提取所需的维度

时间:2013-12-13 22:20:52

标签: python numpy

鉴于我有一个三维[3,500,500]的numpy数组,如何将一维提取为[1,500,500]?

import numpy as np
my_array = np.ones((3,500,500),dtype=int,order='C')
print (my_array)

2 个答案:

答案 0 :(得分:2)

那些?

>>> my_array[0,:,:]
array([[1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       ...,
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1]])
>>> my_array[0,:,:].shape
(500, 500)

答案 1 :(得分:0)

也许你是说换位的:

my_array = np.random.rand(3,3)

[[0.03639883, 0.47393186, 0.25823457],
 [0.3732702 , 0.43692148, 0.13893397],
 [0.11873897, 0.71504391, 0.34635178]]

my_array.T[0]:
[0.03639883, 0.3732702, 0.11873897]
my_array.T[1]:
[0.47393186 , 0.43692148, 0.13893397]
my_array.T[2]:
[0.11873897, 0.71504391, 0.34635178]