1# Standardization
2
3from sklearn import preprocessing
4import numpy as np
5X_train = np.array([[1., -1., 2.],
6 [2., 0., 0.],
7 [0., 1., -1.]])
8X_scaled = preprocessing.scale(X_train)
9
10X_scaled
11# array([[ 0. ..., -1.22..., 1.33...],
12# [ 1.22..., 0. ..., -0.26...],
13# [-1.22..., 1.22..., -1.06...]])
14
15# Scaled data has zero mean and unit variance:
16
17X_scaled.mea(axis=0)
18# array([0., 0., 0.])
19X_scaled.std(axis=0)
20# array([1., 1., 1.])
21
22scaler = preprocessing.StandardScaler().fit(X_train)
23scaler
24# StandardScaler()
25
26scaler.mean_
27# array([1. ..., 0. ..., 0.33...])
28
29scaler.scale_
30# array([0.81..., 0.81..., 1.24...])
31
32scaler.transform(X_train)
33array([[ 0. ..., -1.22..., 1.33...],
34 [ 1.22..., 0. ..., -0.26...],
35 [-1.22..., 1.22..., -1.06...]])
36
37X_test = [[-1., 1., 0.]]
38scaler.transform(X_test)
39# array([[-2.44..., 1.22..., -0.26...]])