像部分/选择性np.ndenumerate之类的东西? [英] something like partial/selective np.ndenumerate?

查看:109
本文介绍了像部分/选择性np.ndenumerate之类的东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读此帖子并怀疑我需要更好地了解索引.我想做类似np.ndenumerate的操作,但是(在这种情况下)仅在前三个维度上执行,并返回一系列向量:

I have read this post and suspect I need to understand indexing better. I would like to do something like np.ndenumerate, but (in this particular case) only over the first three dimensions, returning a series of vectors:

x = np.linspace(0., 1., 4)
y = np.linspace(0., 1., 5)
z = np.linspace(0., 1., 2)
X, Y, Z = np.meshgrid(x, y, z)   # define a grid
F = np.zeros(X.shape + (3,))     # a 3D vector field 
F = np.random.rand(5*4*2*3).reshape(5,4,2,3)   # added this later just to give non-zero for testing

thetas = np.linspace(0, 2.*np.pi, 21)[:-1]    # drop the duplicate
v = np.array([(np.cos(theta), np.sin(theta), 0.0) for theta in thetas])

for tup, vec in magical_enumerate(F, axis=(0,1,2)):  # it's magic! (need correct expression)
    F(tup) = np.cross(v, vec).sum(axis=0)     # sum over 20 vectors in v

有没有一种方法可以在没有大量循环或列表解释的情况下进行操作?网格将很大,因此请欣赏numpythony和速度.非顺序尺寸(例如,轴=(0,2))怎么办?谢谢!

Is there a way to do this without lots of loops or list interpretations? The grids will be large, so numpythony and speed are appreciated. What about non-sequential dimensions (e.g. axis=(0,2))? Thanks!

推荐答案

np.ndindex可能会解决问题.它会在一组维度上生成迭代器.

np.ndindex might do the trick. It generates an iterator over a set of dimensions.

In [231]: F=np.zeros((2,3,3,3))

In [232]: for tup in np.ndindex(F.shape[:3]):
    # vec = data[tup] etc
    F[tup]=tup
   .....:     

In [233]: F
Out[233]: 
array([[[[ 0.,  0.,  0.],
         [ 0.,  0.,  1.],
         [ 0.,  0.,  2.]],

         ...
         [ 1.,  2.,  1.],
         [ 1.,  2.,  2.]]]])

我建议同时查看ndenumeratendindex的代码. ndindexmulti_index模式下使用更新的nditer. ndenumerate使用flat迭代所有值.

I'd suggest looking at the code for both ndenumerate and ndindex. ndindex is using the more recent nditer in a multi_index mode. ndenumerate uses flat to iterate over all values.

我在其他SO中建议了如何构建以ndindex为模型的自己的multi_index迭代器.在nditer上进行搜索可能会产生这些结果.

I've suggested in other SO how you could construct your own multi_index iterator modeled on ndindex. A search on nditer might produce those.

这不会在多个循环上带来速度优势,因为您仍在处理相同数量的内部大多数计算.

This is not going to give a speed advantage over multiple loops, because you still are dealing with the same number of inner most calculations.

对于非顺序维,同样的ndindex也可以使用,但是必须先操作tup才能将其用作索引:

As for non-sequential dimensions, this same ndindex will work, but you have to manipulate tup before using it as an index:

In [243]: for tup in np.ndindex((F.shape[0],F.shape[2])):
    tup1=(tup[0],slice(None),tup[1])
    F[tup]=[tup[0],np.nan,tup[1]]
   .....:     

np.apply_along_axisnp.apply_over_axes是在分割其他轴时在一个或多个轴上生成索引的其他示例.

np.apply_along_axis and np.apply_over_axes are other examples of generating indices over 1 or more of the axes, while slicing others.

这篇关于像部分/选择性np.ndenumerate之类的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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