Normalize Windowed Time Series
Python
Gets a column and a dataframe(or series), fits the scaler to column and transform dataframe(or series).
1| def normalize(X_col, X): 2| '''Gets a column and a dataframe(or series), fits the scaler to column and transform dataframe(or series). 3| Inputs: 4| X_col: A column(Pandas Series) to fit scaler. 5| X: Pandas DataFrame or Series to transform. 6| ''' 7| # Wrangle X_col and fit the scaler 8| from sklearn.preprocessing import MinMaxScaler 9| column_array = np.array(X_col) 10| column_array = column_array.reshape((-1,1)) 11| scaler = MinMaxScaler() 12| scaler.fit(column_array) 13| 14| # Check if X is a dataframe or series. 15| if str(type(X)) == "<class 'pandas.core.frame.DataFrame'>": 16| norm_X = {} 17| for col in X.columns: 18| array_col = np.array(X[str(col)]).reshape((-1,1)) 19| norm_X[col] = scaler.transform(array_col) 20| norm_X[col] = norm_X[col].flatten() 21| 22| X_norm = pd.DataFrame(norm_X) 23| return X_norm 24| elif str(type(X)) == "<class 'pandas.core.series.Series'>": 25| y_col = np.array(y_train).reshape((-1,1)) 26| norm_y = scaler.transform(y_col) 27| norm_y = norm_y.flatten() 28| y_normalized = pd.DataFrame(norm_y) 29| return y_normalized
130
118
115
108