Scale Data Using Standard Scaler in Sklearn

Python

 1|  from sklearn.preprocessing import StandardScaler
 2|  
 3|  #Initalise standard scaler
 4|  scaler = StandardScaler()
 5|  
 6|  #Fit the scaler using X_train data
 7|  scaler.fit(X_train)
 8|  
 9|  #Transform X_train and X_test using the scaler and convert back to DataFrame
10|  X_train = pd.DataFrame(scaler.transform(X_train), columns = X_train.columns)
11|  X_test = pd.DataFrame(scaler.transform(X_test), columns = X_test.columns)
Did you find this snippet useful?

Sign up for free to to add this to your code library