<jupyter_start><jupyter_text>Load data<jupyter_code>y_score_30_linreg = pd.read_csv("plot/y_score_linear.csv",header = None)

y_score_30_logreg = pd.read_csv("plot/y_score_lg.csv",header = None)

y_score_30_GNB = pd.read_csv("plot/y_score_gnb.csv",header = None)

y_score_30_svm = pd.read_csv("plot/y_score_svm.csv",header = None)

y_score_30_kNN = pd.read_csv("plot/y_score_knn.csv",header = None)

y_test_30 = pd.read_csv("plot/y_test.csv",header = None)
y_score_30_kNN.shape<jupyter_output><empty_output><jupyter_text>Plot ROC of different models predicting 1 year mortality.<jupyter_code>def plot_ROC(fpr, tpr, auc,name='', title = 'ROC curve'):
#     sns.set_style('whitegrid')
#     plt.figure(figsize=(8,6))
    plt.plot([0, 1], [0, 1], 'k--')
    plt.plot(fpr, tpr, label='{}: (AUC = {:.3f})'.format(name,auc))
    plt.xlabel('False positive rate')
    plt.ylabel('True positive rate')
    plt.title(title)
    plt.legend(loc='best')
    plt.rcParams.update({'font.size': 18})
#     plt.show()
    pass
# plt.rcParams.update({'font.size': 18})
# plt.figure(figsize=(16,12))
# fpr, tpr, thresholds = metrics.roc_curve(y_test_1y, y_score_1y_vgg)
# auc = metrics.auc(fpr, tpr)
# plot_ROC(fpr, tpr, auc,name = 'VGG')

# fpr, tpr, thresholds = metrics.roc_curve(y_test_1y, y_score_1y_simple)
# auc = metrics.auc(fpr, tpr)
# plot_ROC(fpr, tpr, auc,name = 'Simple CNN')

# fpr, tpr, thresholds = metrics.roc_curve(y_test_1y, y_score_1y_linreg)
# auc = metrics.auc(fpr, tpr)
# plot_ROC(fpr, tpr, auc,name = 'Linear Reg')

# fpr, tpr, thresholds = metrics.roc_curve(y_test_1y, y_score_1y_logreg)
# auc = metrics.auc(fpr, tpr)
# plot_ROC(fpr, tpr, auc,name = 'Logistic Reg')

# fpr, tpr, thresholds = metrics.roc_curve(y_test_1y, y_score_1y_kNN)
# auc = metrics.auc(fpr, tpr)
# plot_ROC(fpr, tpr, auc,name = 'kNN')

# fpr, tpr, thresholds = metrics.roc_curve(y_test_1y, y_score_1y_GNB)
# auc = metrics.auc(fpr, tpr)
# plot_ROC(fpr, tpr, auc,name = 'GNB')

# fpr, tpr, thresholds = metrics.roc_curve(y_test_1y, y_score_1y_xgtree)
# auc = metrics.auc(fpr, tpr)
# plot_ROC(fpr, tpr, auc,name = 'xgtree')

# fpr, tpr, thresholds = metrics.roc_curve(y_test_1y, y_score_1y_rf)
# auc = metrics.auc(fpr, tpr)
# plot_ROC(fpr, tpr, auc,name = 'Random Forest',title = 'ROC of different model predicting 1 year mortality')<jupyter_output><empty_output><jupyter_text>Plot ROC of different models predicting 30 day mortality.<jupyter_code>plt.figure(figsize=(16,12))


fpr, tpr, thresholds = metrics.roc_curve(y_test_30, y_score_30_linreg)
auc = metrics.auc(fpr, tpr)
plot_ROC(fpr, tpr, auc,name = 'Linear   ')

fpr, tpr, thresholds = metrics.roc_curve(y_test_30, y_score_30_logreg)
auc = metrics.auc(fpr, tpr)
plot_ROC(fpr, tpr, auc,name = 'Logistic ')

fpr, tpr, thresholds = metrics.roc_curve(y_test_30, y_score_30_kNN)
auc = metrics.auc(fpr, tpr)
plot_ROC(fpr, tpr, auc,name = 'kNN      ')

fpr, tpr, thresholds = metrics.roc_curve(y_test_30, y_score_30_GNB)
auc = metrics.auc(fpr, tpr)
plot_ROC(fpr, tpr, auc,name = 'NB        ')

fpr, tpr, thresholds = metrics.roc_curve(y_test_30, y_score_30_svm)
auc = metrics.auc(fpr, tpr)
plot_ROC(fpr, tpr, auc,name = 'SVM     ', title = 'ROC for all classifiers')


# fpr, tpr, thresholds = metrics.roc_curve(y_test_30, y_score_30_rf)
# auc = metrics.auc(fpr, tpr)
# plot_ROC(fpr, tpr, auc,name = 'Random Forest',title = 'ROC of different model predicting 30 day mortality')<jupyter_output><empty_output><jupyter_text>Plot Precision-Recall Curve of different models predicting 1 year mortality.<jupyter_code>def plot_PRC(precision, recall, ap, name = '',title= 'Precision-Recall Curve'):
#     sns.set_style('whitegrid')
    
#    plt.figure()
    plt.plot(recall, precision, lw=2, label='{}: (AP = {:.4f})'.format(name,ap))
    plt.xlabel('Recall')
    plt.ylabel('Precision')
    plt.title(title)
    plt.legend(loc='lower left')
    plt.rcParams.update({'font.size': 18})
    return<jupyter_output><empty_output><jupyter_text>Plot Precision-Recall Curve of different models predicting 30 day mortality.<jupyter_code>plt.figure(figsize=(16,12))


precision, recall, _ = metrics.precision_recall_curve(y_test_30, y_score_30_logreg)
average_precision = metrics.average_precision_score(y_test_30, y_score_30_logreg)
plot_PRC(precision, recall, average_precision,name = 'Logistic ')

precision, recall, _ = metrics.precision_recall_curve(y_test_30, y_score_30_linreg)
average_precision = metrics.average_precision_score(y_test_30, y_score_30_linreg)
plot_PRC(precision, recall, average_precision,name = 'Linear   ')

precision, recall, _ = metrics.precision_recall_curve(y_test_30, y_score_30_kNN)
average_precision = metrics.average_precision_score(y_test_30, y_score_30_kNN)
plot_PRC(precision, recall, average_precision,name = 'kNN      ')

precision, recall, _ = metrics.precision_recall_curve(y_test_30, y_score_30_GNB)
average_precision = metrics.average_precision_score(y_test_30, y_score_30_GNB)
plot_PRC(precision, recall, average_precision,name = 'NB        ')

precision, recall, _ = metrics.precision_recall_curve(y_test_30, y_score_30_svm)
average_precision = metrics.average_precision_score(y_test_30, y_score_30_svm)
plot_PRC(precision, recall, average_precision,name = 'SVM     '
        , title = 'Precision-Recall Curve for all classifers')<jupyter_output><empty_output><jupyter_text>Confusion matrix<jupyter_code>def probability_to_label(probabilities, threshold=0.5):
    probabilities = list(probabilities)
    th = threshold
    predictions = [1 if i > th else 0 for i in probabilities]
    return predictions

def plot_cm(y_test, y_score, title='Confusion Matrix', cmap=plt.cm.Blues):
    
    y_pred = probability_to_label(y_score, threshold=0.5)
    
    sns.set_style('white')
    
    cm = metrics.confusion_matrix(y_test, y_pred)
    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] # normarlize 
    
    from sklearn.utils.multiclass import unique_labels
    classes = unique_labels(y_test, y_pred)
    
    fig, ax = plt.subplots()
    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
    ax.figure.colorbar(im, ax=ax)
    # We want to show all ticks...
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           # ... and label them with the respective list entries
           xticklabels=classes, yticklabels=classes,
           title=title,
           ylabel='True label',
           xlabel='Predicted label')

    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
             rotation_mode="anchor")
    plt.rcParams.update({'font.size': 10})
    # Loop over data dimensions and create text annotations.
    fmt = '.2f' 
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="white" if cm[i, j] > thresh else "black")
    fig.tight_layout()
    
y_pred_30_vgg = probability_to_label(y_score_30_vgg.values, threshold=0.5)
plot_cm(y_test_30, y_pred_30_vgg,title = 'Confusion Matrix of VGG for 30 days')

y_pred_30_rf = probability_to_label(y_score_30_rf.values, threshold=0.5)
plot_cm(y_test_30, y_pred_30_rf,title = 'Confusion Matrix of Random Forest for 30 days')

y_pred_30_simple = probability_to_label(y_score_30_simple.values, threshold=0.5)
plot_cm(y_test_30, y_pred_30_simple,title = 'Confusion Matrix of CNN for 30 days')

y_pred_30_linreg = probability_to_label(y_score_30_linreg.values, threshold=0.5)
plot_cm(y_test_30, y_pred_30_linreg,title = 'Confusion Matrix of Linear Regression for 30 days')

y_pred_30_logreg = probability_to_label(y_score_30_logreg.values, threshold=0.5)
plot_cm(y_test_30, y_pred_30_logreg,title = 'Confusion Matrix of Logistic Regression for 30 days')

y_pred_30_kNN = probability_to_label(y_score_30_kNN.values, threshold=0.5)
plot_cm(y_test_30, y_pred_30_kNN,title = 'Confusion Matrix of k-NN for 30 days')

y_pred_30_GNB = probability_to_label(y_score_30_GNB.values, threshold=0.5)
plot_cm(y_test_30, y_pred_30_GNB,title = 'Confusion Matrix of GNB for 30 days')

y_pred_30_xgtree = probability_to_label(y_score_30_xgtree.values, threshold=0.5)
plot_cm(y_test_30, y_pred_30_xgtree,title = 'Confusion Matrix of xgtree for 30 days')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12849430113977203, 'fraction_numerical': 0.029154169166166765, 'mean_word_length': 5.22554144884242, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '511477', 'license': 'MIT', 'n_tokens_mistral': 3514, 'n_tokens_neox': 3258, 'n_words': 591, 'path': 'Plot ROC and PRC CM.ipynb', 'repo_name': 'NicolasHu11/ASD-Detection', 'size': '8335'}