<jupyter_start><jupyter_text>XGBoost Evaluation From our paper on "Explainable Prediction of Acute Myocardial Infarction using Machine Learning and Shapley Values"<jupyter_code># Import libraries 
import pandas as pd
import numpy as np
import seaborn as sns
import pylab as plt
import pickle
import time
from sklearn.metrics import accuracy_score, f1_score, roc_auc_score
from sklearn.model_selection import StratifiedKFold
from sklearn.model_selection import cross_val_score
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report<jupyter_output><empty_output><jupyter_text>Loading Data + XGBoost Model<jupyter_code># Load data
# Import train and test data into dataframes from csv files produced using the data processing code
df_train = pd.read_csv("train.csv", header=None)
df_train = df_train.sample(frac=1)
df_test = pd.read_csv("test.csv", header=None)
# Get data from dataframes
train_y = np.array(df_train[11].values).astype(np.int8)
train_x = np.array(df_train[list(range(11))].values)
test_y = np.array(df_test[11].values).astype(np.int8)
test_x = np.array(df_test[list(range(11))].values)
# load model from file
model = pickle.load(open("XGBoost.model", "rb"))<jupyter_output><empty_output><jupyter_text>Model Evaluation<jupyter_code>start_time = time.time()

## test the model on the test data
pred_test = model.predict(test_x)

# Get f1 score 
f1 = f1_score(test_y, pred_test, average="macro")
print("Test f1 score : %s "% f1)

# Get ROC AUC score 
roc = roc_auc_score(test_y, pred_test)
print("Test ROC AUC score : %s "% roc)

# Get the accuracy
acc = accuracy_score(test_y, pred_test)
print("Test accuracy score : %s "% acc)

print("--- %s seconds ---" % (time.time() - start_time))
# 10-fold cross validation on the test data 
kfold = StratifiedKFold(n_splits=10, random_state=7)
results = cross_val_score(model, test_x, test_y, cv=kfold)
print("Accuracy: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100))
# Confusion matrix
confusion = confusion_matrix(test_y, pred_test)
sns.heatmap(data=confusion, annot=True, xticklabels=["NotMI", "MI"], yticklabels=["NotMI", "MI"], fmt = "d", annot_kws={"fontsize":16})
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.yticks(va="center")
plt.show()
print(classification_report(test_y, pred_test, digits=4))<jupyter_output>precision    recall  f1-score   support

           0     0.9685    0.9942    0.9812     70559
           1     0.9878    0.9351    0.9608     35199

    accuracy                         0.9746    105758
   macro avg     0.9781    0.9647    0.9710    105758
weighted avg     0.9749    0.9746    0.9744    105758
{'fraction_non_alphanumeric': 0.11671087533156499, 'fraction_numerical': 0.04888215233042819, 'mean_word_length': 4.827814569536423, 'pattern_counts': {'":': 1, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '88739', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1016, 'n_tokens_neox': 932, 'n_words': 231, 'path': 'XGBoost/.ipynb_checkpoints/XGBoost_evaluation-checkpoint.ipynb', 'repo_name': 'lujainibrahim/ecg-view-II-machine-learning', 'size': '2639'}