简体   繁体   English

Python:正确迭代numpy数组的字典

[英]Python: properly iterating through a dictionary of numpy arrays

Given the following numpy arrays: 给定以下numpy数组:

import numpy
a=numpy.array([[1,1,1],[1,1,1],[1,1,1]])
b=numpy.array([[2,2,2],[2,2,2],[2,2,2]])
c=numpy.array([[3,3,3],[3,3,3],[3,3,3]])

and this dictionary containing them all: 这个字典包含了所有这些:

mydict={0:a,1:b,2:c}

What is the most efficient way of iterating through mydict so to compute the average numpy array that has (1+2+3)/3=2 as values? 迭代mydict以便计算具有(1+2+3)/3=2作为值的平均numpy数组的最有效方法是什么?

My attempt fails as I am giving it too many values to unpack. 我的尝试失败了,因为我给它提供了太多无法解包的值。 It is also extremely inefficient as it has an O(n^3) time complexity: 它的时间复杂度为O(n^3) ,因此效率非常低:

aver=numpy.empty([a.shape[0],a.shape[1]])

for c,v in mydict.values():
    for i in range(0,a.shape[0]):
        for j in range(0,a.shape[1]):
            aver[i][j]=mydict[c][i][j] #<-too many values to unpack

The final result should be: 最终结果应为:

In[17]: aver
Out[17]: 
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 2.,  2.,  2.]])

EDIT 编辑

I am not looking for an average value for each numpy array. 我不是在寻找每个numpy数组的平均值。 I am looking for an average value for each element of my colleciton of numpy arrays. 我正在寻找numpy数组的每个元素的平均值。 This is a minimal example, but the real thing I am working on has over 120,000 elements per array, and for the same position the values change from array to array. 这是一个最小的示例,但是我正在研究的真实事物每个数组具有超过120,000个元素,并且对于相同位置,值在数组之间变化。

I think you're making this harder than it needs to be. 我认为您正在使这项工作变得比需要做的难。 Either sum them and divide by the number of terms: 将它们相加然后除以术语数:

In [42]: v = mydict.values()

In [43]: sum(v) / len(v)
Out[43]: 
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 2.,  2.,  2.]])

Or stack them into one big array -- which it sounds like is the format they probably should have been in to start with -- and take the mean over the stacked axis: 或将它们堆叠成一个大数组-听起来像是它们可能应该从一开始就采用的格式-并在堆叠轴上取均值:

In [44]: np.array(list(v)).mean(axis=0)
Out[44]: 
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 2.,  2.,  2.]])

You really shouldn't be using a dict of numpy.array s. 您确实不应该使用numpy.arraydict Just use a multi-dimensional array: 只需使用多维数组:

>>> bigarray  = numpy.array([arr.tolist() for arr in mydict.values()])
>>> bigarray
array([[[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]],

       [[2, 2, 2],
        [2, 2, 2],
        [2, 2, 2]],

       [[3, 3, 3],
        [3, 3, 3],
        [3, 3, 3]]])
>>> bigarray.mean(axis=0)
array([[ 2.,  2.,  2.],
       [ 2.,  2.,  2.],
       [ 2.,  2.,  2.]])
>>>

You should modify your code to not even work with a dict . 您应该修改代码以甚至不能使用dict Especially not a dict with integer keys... 特别是不是带有整数键的dict ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM