【问题标题】:Binning a numpy array合并一个 numpy 数组
【发布时间】:2014-03-22 04:26:56
【问题描述】:

我有一个包含时间序列数据的 numpy 数组。我想将该数组分成给定长度的相等分区(如果最后一个分区大小不同,则可以删除它),然后计算每个分区的平均值。

我怀疑有 numpy、scipy 或 pandas 功能可以做到这一点。

示例:

data = [4,2,5,6,7,5,4,3,5,7]

对于 2 的 bin 大小:

bin_data = [(4,2),(5,6),(7,5),(4,3),(5,7)]
bin_data_mean = [3,5.5,6,3.5,6]

对于大小为 3 的 bin:

bin_data = [(4,2,5),(6,7,5),(4,3,5)]
bin_data_mean = [7.67,6,4]

【问题讨论】:

标签: python arrays numpy pandas scipy


【解决方案1】:

使用标准 Python 试试这个(不需要 NumPy)。假设正在使用 Python 2.x:

data = [ 4, 2, 5, 6, 7, 5, 4, 3, 5, 7 ]

# example: for n == 2
n=2
partitions = [data[i:i+n] for i in xrange(0, len(data), n)]
partitions = partitions if len(partitions[-1]) == n else partitions[:-1]

# the above produces a list of lists
partitions
=> [[4, 2], [5, 6], [7, 5], [4, 3], [5, 7]]

# now the mean
[sum(x)/float(n) for x in partitions]
=> [3.0, 5.5, 6.0, 3.5, 6.0]

【讨论】:

  • 我同意 numpy 不是必需的,但这是同时使用 pandas 和 numpy 的大型机器的一小部分,因此它已经存储在 numpy 数组中。我也更喜欢保持简洁。
【解决方案2】:

由于您已经有一个 numpy 数组,为避免 for 循环,您可以使用 reshape 并将新维度视为 bin:

In [33]: data.reshape(2, -1)
Out[33]: 
array([[4, 2, 5, 6, 7],
       [5, 4, 3, 5, 7]])

In [34]: data.reshape(2, -1).mean(0)
Out[34]: array([ 4.5,  3. ,  4. ,  5.5,  7. ])

实际上,如果data 的大小可以被n 整除,这将起作用。我将编辑一个修复程序。

看起来像 Joe Kington has an answer 可以处理这个问题。

【讨论】:

    【解决方案3】:

    只需使用reshape,然后使用mean(axis=1)

    作为最简单的例子:

    import numpy as np
    
    data = np.array([4,2,5,6,7,5,4,3,5,7])
    
    print data.reshape(-1, 2).mean(axis=1)
    

    更一般地说,当最后一个 bin 不是偶数时,我们需要做这样的事情来删除它:

    import numpy as np
    
    width=3
    data = np.array([4,2,5,6,7,5,4,3,5,7])
    
    result = data[:(data.size // width) * width].reshape(-1, width).mean(axis=1)
    
    print result
    

    【讨论】:

      【解决方案4】:

      我刚刚编写了一个函数来将其应用于您想要的所有数组大小或维度。

      • 数据是你的数组
      • 是你想成为的轴
      • binstep 是每个 bin 之间的点数(允许重叠 bin)
      • binsize 是每个 bin 的大小
      • func 是您要应用于 bin 的函数(np.max 表示最大池化,np.mean 表示平均...)

        def binArray(data, axis, binstep, binsize, func=np.nanmean):
            data = np.array(data)
            dims = np.array(data.shape)
            argdims = np.arange(data.ndim)
            argdims[0], argdims[axis]= argdims[axis], argdims[0]
            data = data.transpose(argdims)
            data = [func(np.take(data,np.arange(int(i*binstep),int(i*binstep+binsize)),0),0) for i in np.arange(dims[axis]//binstep)]
            data = np.array(data).transpose(argdims)
            return data
        

      在你的情况下,它将是:

      data = [4,2,5,6,7,5,4,3,5,7]
      bin_data_mean = binArray(data, 0, 2, 2, np.mean)
      

      或者对于 3 的 bin 大小:

      bin_data_mean = binArray(data, 0, 3, 3, np.mean)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-19
        • 1970-01-01
        • 1970-01-01
        • 2021-09-05
        • 1970-01-01
        • 2020-01-03
        • 2021-04-26
        • 1970-01-01
        相关资源
        最近更新 更多