Week 6, Wed, 5/7#
import pandas as pd
from sklearn.preprocessing import StandardScaler
# Create a toy DataFrame
df = pd.DataFrame({
'x1': [10, 20, 30, 40],
'x2': [1, 2, 3, 4]
})
df2 = pd.DataFrame({
'x1': [50, 60, 70, 80],
'x2': [5, 6, 7, 8]
})
scaler = StandardScaler()
scaler.fit_transform(df)
array([[-1.34164079, -1.34164079],
[-0.4472136 , -0.4472136 ],
[ 0.4472136 , 0.4472136 ],
[ 1.34164079, 1.34164079]])
scaler2 = StandardScaler()
scaler2.fit(df)
StandardScaler()In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
StandardScaler()
scaler2.mean_
array([25. , 2.5])
scaler2.scale_
array([11.18033989, 1.11803399])
scaler2.transform(df)
array([[-1.34164079, -1.34164079],
[-0.4472136 , -0.4472136 ],
[ 0.4472136 , 0.4472136 ],
[ 1.34164079, 1.34164079]])
scaler2.transform(df2)
array([[2.23606798, 2.23606798],
[3.13049517, 3.13049517],
[4.02492236, 4.02492236],
[4.91934955, 4.91934955]])