如何通过numpy的数组的初始迭代维度? [英] How to iterate over initial dimensions of a Numpy array?

查看:355
本文介绍了如何通过numpy的数组的初始迭代维度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy的阵列形状 [1000,1000,1000,3] ,是最后的尺寸,大小3,是包含三维空间向量部件的三胞胎。如何使用 nditer 来遍历每个三重?像这样的:

 在np.nditer VEC(my_array,op_flags = ['只写'< ???>]):
     VEC = np.array(东西)


解决方案

我以前就解决了这个问题,但这里有一个简单的例子:

  VEC = np.arange(2 * 2 * 2 * 3).reshape(2,2,2,3)
它= np.ndindex(2,2,2)
因为我在里面:
    打印(VEC [I])

生产

  [0 1 2]
[3 4 5]
[6 7 8]
[9 10 11]
[12 13 14]
[15 16 17]
[18 19 20]
[21 22 23]

ndindex 构建围绕大小的虚拟阵列多指数迭代器,你给它(这里(2,2,2)),并返回一个接下来方法一起。

所以,你可以使用 ndindex 原样,或者用它作为一种模式为在 nditer 。<构造你/ p>

I have a Numpy array with shape [1000, 1000, 1000, 3], being the last dimension, sized 3, is contains the triplets of 3D spatial vectors components. How can I use nditer to iterate over each triplet? Like this:

 for vec in np.nditer(my_array, op_flags=['writeonly', <???>]):
     vec = np.array(something)

解决方案

I've addressed this question before, but here's a short example:

vec=np.arange(2*2*2*3).reshape(2,2,2,3)
it=np.ndindex(2,2,2)
for i in it:
    print(vec[i])

producing:

[0 1 2]
[3 4 5]
[6 7 8]
[ 9 10 11]
[12 13 14]
[15 16 17]
[18 19 20]
[21 22 23]

ndindex constructs a multi-index iterator around a dummy array of the size you give it (here (2,2,2)), and returns it along with a next method.

So you can use ndindex as is, or use it as a model for constructing your on nditer.

这篇关于如何通过numpy的数组的初始迭代维度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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