# ---
# 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
# ---

import tensorflow as tf
import pandas as pd
import lux 
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder

df = pd.read_csv("dataset.csv").fillna("0").sort_values("Disease").reset_index(drop=True)
cat_col = df.select_dtypes("object").columns
df = df[cat_col].astype("category")
df.info()

df[cat_col].nunique().reset_index(name='cardinality')

data_encoder = LabelEncoder()
sym = pd.read_csv("symptom_severity.csv").Symptom
sym = list(sym) + ["0"]
data_encoder = data_encoder.fit(sym)
len(data_encoder.classes_)

for col in cat_col:
    df[col] = df[col].str.replace(" ", "")
#df.to_csv("clean_dataset.csv", index=False)

for col in cat_col[1:]:
    df[col] = data_encoder.transform(df[col])

df.head()

training_index = []
for x in df.index:
    if x % 120 == 0 and x != 0:
        for y in np.random.choice(df[x-120:x].index, size=(24)):
            training_index.append(y)
len(training_index)
training_index[0:10]

training_data = df.drop(training_index)
training_data.tail()

test_data = df.iloc[training_index]
test_data.head()

encoder = LabelEncoder()
encoder = encoder.fit(df.Disease)
encoder.classes_

train_y = encoder.transform(training_data["Disease"])
train_x = training_data.drop("Disease", axis=1)
train_x.shape

test_y = encoder.transform(test_data["Disease"])
test_x = test_data.drop("Disease", axis=1)
test_x.shape

import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], enable=True)

#np.reshape(train_x.values, [4059, 1, 17]).view("<f4")
train_x = np.reshape(train_x.values, [train_x.shape[0], 1, train_x.shape[1]])
test_x = np.reshape(test_x.values, [test_x.shape[0], 1, test_x.shape[1]])
print (train_x.shape)
print(test_x.shape)

tf.random.set_seed(99)
model_full_1 = tf.keras.Sequential([
    tf.keras.layers.LSTM(32, input_shape=[1, 17], return_sequences=True),
    tf.keras.layers.LSTM(64),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(100, activation="relu"),
    tf.keras.layers.Dense(41, activation="softmax")
])
model_full_1.summary()

model_full_1.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
num_epochs = 50
history = model_full_1.fit(train_x, train_y, epochs=num_epochs, verbose=2, validation_data=(test_x, test_y))

# Evaluate the model on the test data using `evaluate`
print("Evaluate on test data")
results = model_full_1.evaluate(test_x, test_y)
print("test loss, test acc:", results)

plt.plot(history.history["loss"], label="Train Loss")
plt.plot(history.history["val_loss"], label="Val Loss")
plt.legend()
plt.show()

plt.plot(history.history["accuracy"], label="Train Acc")
plt.plot(history.history["val_accuracy"], label="Val Acc")
plt.legend()
plt.show()

model_full_1.save('base_lstm')



StarcoderdataJupyterScriptsDedupFiltered