2023-05-10 21:13:53 +02:00
|
|
|
import tensorflow as tf
|
|
|
|
import pandas as pd
|
|
|
|
import numpy as np
|
|
|
|
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
import os
|
2023-05-10 19:49:38 +02:00
|
|
|
|
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
model_path = 'model.h5'
|
|
|
|
test_data_path = 'data.csv'
|
|
|
|
metrics_file_path = 'metrics.txt'
|
|
|
|
plot_path = 'plot.png'
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
model = tf.keras.models.load_model(model_path)
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
test_data = pd.read_csv(test_data_path, sep=';')
|
|
|
|
test_data = pd.get_dummies(test_data, columns=['Sex', 'Medal'])
|
|
|
|
test_data = test_data.drop(columns=['Name', 'Team', 'NOC', 'Games', 'Year', 'Season', 'City', 'Sport', 'Event'])
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
scaler = MinMaxScaler()
|
|
|
|
test_data = pd.DataFrame(scaler.fit_transform(test_data), columns=test_data.columns)
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
X_test = test_data.filter(regex='Sex|Age')
|
|
|
|
y_test = test_data.filter(regex='Medal')
|
|
|
|
y_test = pd.get_dummies(y_test)
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
X_test = X_test.fillna(0)
|
|
|
|
y_test = y_test.fillna(0)
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
y_pred = model.predict(X_test)
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
top_1_accuracy = tf.keras.metrics.categorical_accuracy(y_test, y_pred)
|
|
|
|
top_5_accuracy = tf.keras.metrics.top_k_categorical_accuracy(y_test, y_pred, k=5)
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
if os.path.exists(metrics_file_path):
|
|
|
|
metrics_df = pd.read_csv(metrics_file_path)
|
|
|
|
else:
|
|
|
|
metrics_df = pd.DataFrame(columns=['top_1_accuracy', 'top_5_accuracy'])
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
new_row = pd.DataFrame([{'top_1_accuracy': np.mean(top_1_accuracy.numpy()), 'top_5_accuracy': np.mean(top_5_accuracy.numpy())}])
|
|
|
|
metrics_df = pd.concat([metrics_df, new_row], ignore_index=True)
|
|
|
|
metrics_df.to_csv(metrics_file_path, index=False)
|
2023-05-10 19:49:38 +02:00
|
|
|
|
2023-05-10 21:13:53 +02:00
|
|
|
plt.figure(figsize=(10, 6))
|
|
|
|
plt.plot(metrics_df['top_1_accuracy'], label='Top-1 Accuracy')
|
|
|
|
plt.plot(metrics_df['top_5_accuracy'], label='Top-5 Accuracy')
|
|
|
|
plt.legend()
|
|
|
|
plt.savefig(plot_path)
|