有没有一种方法可以将multi_index转换为int字符串/列表/数组?

时间:2019-05-23 21:08:04

标签: python numpy multidimensional-array array-indexing

我想将numpy中给定数组的值更改为该数组其他元素的乘积。因此,我想提取multi_index并对其进行操作,以便可以确定位置并使用它。 (例如nditer遍历所有元素,并始终执行“数组中的当前位置=数组中的下一个位置+数组中的上方位置”

我试图用当前位置的multi_index调用一个函数,并希望该函数接受它,例如将其增加一个位置。 (<0,1> ---> <0,2>而<0,n> n> =长度,否则<0,1> ---> <1,0>)

import numpy as np;

def fill(multi_index):
    "This will get the new value of the current iteration value judgeing from its index"
    return a[(multi_index + <0,1>) + (multi_index + <0,-1>) + (multi_index + <1,0>) + (multi_index + <-1,0>)]

#a = np.random.uniform(0, 100, size=(100, 100))
a = np.arange(6).reshape(2,3)

it = np.nditer(a, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
    it[0] = fill(it.multi_index)
    print(it[0])
    it.iternext()

"""for x in np.nditer(a, flags=['multi_index'], op_flags=['readwrite']):

    print(x)"""

我不明白如何从multi_index中提取实际的“坐标”。我是python的新手,因此请尽量解释清楚。谢谢。

编辑:在我仅使用C ++和一点Java进行编码之前,所以我以前主要使用数组(在c ++中,它可能是这样的:

int main() { 
  int a[100][100];
  for (int i=1, i<=a.length-1, i++) { 
    for (int j=1, i<=a.width-1, j++) { 
      a[i][j] = 1/4 (a[i][j+1]+a[i][j-1]+a[i+1][j]+a[i-1][j]);
    } 
  } 
return 0;
}

1 个答案:

答案 0 :(得分:0)

In [152]: a = np.arange(6).reshape(2,3)                                                                  
In [153]: a                                                                                              
Out[153]: 
array([[0, 1, 2],
       [3, 4, 5]])

让我们运行您的nditer并查看其值:

In [157]: it = np.nditer(a, flags=['multi_index'], op_flags=['readwrite'])                               
In [158]: while not it.finished: 
     ...:     print(it.multi_index, a[it.multi_index], it[0], type(it[0])) 
     ...:     it.iternext() 
     ...:                                                                                                
(0, 0) 0 0 <class 'numpy.ndarray'>
(0, 1) 1 1 <class 'numpy.ndarray'>
(0, 2) 2 2 <class 'numpy.ndarray'>
(1, 0) 3 3 <class 'numpy.ndarray'>
(1, 1) 4 4 <class 'numpy.ndarray'>
(1, 2) 5 5 <class 'numpy.ndarray'>

在每次迭代中,multiindexi,j索引的元组。 a[it.multiindex]然后从数组中选择该项目。但是it[0]也是那个项目,但包装为0d数组。如果您不满意0d数组(形状())的想法,那么nditer并不是您的工具(当前)。

如果只需要顺序索引元组,则ndindex也可以工作:

In [162]: list(np.ndindex(a.shape))                                                                      
Out[162]: [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]

(实际上,np.lib.index_tricks.py表明ndindex使用nditer多索引。nditernumpy Python级代码中并不常用。)< / p>

或者获取索引加值:

In [177]: list(np.ndenumerate(a))                                                                        
Out[177]: [((0, 0), 0), ((0, 1), 1), ((0, 2), 2), ((1, 0), 3), ((1, 1), 4), ((1, 2), 5)]

只是固定值的值:

In [178]: a.ravel()                                                                                      
Out[178]: array([0, 1, 2, 3, 4, 5])

但是,在numpy中,我们完全不希望迭代。相反,我们尝试使用快速编译的numpy方法编写适用于整个数组的代码。数组上的迭代速度慢,比列表上的迭代速度慢。

===

从某种程度上讲,您的迭代看起来是:

for i in range(n):
    for j in range(m):
         a[i,j] = ( a[i,j+1] + a[i,j-1] + a[i+1,j] + a[i-1,j] )/4 

有一些细节值得担心。那么j+/-1超出范围的边缘呢?并且此计算是连续的,因此a[i,j]取决于刚刚对a[i,j-1]所做的更改;还是被缓冲了?

一般来说,像这样的数组上的顺序计算是不适合numpy的。

另一方面,使用整个数组切片可以很好地完成缓冲计算

x[1:-1, 1:-1] = (x[:,:-1]+x[:,1:]+x[:-1,:]+x[1:,:])/4

scipy中,还有一些卷积函数可以在移动的窗口上执行计算。

相关问题