ium_434760/train.py

56 lines
1.7 KiB
Python
Raw Normal View History

2021-04-25 17:39:38 +02:00
import pandas as pd
from os import path
from tensorflow import keras
from tensorflow.keras import layers
model_name = "model.h5"
train_data=pd.read_csv('train.csv')
input_columns=["Age","Nationality","Position","Club"]
X=train_data[input_columns].to_numpy()
Y=train_data[["Overall"]].to_numpy()
model = None
if path.exists(model_name):
model = keras.models.load_model(model_name)
else:
model = keras.Sequential(name="fifa_overall")
model.add(keras.Input(shape=(len(input_columns),), name="player_info"))
model.add(layers.Dense(4, activation="relu", name="layer1"))
model.add(layers.Dense(8, activation="relu", name="layer2"))
model.add(layers.Dense(8, activation="relu", name="layer3"))
model.add(layers.Dense(5, activation="relu", name="layer4"))
model.add(layers.Dense(1, activation="relu", name="output"))
model.compile(
optimizer=keras.optimizers.RMSprop(),
loss=keras.losses.MeanSquaredError(),
)
history = model.fit(
X,
Y,
batch_size=16,
epochs=15,
)
model.save(model_name)
test_data=pd.read_csv('test.csv')
X_test=test_data[input_columns].to_numpy()
Y_test=test_data[["Overall"]].to_numpy()
results_train = model.evaluate(X, Y, batch_size=128)
results_test = model.evaluate(X_test, Y_test, batch_size=128)
y_pred = model(X_test)
lines = ["Name;Overall;Predicted overall\n"]
for i in range(len(X_test)):
name = test_data["Name"][i]
lines.append(f"{name};{int(Y_test[i])};{int(y_pred[i])}\n")
with open('results.csv', 'w+', encoding="UTF-8") as f:
f.writelines(lines)
with open('evaluation_result.txt', 'w+', encoding="UTF-8") as f:
f.write(f"Train: {str(results_train)}\nTest: {str(results_test)}")