Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions scripts/Scaling.py
Original file line number Diff line number Diff line change
@@ -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()
Loading