我使用以下代码创建数组的索引列表。但是,我希望索引以Fortran顺序运行,即内循环是更快变化的循环。有没有办法在python中实现这一点。目前,我得到的输出是C顺序。
np.transpose(np.nonzero(np.ones([32,30])))
输出:
array([[ 0, 0],
[ 0, 1],
[ 0, 2],
...,
[31, 27],
[31, 28],
[31, 29]])
但是,我需要以下列形式的ouptut:
array([[ 0, 0],
[ 1, 0],
[ 2, 0],
...,
[29, 29],
[30, 29],
[31, 29]])
答案 0 :(得分:5)
您可以使用np.indices
生成这些索引,然后进行转置并重塑这项工作 -
np.indices((32,30)).T.reshape(-1,2)
示例输出 -
In [36]: np.indices((32,30)).T.reshape(-1,2)
Out[36]:
array([[ 0, 0],
[ 1, 0],
[ 2, 0],
...,
[29, 29],
[30, 29],
[31, 29]])
运行时测试 -
In [74]: points = [32,30]
# @218's soln
In [75]: %timeit np.transpose(np.nonzero(np.ones(points[::-1])))[:,::-1]
100000 loops, best of 3: 18.6 µs per loop
In [76]: %timeit np.indices((points)).T.reshape(-1,2)
100000 loops, best of 3: 16.1 µs per loop
In [77]: points = [320,300]
# @218's soln
In [78]: %timeit np.transpose(np.nonzero(np.ones(points[::-1])))[:,::-1]
100 loops, best of 3: 2.14 ms per loop
In [79]: %timeit np.indices((points)).T.reshape(-1,2)
1000 loops, best of 3: 1.26 ms per loop
进一步提升绩效
我们可以使用points
翻转np.indices
,然后使用np.column_stack
创建最终的2
列数组,进一步优化它。让我们来反对已经提出的问题进行验证。列出以下两种方法 -
def app1(points):
return np.indices((points)).T.reshape(-1,2)
def app2(points):
R,C = np.indices((points[::-1]))
return np.column_stack((C.ravel(), R.ravel()))
计时 -
In [146]: points = [32,30]
In [147]: np.allclose(app1(points), app2(points))
Out[147]: True
In [148]: %timeit app1(points)
100000 loops, best of 3: 14.8 µs per loop
In [149]: %timeit app2(points)
100000 loops, best of 3: 17.4 µs per loop
In [150]: points = [320,300]
In [151]: %timeit app1(points)
1000 loops, best of 3: 1.1 ms per loop
In [152]: %timeit app2(points)
1000 loops, best of 3: 822 µs per loop
所以,对于更大的形状,这个更好。
答案 1 :(得分:2)
一般解决方案(适用于更高维度指数,例如3D,4D)等似乎与Nils Werner的评论中所建议的一样:
this.state.roundshistories
输出:
points = [32,30]
np.transpose(np.nonzero(np.ones(points[::-1])))[:,::-1]
答案 2 :(得分:1)
这就是你想要的吗?
a = np.transpose(np.nonzero(np.ones([32,30])))
a.reshape(32,30,2).transpose(1,0,2).reshape(-1,2)
Out[2197]:
array([[ 0, 0],
[ 1, 0],
[ 2, 0],
...,
[29, 29],
[30, 29],
[31, 29]], dtype=int64)