# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3.7.7 64-bit
#     name: python3
# ---

import os
import os.path as path
import numpy as np
import pandas as pd 
import matplotlib.pyplot as plt 
from tensorflow.keras import layers, models, optimizers, regularizers
from tensorflow.keras.models import load_model

current_dir = os.path.join(os.getcwd())
file = os.path.join(path.dirname(path.dirname(current_dir)), "generate_data\data_cwa.csv")
myData = pd.read_csv(file, delimiter=',', usecols=['cwa','credit','time','difficulty', 'score'])
my_data_copy = myData
myData.shape

myData["score"] = myData["score"].values / 100
myData["cwa"] = myData["cwa"].values / 100
myData["credit"] = myData["credit"].values / 10
myData ["difficulty"] = myData['difficulty'].values / 5
myData["time"] = myData["time"].values / 6

# +
df = pd.DataFrame(myData)
df = df.sample(frac=1)

myData = df

# +
targets = myData[['time']].values
myData.drop(('time'), axis=1, inplace=True)
data = myData.values

print(targets.shape)
print(data.shape)

# +
# num_train = int(0.5 * len(data)) 
# num_val = int(0.25 * len(data))
# num_test = int(0.25 * len(data))

# +
# train_data = data[0 : num_train]
# test_data = data[num_train: num_train + num_test]
# val_data = data[num_train + num_test:]

# train_targets = targets[0 : num_train]
# test_targets = targets[num_train: num_train + num_test]
# val_targets = targets[num_train + num_test:]

train_data = data[0 : 2000]
test_data = data[2000: 3000]
val_data = data[3000:4000]

train_targets = targets[0 : 2000]
test_targets = targets[2000: 3000]
val_targets = targets[3000 :4000]

print(len(train_data) + len(test_data) + len(val_data))
print(len(train_targets) + len(test_targets) + len(val_targets))

# +
model = models.Sequential()
model.add(layers.Dense(64, activation="relu", input_shape=(train_data.shape[1],)))
# model.add(layers.Dropout(0.5))
model.add(layers.Dense(64, activation="relu"))
# model.add(layers.Dropout(0.5))
model.add(layers.Dense(64, activation="relu"))
# model.add(layers.Dropout(0.5))
model.add(layers.Dense(1))

model.summary()
# -

model.compile(
    optimizer=optimizers.RMSprop(learning_rate=2e-4),
    loss="mse",
    metrics=['mae']
    )

history = model.fit(train_data,
                    train_targets,
                    epochs=50,
                    batch_size=100,
                    validation_data=(val_data, val_targets)
                    )

acc = history.history['mae']
val_acc = history.history['val_mae']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
# "bo" is for "blue dot"
plt.plot(epochs, loss, 'bo', label='Training loss')
# b is for "solid blue line"
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

test_mse_score, test_mae_score = model.evaluate(test_data, test_targets)

model.save('time_prediction.h5')

predicted = model.predict([[0.8771, 0.4, 0.6, 0.82]])
predicted   



StarcoderdataJupyterScriptsDedupFiltered