Home > database >  How to create a function that loops through numpy matrix to z-scale each and every data points retur
How to create a function that loops through numpy matrix to z-scale each and every data points retur

Time:11-12

How to create a function that loops through numpy matrix to z-scale each and every data points returning the data standardized. Just like how sklearn.preprocessing.StandardScaler does it. I have got up to here with no success. May somebody help me with this?

def stand_scaler(data):
    mean = np.mean(data, axis=0)
    std = np.std(data, axis=0)
    for i in range(len(data)):
        data[i] = (data[i] - mean)/std
        return data

stand_scaler(data)

CodePudding user response:

You shouldn't need a for-loop for this; numpy's array operations are intended for exactly this case. For a one dimensional array it's straightforward:

In [1]: import numpy as np

In [2]: x = np.random.normal(size=10)

In [3]: nx = (x - x.mean()) / x.std()

In [4]: x
Out[4]: 
array([ 0.52700345, -0.57358563, -0.16925383,  2.14401554,  1.05223331,
        0.72659482,  1.06816826,  0.31194848,  0.04004589,  1.09046925])

In [5]: nx
Out[5]: 
array([-0.12859083, -1.62209992, -1.0734181 ,  2.06570881,  0.58415071,
        0.14225641,  0.60577458, -0.42042233, -0.78939654,  0.63603721])

In [6]: nx.mean()
Out[6]: 5.551115123125783e-17

In [7]: nx.std()
Out[7]: 1.0000000000000002

For higher dimensions, you can choose an axis to work over, and scale by using numpy's broadcasting; e.g., in this case, imagine each column is a different variable:

In [8]: y = np.array([10,1]) * np.random.normal(size=(5,2)) - np.array([5,-10])

In [9]: ny = (y - y.mean(axis=0)) / y.std(axis=0)

In [10]: ny
Out[10]: 
array([[ 0.78076062, -0.26971997],
       [-1.59591909, -1.2409338 ],
       [-0.55740483, -0.81901609],
       [ 1.22978416,  1.12697814],
       [ 0.14277914,  1.20269171]])

In [11]: ny.mean(axis=0), ny.std(axis=0)
Out[11]: (array([-3.33066907e-17,  8.43769499e-16]), array([1., 1.]))
  • Related