From 1aa9c99f040015fed4526beab428fae0c77059ea Mon Sep 17 00:00:00 2001 From: acgm8 Date: Mon, 25 Aug 2025 12:26:20 +0200 Subject: [PATCH] scaling commit --- scripts/Scaling.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 scripts/Scaling.py diff --git a/scripts/Scaling.py b/scripts/Scaling.py new file mode 100644 index 0000000..9908e3f --- /dev/null +++ b/scripts/Scaling.py @@ -0,0 +1,29 @@ +#This function scales numerical values +from sklearn.preprocessing import RobustScaler +def scaler_numeric(df, target_col=''): + + #separate the nutriscore and the rest of the values to do the scaling + X = df.drop([target_col], axis = 1) + y = df[target_col] + + #select only the numerical variables to do the scaling + X_numeric = X.select_dtypes(include=['float','int']) + print(f": shape of df with only numeric features={X_numeric.shape}") + + #scale the numerical values and put the scaled numerical data into a dataframe + scaler = RobustScaler() + X_scaled = scaler.fit_transform(X_numeric) + X_scaled_df = pd.DataFrame(X_scaled, columns=X_numeric.columns, index=X_numeric.index) + + #combine the scaled df with the nutriscore + X_non_numeric = X.select_dtypes(exclude=['float','int']) + X_processed = pd.concat([X_scaled_df, X_non_numeric], axis=1) + scaled_df = pd.concat([X_processed, y], axis=1) + + # Ensure the column order is the same as the original dataframe + scaled_df = scaled_df[df.columns] + + return scaled_df + +scaled_df = scaler_numeric(imputed_df, target_col='nutriscore_score') +scaled_df.head() \ No newline at end of file