SNIPPET
4 Upvotes

Normalize Windowed Time Series

Python
Data Preprocessing

Gets a column and a dataframe(or series), fits the scaler to column and transform dataframe(or series).

def normalize(X_col, X):
    '''Gets a column and a dataframe(or series), fits the scaler to column and transform dataframe(or series).
    Inputs:
    X_col: A column(Pandas Series) to fit scaler.
    X: Pandas DataFrame or Series to transform.
    '''
    # Wrangle X_col and fit the scaler
    from sklearn.preprocessing import MinMaxScaler
    column_array = np.array(X_col)
    column_array = column_array.reshape((-1,1))
    scaler = MinMaxScaler()
    scaler.fit(column_array)
    
    # Check if X is a dataframe or series.
    if str(type(X)) == "":
        norm_X = {}
        for col in X.columns:
            array_col = np.array(X[str(col)]).reshape((-1,1))
            norm_X[col] = scaler.transform(array_col)
            norm_X[col] = norm_X[col].flatten()
    
        X_norm = pd.DataFrame(norm_X)
        return X_norm
    elif str(type(X)) == "":
        y_col = np.array(y_train).reshape((-1,1))
        norm_y = scaler.transform(y_col)
        norm_y = norm_y.flatten()
        y_normalized = pd.DataFrame(norm_y)
        return y_normalized        

By themeansquare - Last Updated June 9, 2021, 5:29 p.m.

COMMENTS
RELATED SNIPPETS
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3
SparkSession instantiate
Python
Data Preprocessing

2
Converting Data Types
Python
Data Preprocessing

Data-cleaning

2
Data Minification
Python
Data Preprocessing

Cleaning

2
Search Snippets by Tag: