归一化二维Numpy数组:零均值单位方差 [英] Normalise 2D Numpy Array: Zero Mean Unit Variance

查看:358
本文介绍了归一化二维Numpy数组:零均值单位方差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2D Numpy数组,我想在其中将每一列归一化为零均值和单位方差.由于我主要使用C ++,因此我正在执行的方法是使用循环对列中的元素进行迭代并执行必要的操作,然后对所有列重复此操作.我想知道一种使用Python的方法.

I have a 2D Numpy array, in which I want to normalise each column to zero mean and unit variance. Since I'm primarily used to C++, the method in which I'm doing is to use loops to iterate over elements in a column and do the necessary operations, followed by repeating this for all columns. I wanted to know about a pythonic way to do so.

class_input_data是我的2D数组.我可以将列均值表示为:

Let class_input_data be my 2D array. I can get the column mean as:

column_mean = numpy.sum(class_input_data, axis = 0)/class_input_data.shape[0]

然后我将所有列的均值减去:

I then subtract the mean from all columns by:

class_input_data = class_input_data - column_mean

现在,数据应为零均值.但是,值:

By now, the data should be zero mean. However, the value of:

numpy.sum(class_input_data, axis = 0)

不等于0,表示我在归一化过程中做错了什么. 不"等于0,并不表示可以归因于浮点误差的很小的数字.

isn't equal to 0, implying that I have done something wrong in my normalisation. By isn't equal to 0, I don't mean very small numbers which can be attributed to floating point inaccuracies.

推荐答案

类似的东西

import numpy as np

eg_array = 5 + (np.random.randn(10, 10) * 2)
normed = (eg_array - eg_array.mean(axis=0)) / eg_array.std(axis=0)

normed.mean(axis=0)
Out[14]: 
array([  1.16573418e-16,  -7.77156117e-17,  -1.77635684e-16,
         9.43689571e-17,  -2.22044605e-17,  -6.09234885e-16,
        -2.22044605e-16,  -4.44089210e-17,  -7.10542736e-16,
         4.21884749e-16])

normed.std(axis=0)
Out[15]: array([ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.])

这篇关于归一化二维Numpy数组:零均值单位方差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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