如何使用numpy计算不规则形状的数组的均值和标准差 [英] How to use numpy to calculate mean and standard deviation of an irregular shaped array

查看:530
本文介绍了如何使用numpy计算不规则形状的数组的均值和标准差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy数组,其中有许多不同长度的样本

I have a numpy array that has many samples in it of varying length

Samples = np.array([[1001, 1002, 1003],
                    ... ,
                    [1001, 1002]])

我想(基本)减去数组的平均值,然后除以数组的标准偏差.像这样:

I want to (elementwise) subtract the mean of the array then divide by the standard deviation of the array. Something like:

newSamples = (Samples-np.mean(Samples))/np.std(Samples)

除了不适用于不规则形状的数组,

Except that doesn't work for irregular shaped arrays,

np.mean(示例)原因

np.mean(Samples) causes

unsupported operand type(s) for /: 'list' and 'int'

由于我假设它已经为每个轴设置了静态大小,然后当遇到不同大小的样本时它无法处理它.用numpy解决此问题的方法是什么?

due to what I assume to be it having set a static size for each axis and then when it encounters a different sized sample it can't handle it. What is an approach to solve this using numpy?

示例输入:

Sample = np.array([[1, 2, 3],
                   [1, 2]])

减去平均值然后除以标准偏差:

After subtracting by the mean and then dividing by standard deviation:

Sample = array([[-1.06904497,  0.26726124,  1.60356745], 
                [-1.06904497,  0.26726124]])

推荐答案

@MichaelHackman(带注释). 这很奇怪,因为当我计算总体std并平均然后应用它时,我得到了不同的结果(请参见下面的代码).

@MichaelHackman (following the comment remark). That's weird because when I compute the overall std and mean then apply it, I obtain different result (see code below).

import numpy as np

Samples = np.array([[1, 2, 3],
                   [1, 2]])
c = np.hstack(Samples)  # Will gives [1,2,3,1,2]
mean, std = np.mean(c), np.std(c)
newSamples = np.asarray([(np.array(xi)-mean)/std for xi in Samples])
print newSamples
# [array([-1.06904497,  0.26726124,  1.60356745]), array([-1.06904497,  0.26726124])]

edit :添加np.asarray(),将mean,std计算放在循环之外,紧随Imanol Luengo的精彩评论(谢谢!)

edit: Add np.asarray(), put mean,std computation outside loop following Imanol Luengo's excellent comments (Thanks!)

这篇关于如何使用numpy计算不规则形状的数组的均值和标准差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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