1import pandas as pd
2from sklearn import preprocessing
3
4x = df.values #returns a numpy array
5min_max_scaler = preprocessing.MinMaxScaler()
6x_scaled = min_max_scaler.fit_transform(x)
7df = pd.DataFrame(x_scaled)
1>>> from sklearn import preprocessing
2>>>
3>>> data = [100, 10, 2, 32, 31, 949]
4>>>
5>>> preprocessing.normalize([data])
6array([[0.10467389, 0.01046739, 0.00209348, 0.03349564, 0.03244891,0.99335519]])
7
1# min-max normalization:
2df=(df-df.min())/(df.max()-df.min())
3
4# or...
5
6# mean normalization:
7df=(df-df.mean())/df.std()
1# define a method to scale data, looping thru the columns, and passing a scaler
2def scale_data(data, columns, scaler):
3 for col in columns:
4 data[col] = scaler.fit_transform(data[col].values.reshape(-1, 1))
5 return data