使用NumPy数组执行分组的平均值和标准差 [英] Performing grouped average and standard deviation with NumPy arrays

查看:315
本文介绍了使用NumPy数组执行分组的平均值和标准差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组数据(X,Y).我的自变量值X不是唯一的,所以有多个重复的值,我想输出一个包含以下内容的新数组:X_unique,它是X的唯一值的列表.Y_mean,对应于X_unique的所有Y值的平均值. Y_std,对应于X_unique的所有Y值的标准偏差.

I have a set of data (X,Y). My independent variable values X are not unique, so there are multiple repeated values, I want to output a new array containing : X_unique, which is a list of unique values of X. Y_mean, the mean of all of the Y values corresponding to X_unique. Y_std, the standard deviation of all the Y values corresponding to X_unique.

x = data[:,0]
y = data[:,1]

推荐答案

x_unique  = np.unique(x)
y_means = np.array([np.mean(y[x==u]) for u in x_unique])
y_stds = np.array([np.std(y[x==u]) for u in x_unique])

这篇关于使用NumPy数组执行分组的平均值和标准差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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