Normalize an array of two-dimensional numbers: zero unit spread

I have a 2D Numpy array in which I want to normalize each column to zero mean and a variance of one. Since I'm mainly used for C ++, the method I'm doing is to use loops to iterate over the elements in a column and perform the necessary operations, and then repeat this for all columns. I wanted to know about the pythonic way of doing this.

Let be class_input_datamy 2D array. I can get the column value as:

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

Then I subtract the average from all the columns:

class_input_data = class_input_data - column_mean

Currently, the data must be null. However, the meaning:

numpy.sum(class_input_data, axis = 0)

0, , - . By 0, , .

+4
1

- :

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.])
+8

All Articles