<jupyter_start><jupyter_text>In this assignment, you are required to implement functions to calculate the listed performance measures below from scratch. **YOU CAN USE NUMPY FOR CALCULATIONS AND MATPLOTLIB FOR VISUALIZATION. DON'T USE ANY OTHER PACKAGE**.* Accuracy* Confusion matrix* Percision* Recall* F1 score* AUC of ROCYou're also required to implement functions to display the following* Confusion matrix* Percision VS Recall curve* ROC curveAfter finishing the assignment, please run all cells then save the notebook as PDF and attach the PDF to the original notebook in the email.<jupyter_code># DON'T CHANGE THIS
# DON'T 
import numpy as np

np.random.seed(42)
y = np.random.randint(2, size=(100, 1))          # Generating random labels
y_preds = np.random.uniform(0, 1, size=(100, 1)) # Generating random predictions

# DON'T CHANGE THIS
import matplotlib.pyplot as plt

plt.plot(y)
plt.plot(y_preds)<jupyter_output><empty_output><jupyter_text>You can start your implementation below. Accuracy<jupyter_code>y_preds_rounded = y_preds.round()
def accuracy(y_preds_rounded, y):
    acc = (y == y_preds_rounded).sum() / len(y)
    return acc

print(f"Accuracy is: {accuracy(y_preds_rounded, y)}")<jupyter_output>Accuracy is: 0.54<jupyter_text>Confusion matrix<jupyter_code>def confusion_matrix(y_preds_rounded, y):
    num_classes = len(np.unique(y)) 
    result = np.zeros((num_classes, num_classes))

    for i in range(len(y)):
        result[int(y[i])][int(y_preds_rounded[i])] += 1

    return result

conf_matrix = confusion_matrix(y_preds_rounded, y)
print(f"Confusion Matrix: \n{conf_matrix}")
def plot_confusion_matrix(conf_matrix, title='Confusion matrix'):
    fig, ax = plt.subplots(figsize=(5, 5))
    ax.matshow(conf_matrix, cmap=plt.cm.BuPu, alpha=0.4)
    
    for i in range(conf_matrix.shape[0]):
        for j in range(conf_matrix.shape[1]):
            ax.text(x=j, y=i, s=conf_matrix[i, j],
                    va='center', ha='center',
                    size='xx-large')
    
    plt.xlabel('Predictions',     fontsize=18)
    plt.ylabel('Actuals',         fontsize=18)
    plt.title(title, fontsize=18)
    plt.show()
    

plot_confusion_matrix(conf_matrix)<jupyter_output><empty_output><jupyter_text>3. Percision<jupyter_code># True Positive  --> 1,1
# False Positive --> 1,0

def precision(y_preds_rounded, y):
    TP = ((y_preds_rounded == 1) & (y == 1)).sum()
    FP = ((y_preds_rounded == 1) & (y == 0)).sum()
    precision = TP / (TP + FP)
    return precision

precision_val = precision(y_preds_rounded, y)
print(f"Precision is: {precision_val}")<jupyter_output>Precision is: 0.6041666666666666<jupyter_text>4. Recall<jupyter_code># True Positive  --> 1,1
# False Negative --> 0,1
def recall(y_preds_rounded, y):
    TP = ((y_preds_rounded == 1) & (y == 1)).sum()
    FN = ((y_preds_rounded == 0) & (y == 1)).sum()
    recall = (TP) / (TP + FN)
    return recall

recall_val = recall(y_preds_rounded, y)
print(f"Recall is: {recall_val}")<jupyter_output>Recall is: 0.5178571428571429<jupyter_text>Percision VS Recall curve<jupyter_code>def precision_recall_curve(y_preds_rounded, y):
    
    thresholds = np.arange(0, 1, 0.1)
    
    precisons = []
    recalls   = [] 
    
    for t in thresholds:
        y_preds_rounded = np.array([1 if i >= t else 0 for i in y_preds])
        y_preds_rounded = y_preds_rounded.reshape(y.shape)
        
        TP = ((y_preds_rounded == 1) & (y == 1)).sum()
        FN = ((y_preds_rounded == 0) & (y == 1)).sum()
        TN = ((y_preds_rounded == 0) & (y == 0)).sum()
        FP = ((y_preds_rounded == 1) & (y == 0)).sum()
        
        precisons.append(TP / (TP+FP))
        recalls.append  (TP / (TP+FN))
        
        plt.plot(recalls, precisons, color='purple')
    
precision_recall_curve(y_preds_rounded, y)<jupyter_output><empty_output><jupyter_text>5. F1 Score<jupyter_code>def f1_score(precision, recall):
    score = 2 * (precision * recall) / (precision + recall)
    return score

score = f1_score(precision_val, recall_val)
print(f"F1_Score: {score}")<jupyter_output>F1_Score: 0.5576923076923077<jupyter_text>6. AUC of ROC<jupyter_code>def auc_roc(y_preds_rounded, y):
    TP = ((y_preds_rounded == 1) & (y == 1)).sum()
    FN = ((y_preds_rounded == 0) & (y == 1)).sum()
    TN = ((y_preds_rounded == 0) & (y == 0)).sum()
    FP = ((y_preds_rounded == 1) & (y == 0)).sum()
    
    FPR = FP / ( FP + TN )
    TPR = TP / ( TP + FN )
    
    roc = (1+TPR-FPR) / 2
    return roc

auc_roc_val = auc_roc(y_preds_rounded, y)
print(f"AUC_ROC: {auc_roc_val}")
def roc_curve(y_preds_rounded, y):
    
    thresholds = np.arange(0, 1, 0.1)
    
    TPR_lst = []
    FPR_lst = [] 
    
    for t in thresholds:
        y_preds_rounded = np.array([1 if i >= t else 0 for i in y_preds])
        y_preds_rounded = y_preds_rounded.reshape(y.shape)
        
        TP = ((y_preds_rounded == 1) & (y == 1)).sum()
        FN = ((y_preds_rounded == 0) & (y == 1)).sum()
        TN = ((y_preds_rounded == 0) & (y == 0)).sum()
        FP = ((y_preds_rounded == 1) & (y == 0)).sum()
        
        TPR_lst.append(TP / (TP+FN))
        FPR_lst.append(FP / (FP+TN))
        
        plt.plot([1,0], [1,0], linestyle='--', color='grey')
        plt.plot(FPR_lst, TPR_lst)
        
        
    
    
roc_curve(y_preds_rounded, y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14617691154422788, 'fraction_numerical': 0.02792353823088456, 'mean_word_length': 3.346091205211726, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '489092', 'license': 'Apache-2.0', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2007, 'n_words': 440, 'path': 'exercises/E1 - Classification Performance Measures.ipynb', 'repo_name': '0marMarie/handson-ml2', 'size': '5336'}