Close Menu
    Facebook LinkedIn YouTube WhatsApp X (Twitter) Pinterest
    Trending
    • Kalshi debuts regulated crypto perpetual futures
    • Apple Will Reportedly Add Bill-Splitting Feature to iOS 27
    • Escaping the Valley of Choice in BI
    • SEO headline New urine test uses gut biomarkers to identify autism earlier
    • Socceroos legend Tim Cahill backs sports swag design platform Nardo in $1 million pre-Seed raise
    • ‘Sexual Chocolate’ Faces Recalls After FDA Tests Reveal Undisclosed Viagra
    • Manchester gambling raid sparks wider enforcement focus
    • Electrify America Shifts From Prepaid Accounts to Direct Card Payments
    Facebook LinkedIn WhatsApp
    Times FeaturedTimes Featured
    Tuesday, June 2
    • Home
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    • More
      • AI
      • Robotics
      • Industries
      • Global
    Times FeaturedTimes Featured
    Home»Artificial Intelligence»Confusion Matrix Made Simple: Accuracy, Precision, Recall & F1-Score
    Artificial Intelligence

    Confusion Matrix Made Simple: Accuracy, Precision, Recall & F1-Score

    Editor Times FeaturedBy Editor Times FeaturedJuly 31, 2025No Comments9 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr WhatsApp Email
    Share
    Facebook Twitter LinkedIn Pinterest Telegram Email WhatsApp Copy Link


    we cope with classification algorithms in machine studying like Logistic Regression, Okay-Nearest Neighbors, Help Vector Classifiers, and many others., we don’t use analysis metrics like Imply Absolute Error (MAE), Imply Squared Error (MSE) or Root Imply Squared Error (RMSE).

    As an alternative, we generate a confusion matrix, and based mostly on the confusion matrix, a classification report.

    On this weblog, we goal to know what a confusion matrix is, the right way to calculate Accuracy, Precision, Recall and F1-Rating utilizing it, and the right way to choose the related metric based mostly on the traits of the information.

    To know the confusion matrix and classification metrics, let’s use the Breast Cancer Wisconsin Dataset.

    This dataset consists of 569 rows, and every row supplies info on varied options of a tumor together with its prognosis, whether or not it’s malignant (cancerous) or benign (non-cancerous).

    Now let’s construct a classification mannequin for this knowledge to categorise the tumors based mostly on their options.

    We now apply Logistic Regression to coach a mannequin on this dataset.

    Code:

    import pandas as pd
    from sklearn.preprocessing import LabelEncoder
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LogisticRegression
    from sklearn.metrics import confusion_matrix, classification_report
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    # Load the dataset 
    column_names = [
        "id", "diagnosis", "radius_mean", "texture_mean", "perimeter_mean", "area_mean", "smoothness_mean",
        "compactness_mean", "concavity_mean", "concave_points_mean", "symmetry_mean", "fractal_dimension_mean",
        "radius_se", "texture_se", "perimeter_se", "area_se", "smoothness_se", "compactness_se", "concavity_se",
        "concave_points_se", "symmetry_se", "fractal_dimension_se", "radius_worst", "texture_worst",
        "perimeter_worst", "area_worst", "smoothness_worst", "compactness_worst", "concavity_worst",
        "concave_points_worst", "symmetry_worst", "fractal_dimension_worst"
    ]
    
    df = pd.read_csv("C:/wdbc.knowledge", header=None, names=column_names)
    
    # Drop ID column
    df = df.drop(columns=["id"])
    
    # Encode goal: M=1 (malignant), B=0 (benign)
    df["diagnosis"] = df["diagnosis"].map({"M": 1, "B": 0})
    
    # Break up options and goal
    X = df.drop(columns=["diagnosis"])
    y = df["diagnosis"]
    
    # Practice-test cut up
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, stratify=y, random_state=42)
    
    # Scale the options
    scaler = StandardScaler()
    X_train = scaler.fit_transform(X_train)
    X_test = scaler.remodel(X_test)
    
    # Practice logistic regression
    mannequin = LogisticRegression(max_iter=10000)
    mannequin.match(X_train, y_train)
    
    # Predict
    y_pred = mannequin.predict(X_test)
    
    # Confusion Matrix and Classification Report
    conf_matrix = confusion_matrix(y_test, y_pred, labels=[1, 0])  # 1 = Malignant, 0 = Benign
    
    report = classification_report(y_test, y_pred, labels=[1, 0], target_names=["Malignant", "Benign"])
    
    # Show outcomes
    print("Confusion Matrix:n", conf_matrix)
    print("nClassification Report:n", report)
    
    # Plot Confusion Matrix
    sns.heatmap(conf_matrix, annot=True, fmt="d", cmap="Purples", xticklabels=["Malignant", "Benign"], yticklabels=["Malignant", "Benign"])
    plt.xlabel("Predicted")
    plt.ylabel("Precise")
    plt.title("Confusion Matrix")
    plt.tight_layout()
    plt.present()
    
    

    Right here, after making use of logistic regression to the information, we generated a confusion matrix and a classification report to judge the mannequin’s efficiency.

    First let’s perceive the confusion matrix

    Picture by Writer

    From the above confusion matrix

    ’60’ represents the appropriately predicted Malignant Tumors, which we consult with as “True Positives”.

    ‘4’ represents the incorrectly predicted Benign Tumors which are literally Malignant Tumors, which we consult with as “False Negatives”.

    ‘1’ represents the incorrectly predicted Malignant Tumors which are literally Benign Tumors, which we consult with as “False Positives”.

    ‘106’ represents the appropriately predicted Benign Tumors, which we consult with as “True Negatives”.

    Now let’s see what we are able to do with these values.

    For that we take into account the classification report.

    Picture by Writer

    From the above classification report, we are able to say that

    For Malignant:
    – Precision is 0.98, which suggests when the mannequin predicts the tumor as Malignant, it’s right 98% of the time.
    – Recall is 0.94, which suggests the mannequin appropriately recognized 94% of all Malignant Tumors.
    – F1-score is 0.96, which balances each the precision and recall.

    For Benign:
    – Precision is 0.96, which suggests when the mannequin predicts the tumor as Benign, it’s right 96% of the time.
    – Recall is 0.99, which suggests the mannequin appropriately recognized 99% of all Benign Tumors.
    – F1-score is 0.98.

    From the report we are able to observe that the accuracy of the mannequin is 97%.

    We even have Macro Common and Weighted Common, let’s see how these are calculated.

    Macro Common

    Macro Common calculates the typical of all metrics (precision, recall and f1-score) throughout each lessons giving equal weight to every class, no matter what number of samples every class accommodates.

    We use macro common, once we need to know the efficiency of mannequin throughout all lessons, ignoring class imbalances.

    For this knowledge:

    Picture by Writer

    Weighted Common

    Weighted Common additionally calculates the typical of all metrics however offers extra weight to the category with extra samples.

    Within the above code, we used test_size = 0.3, which suggests we put aside 30% for testing which suggests we’re utilizing 171 samples from an information of 569 samples for a take a look at set.

    The confusion matrix and classification report are based mostly on this take a look at set.

    Out of 171 samples of take a look at set, we’ve 64 Malignant tumors and 107 Benign tumors.

    Now let’s see how this weighted common is calculated for all metrics.

    Picture by Writer

    Weighted common offers us a extra lifelike efficiency measure when we’ve the category imbalanced datasets.

    We now bought an concept of every time period within the classification report and likewise the right way to calculate the macro and weighted averages.

    Now let’s see what’s using confusion matrix for producing a classification report.

    In classification report we’ve totally different metrics like accuracy, precision and many others. and these metrics are calculated utilizing the values within the confusion matrix.

    From the confusion matrix we’ve
    True Positives (TP) = 60
    False Negatives (FN) = 4
    False Positives (FP) = 1
    True Negatives (TN) = 106

    Now let’s calculate the classification metrics utilizing these values.

    Picture by Writer

    That is how we calculate the classification metrics utilizing a confusion matrix.

    However why do we’ve 4 totally different classification metrics as an alternative of 1 metric like accuracy? It’s as a result of the totally different metrics present totally different strengths and weaknesses of the classifier based mostly on the context of the information.

    Now let’s come again to the Wisconsin Breast Most cancers Dataset which we used right here.

    After we utilized a logistic regression mannequin to this knowledge, we bought an accuracy of 97% which is excessive, which can make us assume that the mannequin is environment friendly.

    However let’s take into account one other metric referred to as ‘recall’ which is 0.94 for this mannequin, which suggests out of all of the malignant tumors we’ve within the take a look at set the mannequin was in a position to determine 94% of them appropriately.

    Right here the mannequin missed 6% of malignant circumstances.

    In real-world eventualities, primarily healthcare functions like most cancers detection, if we miss a optimistic case, it would delay the prognosis and therapy.

    By this we are able to perceive that even when we’ve an accuracy of 97%, we have to look deeper based mostly on context of information by contemplating totally different metrics.

    So, what we are able to do now, ought to we goal for a recall worth of 1.0 which suggests all of the malignant tumors are recognized appropriately, but when we push recall to 1.0 then the precision drops as a result of the mannequin might classify extra benign tumors as malignant.

    When the mannequin classifies extra benign tumors as malignant, there can be pointless anxiousness, and it might require extra exams or therapies.

    Right here we should always goal to maximise ‘recall’ by retaining the ‘precision’ moderately excessive.

    We will do that by altering the thresholds set by classifiers to categorise the samples.

    A lot of the classifiers set the edge to 0.5, and if we modify it 0.3, we’re saying that even whether it is 30% assured, classify it as malignant.

    Now let’s use a customized threshold of 0.3.

    Code:

    # Practice logistic regression
    mannequin = LogisticRegression(max_iter=10000)
    mannequin.match(X_train, y_train)
    
    # Predict chances
    y_probs = mannequin.predict_proba(X_test)[:, 1]
    
    # Apply customized threshold
    threshold = 0.3
    y_pred_custom = (y_probs >= threshold).astype(int)
    
    # Classification Report
    report = classification_report(y_test, y_pred_custom, target_names=["Benign", "Malignant"])
    
    # Confusion Matrix
    conf_matrix = confusion_matrix(y_test, y_pred_custom, labels=[1, 0])
    
    # Plot Confusion Matrix
    plt.determine(figsize=(6, 4))
    sns.heatmap(
        conf_matrix,
        annot=True,
        fmt="d",
        cmap="Purples",
        xticklabels=["Malignant", "Benign"],
        yticklabels=["Malignant", "Benign"]
    )
    plt.xlabel("Predicted")
    plt.ylabel("Precise")
    plt.title("Confusion Matrix (Threshold = 0.3)")
    plt.tight_layout()
    plt.present()

    Right here we utilized a customized threshold of 0.3 and generated a confusion matrix and a classification report.

    Picture by Writer

    Classification Report:

    Picture by Writer

    Right here, the accuracy elevated to 98% and the recall for malignant elevated to 97% and the precision remained the identical.

    We earlier mentioned that there is likely to be a lower in precision if we attempt to maximize the recall however right here the precision stays identical, this is determined by the information (whether or not balanced or not), preprocessing steps and tuning the edge.

    For medical datasets like this, maximizing recall is usually most well-liked over accuracy or precision.

    After we take into account datasets like spam detection or fraud detection, we favor precision and identical as in above technique we attempt to enhance precision by tuning threshold accordingly and likewise by balancing the tradeoff between precision and recall.

    We use f1-score when the information is imbalanced, and once we favor each precision and recall the place neither false positives nor false negatives will be ignored.


    Dataset Supply
    Wisconsin Breast Cancer Dataset
    Wolberg, W., Mangasarian, O., Avenue, N., & Avenue, W. (1993). Breast Most cancers Wisconsin (Diagnostic) [Dataset]. UCI Machine Studying Repository. https://doi.org/10.24432/C5DW2B.

    This dataset is licensed underneath a Creative Commons Attribution 4.0 International (CC BY 4.0) license and is free to make use of for business or academic functions so long as correct credit score is given to unique supply.


    Right here we mentioned what a confusion matrix is and the way it’s used to calculate the totally different classification metrics like accuracy, precision, recall and f1-score.

    We additionally explored when to prioritize which classification metric, utilizing the Wisconsin most cancers dataset for instance, the place we most well-liked maximizing recall.

    I hope you discovered this weblog useful in understanding confusion matrix and classification metrics extra clearly.

    Thanks for studying.



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Editor Times Featured
    • Website

    Related Posts

    Escaping the Valley of Choice in BI

    June 2, 2026

    Ensuring Data Integrity with Cryptographic Hashing and the Ethereum Blockchain

    June 1, 2026

    RAG Is Not Machine Learning, and the ML Toolkit Solves the Wrong Problem

    June 1, 2026

    How to Combine Claude Code and Codex for Maximum Coding Power

    June 1, 2026

    It’s the Lessons We Learned Along the Way. Or, Is It?

    June 1, 2026

    Proxy-Pointer RAG: Eliminating Wasteful Entity & Relations Extraction in Knowledge Graphs

    May 31, 2026

    Comments are closed.

    Editors Picks

    Kalshi debuts regulated crypto perpetual futures

    June 2, 2026

    Apple Will Reportedly Add Bill-Splitting Feature to iOS 27

    June 2, 2026

    Escaping the Valley of Choice in BI

    June 2, 2026

    SEO headline New urine test uses gut biomarkers to identify autism earlier

    June 2, 2026
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    About Us
    About Us

    Welcome to Times Featured, an AI-driven entrepreneurship growth engine that is transforming the future of work, bridging the digital divide and encouraging younger community inclusion in the 4th Industrial Revolution, and nurturing new market leaders.

    Empowering the growth of profiles, leaders, entrepreneurs businesses, and startups on international landscape.

    Asia-Middle East-Europe-North America-Australia-Africa

    Facebook LinkedIn WhatsApp
    Featured Picks

    I’m a Fitness Expert and These Are My Go-To Pieces of Fitness Tech for 2026

    January 20, 2026

    How to Reduce Your Power BI Model Size by 90%

    May 26, 2025

    Tern GSD S10 Electric Cargo Bike Review: The Best Little Electric Cargo Bike

    June 15, 2025
    Categories
    • Founders
    • Startups
    • Technology
    • Profiles
    • Entrepreneurs
    • Leaders
    • Students
    • VC Funds
    Copyright © 2024 Timesfeatured.com IP Limited. All Rights.
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us

    Type above and press Enter to search. Press Esc to cancel.