text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from sklearn.preprocessing import StandardScaler

from sklearn.utils.class_weight import compute_class_weight

import numpy as np

import tensorflow as tf

X_train, y_train = (train_df.drop('quality', axis=1), train_df['quality'])
X_val, y_val = (val_df.drop('quality', axis=1), val_df['quality'])
X_test, y_test = (test_df.drop('quality', axis=1), test_df['quality'])
(X_train.shape, y_train.shape, X_val.shape, y_val.shape, X_test.shape, y_test.shape)

from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_val_scaled = scaler.transform(X_val)
X_test_scaled = scaler.transform(X_test)

y_train_scaled = y_train - 3
y_val_scaled = y_val - 3
y_test_scaled = y_test - 3

model = tf.keras.Sequential([tf.keras.layers.Dense(100, activation='relu'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(100, activation='relu'), tf.keras.layers.Dropout(0.5), tf.keras.layers.Dense(6, activation='softmax')])
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])
history = model.fit(X_train_scaled, y_train_scaled, epochs=30, validation_data=(X_val_scaled, y_val_scaled))

y_pred = tf.argmax(model.predict(X_val_scaled), axis=1) + 3

from sklearn.utils.class_weight import compute_class_weight
weights = compute_class_weight(class_weight='balanced', classes=np.unique(y_train), y=y_train)
for label, weight in zip(np.unique(y_train), weights):
    print(f'label: {label}, number of instances: {len(y_train[y_train == label])}, weight: {weight}')