【问题标题】:Decrease array size by averaging adjacent values with numpy通过使用 numpy 平均相邻值来减小数组大小
【发布时间】:2014-12-25 15:09:57
【问题描述】:

我在 numpy 中有大量数千个 val。我想通过平均相邻值来减小它的大小。 例如:

a = [2,3,4,8,9,10]
#average down to 2 values here
a = [3,9]
#it averaged 2,3,4 and 8,9,10 together

所以,基本上,我在数组中有 n 个元素,我想告诉它平均到 X 个值,并且它的平均值类似于上面。

有没有办法用 numpy 做到这一点(已经将它用于其他事情,所以我想坚持下去)。

【问题讨论】:

  • 我打算先提议reshape,然后再提议mean,但这与this question 接受的答案相同。这对你的目的有用吗?
  • 亲爱的亚当,我希望您发现下面给出的答案对您有所帮助。如果您发现它们有用,请接受给出的众多答案之一。 =)

标签: python arrays numpy mean


【解决方案1】:

看起来像一个简单的非重叠移动窗口平均值,怎么样:

In [3]:

import numpy as np
a = np.array([2,3,4,8,9,10])
window_sz = 3
a[:len(a)/window_sz*window_sz].reshape(-1,window_sz).mean(1) 
#you want to be sure your array can be reshaped properly, so the [:len(a)/window_sz*window_sz] part
Out[3]:
array([ 3.,  9.])

【讨论】:

    【解决方案2】:

    试试这个:

    n_averaged_elements = 3
    averaged_array = []
    a = np.array([ 2,  3,  4,  8,  9, 10])
    for i in range(0, len(a), n_averaged_elements):
       slice_from_index = i
       slice_to_index = slice_from_index + n_averaged_elements
       averaged_array.append(np.mean(a[slice_from_index:slice_to_index]))
    
    >>>> averaged_array
    >>>> [3.0, 9.0]
    

    【讨论】:

      【解决方案3】:

      使用reshapemean,您可以平均大小为N*m 的一维数组的每个m 相邻值,其中N 是任何正整数。例如:

      import numpy as np
      
      m = 3
      a = np.array([2, 3, 4, 8, 9, 10])
      b = a.reshape(-1, m).mean(axis=1)
      #array([3., 9.])
      

      1)a.reshape(-1, m) 将在不复制数据的情况下创建数组的 2D 图像:

      array([[ 2,  3,  4],
             [ 8,  9, 10]])
      

      2)取第二个轴的平均值(axis=1)然后计算每一行的平均值,得到array([3., 9.])

      【讨论】:

        【解决方案4】:

        在这个例子中,我假设 a 是需要平均的一维 numpy 数组。在我下面给出的方法中,我们首先找到这个数组长度的因素a。然后,我们选择一个合适的因子作为平均数组的步长。

        这里是代码。

        import numpy as np
        from functools import reduce
        
        ''' Function to find factors of a given number 'n' '''
        def factors(n):    
            return list(set(reduce(list.__add__, 
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
        
        a = [2,3,4,8,9,10]  #Given array.
        
        '''fac: list of factors of length of a. 
           In this example, len(a) = 6. So, fac = [1, 2, 3, 6] '''
        fac = factors(len(a)) 
        
        '''step: choose an appropriate step size from the list 'fac'.
           In this example, we choose one of the middle numbers in fac 
           (3). '''   
        step = fac[int( len(fac)/3 )+1]
        
        '''avg: initialize an empty array. '''
        avg = np.array([])
        for i in range(0, len(a), step):
            avg = np.append( avg, np.mean(a[i:i+step]) ) #append averaged values to `avg`
        
        print avg  #Prints the final result
        
        [3.0, 9.0]      
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-26
          • 2012-04-13
          • 1970-01-01
          • 1970-01-01
          • 2012-04-21
          相关资源
          最近更新 更多