# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Multiclass classification/ Phân loại nhiều lớp với DNN

# ## Import motdules

# %matplotlib inline
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD, Adam
from tensorflow.keras.callbacks import EarlyStopping

# ## Load dataset

df = pd.read_csv('../data/fetal_health.csv')
df.head()

df.info()

df.head()

X = df.drop('fetal_health', axis=1)
X.head()

# ## Mã hoá lại y

target_names = df['fetal_health'].unique()
target_names

target_dict = {n:i for i, n in enumerate(target_names)}
target_dict

y= df['fetal_health'].map(target_dict)
y.head(10)

from tensorflow.keras.utils import to_categorical

y_cat = to_categorical(y, dtype = int)

y_cat[:10]

# ### Train test split

X_train, X_test, y_train, y_test = train_test_split(X.values, y_cat,
                                                    test_size=0.2,
                                                   random_state=10)

# ## Tạo model

model = Sequential()
model.add(Dense(64, input_shape=(21,), activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu'))
# model.add(Dense(100, activation='relu'))
# model.add(Dense(12, activation='relu'))
# model.add(Dense(10, activation='relu'))
# model.add(Dense(4, activation='relu'))
model.add(Dense(3, activation='softmax'))
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.summary()

model.fit(X_train, y_train, epochs=150, validation_split=0.1)

y_pred = model.predict(X_test)

y_pred[:5]

y_test_class = np.argmax(y_test, axis=1)
y_pred_class = np.argmax(y_pred, axis=1)

from sklearn.metrics import classification_report

print(classification_report(y_test_class, y_pred_class))

from sklearn.metrics import accuracy_score, confusion_matrix
confusion_matrix(y_test_class, y_pred_class)

from sklearn.metrics import log_loss
log_loss(y_test_class, y_pred)

loss, accuracy = model.evaluate(X_test, y_test)
print("Loss: " + str(loss));
print("Accuracy: " + str(accuracy));



StarcoderdataJupyterScriptsDedupFiltered