Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
Scikit-Learn
import pandas as pd
import numpy as np
import sklearn
np.random.seed(1)
df= pd.DataFrame({
'x1': np.random.normal(0, 4, 500),
'x2': np.random.normal(-2, 2, 500),
'x3': np.random.normal(4, 2, 500)
})
df #dataframe before scaling
from sklearn import preprocessing
scaler = preprocessing.StandardScaler()
scaled_df = scaler.fit_transform(df)
scaled_df = pd.DataFrame(scaled_df, columns=['x1', 'x2', 'x3'])
#inverse transform is used to scale the data back to orginal form
inv_scaled_df = scaler.inverse_transform(scaled_df)
print(scaled_df) #dataframe after scaling
#print(inv_scaled_df)
#print(df)
x1 x2 x3
0 1.589332 -1.791369 -0.226042
1 -0.672994 0.033836 -2.456129
2 -0.588430 -0.846312 0.420909
3 -1.139616 -0.324437 -0.393153
4 0.821493 -0.290932 -1.554581
.. ... ... ...
495 -0.892409 -0.144487 0.116289
496 0.481019 -2.364563 -0.695052
497 -2.317386 -0.096384 -0.281432
498 -1.174767 0.338718 1.780107
499 -0.071990 -0.216930 -1.427694
[500 rows x 3 columns]