简体   繁体   English

如何使用python将所有索引值的总和加到numpy ndarray的每个元素中?

[英]How can I add to each element of a numpy ndarray the sum of all its index values with python ?

I have a numpy ndarray, let's take an example (but it can be totally different): 我有一个numpy ndarray,让我们举个例子(但可以完全不同):

[[[0 0 0]
  [1 1 1]
  [0 0 0]]

 [[1 0 1]
  [1 0 1]
  [1 0 1]]]

 [[1 0 0]
  [1 1 0]
  [1 1 1]]]

I want to add to each element the sum of its indexes in all dimensions. 我想将每个维度的索引总和添加到每个元素。 The result here would be: 结果将是:

[[[ 0  1  2]
  [ 4  5  6]
  [ 6  7  8]]

 [[10 10 12]
  [13 13 15]
  [16 16 18]]

 [[19 19 20]
  [22 23 23]
  [25 26 27]]]

To do so, I built another ndarray: 为此,我构建了另一个ndarray:

shp = a.shape
b = np.arange(shp[0]**len(shp)).reshape(shp)

And I got my result: 我得到了我的结果:

result = a+b

I would like to know if there is a more direct solution, which wouldn't need the creation of this second ndarray, a way to do the same operation 'on location' ?? 我想知道是否有一个更直接的解决方案,它不需要创建第二个ndarray,一种在“位置”上执行相同操作的方法?

Simply create open grids, that are basically 1D arrays extended to more dims and add into input array leveraging broadcasting - 只需创建开放式网格即可,基本上是一维阵列,扩展到更多的暗点,然后利用broadcasting将其添加到输入阵列中-

m,n,r = a.shape
I,J,K = np.ogrid[:m,:n,:r]
out = a + I*n*r + J*r + K

Hence, in terms of memory occupancy, we are creating only 9 (=m+n+r) more elements as opposed to 27 (= m * n * r) elements with the range-based soltuion. 因此,就内存占用而言,与range-based相比,我们只创建了9(= m + n + r)个元素,而不是27(= m * n * r)个元素。

Sample run - 样品运行-

In [41]: a
Out[41]: 
array([[[0, 0, 0],
        [1, 1, 1],
        [0, 0, 0]],

       [[1, 0, 1],
        [1, 0, 1],
        [1, 0, 1]],

       [[1, 0, 0],
        [1, 1, 0],
        [1, 1, 1]]])

In [42]: m,n,r = a.shape

In [43]: I,J,K = np.ogrid[:m,:n,:r]

In [44]: a + I*n*r + J*r + K
Out[44]: 
array([[[ 0,  1,  2],
        [ 4,  5,  6],
        [ 6,  7,  8]],

       [[10, 10, 12],
        [13, 13, 15],
        [16, 16, 18]],

       [[19, 19, 20],
        [22, 23, 23],
        [25, 26, 27]]])

N-dim array case N维阵列盒

For a generic n-dim array, a to add into itself - 对于通用n维数组,将a添加到自身中-

shp = a.shape
grid = np.ogrid[tuple(map(slice, shp))]
scale = np.r_[np.array(shp)[::-1].cumprod()[::-1][1:],1]
for i,j in zip(grid,scale):
    a += i*j

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除 numpy 数组中的元素并在 python 及其索引中使用 for 循环 - how can i delete an element in numpy array and use a for loop in python and its index 使用numpy,如何生成一个数组,其中每个索引的值是从0到第二个数组中相同索引的值的总和? - Using numpy, how can I generate an array where the value at each index is the sum of the values from 0 to that same index in a second array? 如何获得ndarray的x和y维度 - Numpy / Python - How can I get the x and y dimensions of a ndarray - Numpy / Python 如何在python中使用numpy在索引处添加元素 - How to add an element at the index using numpy in python 我如何总结所有值取决于熊猫中的索引值? - how can i sum all values depend on index value in pandas? 我如何总结字典中具有相同索引的所有值,其中每个键都有一个嵌套列表作为值? - How can i sum up all values with the same index in a dictionary which each key has a nested list as a value? python在每个索引处总结数组中的所有先前值 - python sum all previous values in array at each index python 将数组中每个索引处的所有下一个 n 值相加 - python sum all next n values in array at each index 我可以强迫一个numpy ndarray获取其内存的所有权吗? - Can I force a numpy ndarray to take ownership of its memory? 将新列添加到倒置二进制值的 ndarray | NumPy 阵列 Python - Add new column to ndarray of inverted binary values | NumPy Array Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM