numpy的数组[? 5 5]至五个阵列,[? 5] [英] Numpy array [? 5 5] to five arrays with [? 5]

查看:113
本文介绍了numpy的数组[? 5 5]至五个阵列,[? 5]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个[? 5 5]数组:(?= 3在这种情况下)

I have a [? 5 5] array: (?=3 in this case)

[[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
[[25 26 27 28 29]
 [30 31 32 33 34]
 [35 36 37 38 39]
 [40 41 42 43 44]
 [45 46 47 48 49]]
[[50 51 52 53 54]
 [55 56 57 58 59]
 [60 61 62 63 64]
 [65 66 67 68 69]
 [70 71 72 73 74]]]

我想有5(行#)独立阵列有这样(5):

I want to have 5 (the # of row) separate array have (? 5) like this:

[array([[ 0,  1,  2,  3,  4],
   [25, 26, 27, 28, 29],
   [50, 51, 52, 53, 54]]), 
 array([[ 5,  6,  7,  8,  9],
   [30, 31, 32, 33, 34],
   [55, 56, 57, 58, 59]]), 
 array([[10, 11, 12, 13, 14],
   [35, 36, 37, 38, 39],
   [60, 61, 62, 63, 64]]), 
 array([[15, 16, 17, 18, 19],
   [40, 41, 42, 43, 44],
   [65, 66, 67, 68, 69]]), 
 array([[20, 21, 22, 23, 24],
   [45, 46, 47, 48, 49],
   [70, 71, 72, 73, 74]])]

这是一个简单的preferably一/二numpy的操作方式做到这一点?

Is this a simple preferably one/two numpy operation way to do this?

推荐答案

是的,有: numpy.transpose 。你能选择在您的3D结构的轴的任何序列。

Yes, there is : numpy.transpose. You get to chose any sequence in the axes of your 3D structure.

import numpy as np

aaa=np.array([[[ 0 , 1,  2,  3,  4],
 [ 5,  6,  7,  8,  9],
 [10, 11, 12, 13, 14],
 [15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24]],
[[25, 26, 27, 28, 29],
 [30, 31, 32, 33, 34],
 [35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44],
 [45, 46, 47, 48, 49]],
[[50, 51, 52, 53, 54],
 [55, 56, 57, 58, 59],
 [60, 61, 62, 63, 64],
 [65, 66, 67, 68, 69],
 [70, 71, 72, 73, 74]]])

bb= np.transpose(aaa,axes=[1,0,2])
print bb

输出:

[[[ 0  1  2  3  4]
  [25 26 27 28 29]
  [50 51 52 53 54]]

 [[ 5  6  7  8  9]
  [30 31 32 33 34]
  [55 56 57 58 59]]

 [[10 11 12 13 14]
  [35 36 37 38 39]
  [60 61 62 63 64]]

 [[15 16 17 18 19]
  [40 41 42 43 44]
  [65 66 67 68 69]]

 [[20 21 22 23 24]
  [45 46 47 48 49]
  [70 71 72 73 74]]].

要访问子阵列,只需使用索引像这样:

To access the subarrays, just use indexing like so:

 c= b[0]
 print c

输出:

 [[ 0  1  2  3  4]
 [25 26 27 28 29]
 [50 51 52 53 54]]

这篇关于numpy的数组[? 5 5]至五个阵列,[? 5]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆