text_plain_output_1.png
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

from sklearn.preprocessing import StandardScaler

from sklearn.utils.class_weight import compute_class_weight

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

import matplotlib.pyplot as plt

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

# plot confusion matrix
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

cm = confusion_matrix(y_val, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix = cm,
                             display_labels = [3, 4, 5, 6, 7, 8])

fig, ax = plt.subplots(figsize = (10, 10))
disp.plot(ax = ax)
plt.show()

from sklearn.utils.class_weight import compute_class_weight
weights = compute_class_weight(class_weight='balanced', classes=np.unique(y_train), y=y_train)

weights_dict = {i: weights[i] for i in range(len(weights))}

model = tf.keras.Sequential([tf.keras.layers.Dense(100, activation='relu'), tf.keras.layers.Dropout(0.8), 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.3), tf.keras.layers.Dense(6, activation='softmax')])
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(), optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), metrics=['accuracy'])
history = model.fit(X_train_scaled, y_train_scaled, epochs=400, validation_data=(X_val_scaled, y_val_scaled), class_weight=weights_dict)

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

# plot confusion matrix
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

cm = confusion_matrix(y_val, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix = cm,
                             display_labels = [3, 4, 5, 6, 7, 8])

fig, ax = plt.subplots(figsize = (10, 10))
disp.plot(ax = ax)
plt.show()

y_pred_test = tf.argmax(model.predict(X_test_scaled), axis=1) + 3

import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
cm = confusion_matrix(y_test, y_pred_test)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=[3, 4, 5, 6, 7, 8])
fig, ax = plt.subplots(figsize=(10, 10))
disp.plot(ax=ax)
plt.show()