ium_464953/sacred/sacred_use_model.py
Michal Gulczynski 54c34cd3ad ium_07 sacred
2024-06-11 19:30:31 +02:00

47 lines
1.8 KiB
Python

import pickle
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
from sacred import Experiment
from sacred.observers import MongoObserver, FileObserver
ex = Experiment('464953_evaluation')
ex.observers.append(MongoObserver(url='mongodb://admin:IUM_2021@tzietkiewicz.vm.wmi.amu.edu.pl:27017'))
ex.observers.append(FileObserver('my_experiment_logs'))
@ex.config
def config():
model_filename = 'model.pkl'
test_dataset_filename = 'datasets/docker_test_dataset.csv'
@ex.main
def run_evaluation(model_filename, test_dataset_filename):
with open(model_filename, 'rb') as file:
model = pickle.load(file)
print("Model został wczytany z pliku:", model_filename)
test_df = pd.read_csv(test_dataset_filename)
Y_test = test_df[['playlist_genre']]
X_test = test_df.drop(columns='playlist_genre')
Y_test = np.ravel(Y_test)
scaler = StandardScaler()
numeric_columns = X_test.select_dtypes(include=['int', 'float']).columns
X_test_scaled = scaler.fit_transform(X_test[numeric_columns])
Y_pred = model.predict(X_test_scaled)
labels_dict = {0: 'edm', 1 : 'latin', 2 : 'pop', 3 : 'r&b', 4 : 'rap', 5 :'rock'}
Y_test_labels = [labels_dict[number] for number in Y_test]
Y_pred_labels = [labels_dict[number] for number in Y_pred]
accuracy = accuracy_score(Y_test, Y_pred)
ex.log_scalar('accuracy', accuracy)
with open('model_predictions.txt', 'w') as f:
f.write("Real:" + str(Y_test_labels[:20]) + " \nPredicted: " + str(Y_pred_labels[:20]))
f.write("\nAccuracy:" + str(accuracy))
ex.add_artifact('model_predictions.txt')
ex.add_resource(__file__)
print("Accuracy:", accuracy)
return accuracy
if __name__ == '__main__':
ex.run_commandline()