sklearn in StandardScacler

StandardScaler role:

StandardScaler data set is normalized to do, he performed as a unit calculated on the basis that each feature

 

Calculation method:

(Original value - average value) / standard deviation

 

Code validation:

 

Call StandardScaler

import numpy as np
from sklearn.preprocessing import StandardScaler
np.random.seed(42)
samples = np.random.randn(10,1)
scaler = StandardScaler()
scaler.fit(samples)

Out:

StandardScaler(copy=True, with_mean=True, with_std=True)

Done manually

 

# 预测函数
def scale(series, x):
    mean = np.mean(series)
    std = np.std(series)
    return (x-mean)/std
 

verification

scale(samples[:,0], np.array([[1],[2]]))

Out:

array([[0.80468598],
       [2.26261185]])

 

scaler.transform(np.array([[1],[2]]))

Out:

array([[0.80468598],
       [2.26261185]])

in conclusion

Manual method with the same result, which is verified StandardScaler algorithm, which is calculated as:

\frac{X-mean(x)}{std(x)}

url?signature=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0eXBlIjoidmlldyIsImlhdCI6MTU4MzIwNjQxOCwiZmlsZUlkIjozNDg5NzM2LCJ0aW1lc3RhbXAiOjE1ODMyMDY0MTgzMTV9.XiyqNS4p05JUE8lNVjlLiwU3eZLzczB3l1Z7byQw9kUuploading.4e448015.gifDump failed to re-upload canceled url?signature=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0eXBlIjoidmlldyIsImlhdCI6MTU4MzIwNjQxOCwiZmlsZUlkIjozNDg5NzM2LCJ0aW1lc3RhbXAiOjE1ODMyMDY0MTgzMTV9.XiyqNS4p05JUE8lNVjlLiwU3eZLzczB3l1Z7byQw9kUuploading.4e448015.gifdump failed to re-upload canceled   

Published an original article · won praise 0 · Views 2

Guess you like

Origin blog.csdn.net/xuxingyuan/article/details/104629400