绑定一个numpy数组 [英] Binning a numpy array

查看:65
本文介绍了绑定一个numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a numpy array which contains time series data. I want to bin that array into equal partitions of a given length (it is fine to drop the last partition if it is not the same size) and then calculate the mean of each of those bins.

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

I suspect there is numpy, scipy, or pandas functionality to do this.

示例:

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

bin大小为2:

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

对于3的垃圾箱大小

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

推荐答案

只需使用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:

More generally, we'd need to do something like this to drop the last bin when it's not an even multiple:

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

这篇关于绑定一个numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆