Close Menu
    Trending
    • Creating AI that matters | MIT News
    • Scaling Recommender Transformers to a Billion Parameters
    • Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know
    • Is RAG Dead? The Rise of Context Engineering and Semantic Layers for Agentic AI
    • ChatGPT Gets More Personal. Is Society Ready for It?
    • Why the Future Is Human + Machine
    • Why AI Is Widening the Gap Between Top Talent and Everyone Else
    • Implementing the Fourier Transform Numerically in Python: A Step-by-Step Guide
    ProfitlyAI
    • Home
    • Latest News
    • AI Technology
    • Latest AI Innovations
    • AI Tools & Technologies
    • Artificial Intelligence
    ProfitlyAI
    Home » ROC AUC Explained: A Beginner’s Guide to Evaluating Classification Models
    Artificial Intelligence

    ROC AUC Explained: A Beginner’s Guide to Evaluating Classification Models

    ProfitlyAIBy ProfitlyAISeptember 17, 2025No Comments12 Mins Read
    Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
    Share
    Facebook Twitter LinkedIn Pinterest Email


    publish on the Confusion Matrix, we utilized the logistic regression algorithm to the Breast Most cancers Wisconsin dataset to categorise whether or not the tumor is malignant or benign.

    We evaluated the classification mannequin utilizing varied metrics like accuracy, precision, and many others.

    Now, in binary classification fashions, we now have one other strategy to consider the mannequin, and that’s ROC AUC.

    On this weblog, we are going to focus on why we now have one other metric and when it needs to be used.

    To know ROC AUC intimately, we are going to take into account the IBM HR Analytics dataset.

    On this dataset, we now have details about 1,470 workers equivalent to their age, job position, gender, month-to-month revenue, job satisfaction, and many others.

    In whole, there are 34 options describing every worker.

    We even have a goal column, ‘Attrition’, which is ‘Sure’ if the worker left the corporate and ‘No’ if the worker stayed.

    Let’s take a look on the class distribution of the goal column.

    Picture by Creator

    From the above class distribution, we are able to observe that the dataset is imbalanced.

    Now, we have to construct a mannequin primarily based on this information to categorise workers based on whether or not they are going to keep within the firm or not.

    As this can be a binary classification (Sure/No) job, let’s use the logistic regression algorithm on this information.

    Code:

    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LogisticRegression
    from sklearn.preprocessing import StandardScaler
    from sklearn.metrics import confusion_matrix, classification_report
    
    # Load the dataset
    df = pd.read_csv("C:/HR-Worker-Attrition.csv")
    
    # Drop non-informative columns
    df.drop(['EmployeeNumber', 'Over18', 'EmployeeCount', 'StandardHours'], axis=1, inplace=True)
    
    # Encode the goal column
    df['Attrition'] = df['Attrition'].map({'Sure': 1, 'No': 0})
    
    # One-hot encode categorical options
    df = pd.get_dummies(df, drop_first=True)
    
    # Break up options and goal
    X = df.drop('Attrition', axis=1)
    y = df['Attrition']
    
    # Prepare-test cut up
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, stratify=y, random_state=42
    )
    
    # Characteristic scaling
    scaler = StandardScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.rework(X_test)
    
    # Prepare logistic regression mannequin
    mannequin = LogisticRegression(max_iter=1000)
    mannequin.match(X_train_scaled, y_train)
    
    # Predict on check information
    y_pred = mannequin.predict(X_test_scaled)
    
    # Predict chances for the optimistic class
    y_prob = mannequin.predict_proba(X_test_scaled)[:, 1]
    
    
    # Confusion matrix and classification report
    conf_matrix = confusion_matrix(y_test, y_pred)
    report = classification_report(y_test, y_pred)
    
    # Show outcomes
    print("Confusion Matrix:n", conf_matrix)
    print("nClassification Report:n", report)
    

    Confusion Matrix and Classification Report

    Picture by Creator

    From the above classification report, we observe that the accuracy is 86%. Nonetheless, the recall for ‘1’ (attrition = Sure, which means the worker left the job) is 0.34, indicating that the mannequin appropriately recognized solely 34% of workers who left the job.

    The recall for ‘0’ (attrition = No, which means the worker stayed within the job) is 0.96, indicating that the mannequin appropriately recognized 96% of workers who stayed.

    This occurs resulting from an imbalanced dataset. Accuracy may be deceptive right here.

    Does this imply we have to change our algorithm? No.

    We have to change the best way we consider our mannequin, and one of the best ways to guage classification fashions with an imbalanced dataset is ROC AUC.


    Now we all know that there’s one other methodology to guage classification fashions, i.e., ROC AUC. However earlier than exploring ROC AUC, let’s have a transparent concept of what has occurred to date.

    We utilized Logistic Regression on the IBM HR dataset, and the mannequin gave us a likelihood rating for every worker, representing how probably they’re to depart the job.

    Picture by Creator

    Once we generate a confusion matrix and classification report, they’re primarily based on a threshold, which by default is 0.5.

    If the anticipated likelihood is larger than 0.5, the worker is taken into account to have left the job; if the likelihood is smaller than 0.5, the worker is taken into account to have stayed.

    From this, we obtained an accuracy of 86%, however recall was solely 34%. We noticed that accuracy is deceptive, so we determined to guage the mannequin utilizing ROC AUC.


    ROC AUC

    First, we are going to focus on the Receiver Working Attribute (ROC) curve.

    We get the ROC curve by plotting the True Optimistic Price versus False Optimistic Price.

    We already know that the classification report is predicated on a single threshold, However the ROC curve is generated by calculating True Optimistic Price (TPR) and False Optimistic Price (FPR) in any respect doable thresholds after which plotting them.

    Let’s take a pattern dataset and see how we generate the ROC curve from it.

    Picture by Creator

    Now, for the above information, we calculate TPR and FPR on the doable thresholds after which plot them.

    What are Doable Thresholds?

    To generate the ROC curve, we don’t must calculate the TPR and FPR at each worth between 0 and 1.

    As an alternative, we use the anticipated chances from the dataset and one worth above the utmost predicted likelihood (so all predictions are unfavorable, beginning the curve at (0,0)) and one under the minimal predicted likelihood as thresholds (so all predictions are optimistic, ending the curve at (1,1)).

    Why not each quantity between 0 and 1 as Threshold?

    Contemplate our pattern information. We’ve a predicted likelihood of 0.6592, and we are going to use that as a threshold to calculate TPR and FPR.

    Now, between 0.6592 and 0.8718 the TPR and FPR stay the identical, they usually solely change as soon as the edge crosses a predicted likelihood.

    That’s the reason we use distinctive predicted chances as thresholds to generate the ROC curve.

    Now, primarily based on our pattern information, let’s generate the ROC curve and see what we are able to observe.

    To generate the ROC curve, we have to calculate the TPR and FPR.

    [
    text{True Positive Rate (TPR)} = frac{text{True Positives (TP)}}{text{True Positives (TP)} + text{False Negatives (FN)}}
    ]

    The True Optimistic Price (TPR) can also be referred to as Recall.

    [
    text{False Positive Rate (FPR)} = frac{text{False Positives (FP)}}{text{False Positives (FP)} + text{True Negatives (TN)}}
    ]

    The thresholds we’re going to use for this pattern information to calculate TPR and FPR are {1, 0.9799, 0.9709, 0.8737, 0.8718, 0.6592, 0.6337, 0}.

    Let’s calculate TPR and FPR at every threshold.

    [
    begin{aligned}
    mathbf{At threshold 0.9799:} & [4pt]
    mathrm{True Positives (TP)} &= 1,quad mathrm{False Negatives (FN)} = 2,
    mathrm{False Positives (FP)} &= 0,quad mathrm{True Negatives (TN)} = 3[6pt]
    mathrm{TPR} &= frac{mathrm{TP}}{mathrm{TP}+mathrm{FN}} = frac{1}{1+2} = frac{1}{3} approx 0.33[6pt]
    mathrm{FPR} &= frac{mathrm{FP}}{mathrm{FP}+mathrm{TN}} = frac{0}{0+3} = 0[6pt]
    Rightarrow (mathrm{FPR},,mathrm{TPR}) &= (0,,0.33)
    finish{aligned}
    ]

    This fashion, we calculate TPR and FPR at every threshold.

    Picture by Creator

    Now, let’s plot TPR versus FPR to acquire the ROC curve.

    Picture by Creator

    That is how the ROC curve is generated. Since we thought-about solely a 6-point pattern, it’s troublesome to watch and interpret the curve clearly. The primary purpose right here is to grasp how a ROC curve is generated.

    Now we have to interpret the ROC curve, and for that we are going to generate the ROC curve utilizing Python on our dataset.

    Code:

    # Compute ROC curve and AUC
    fpr, tpr, thresholds = roc_curve(y_test, y_prob)
    roc_auc = auc(fpr, tpr)
    
    # Print AUC
    print(f"AUC: {roc_auc:.2f}")
    
    # Plot ROC curve
    plt.determine(figsize=(6,6))
    plt.plot(fpr, tpr, label=f"ROC curve (AUC = {roc_auc:.2f})", linewidth=2)
    plt.plot([0,1], [0,1], 'k--', label="Random guess (AUC = 0.5)")
    plt.xlim([0,1])
    plt.ylim([0,1.05])
    plt.xlabel("False Optimistic Price (FPR)")
    plt.ylabel("True Optimistic Price (TPR)")
    plt.title("ROC Curve - Logistic Regression (HR Dataset)")
    plt.legend(loc="decrease proper")
    plt.grid(True)
    plt.present()
    

    Plot:

    Picture by Creator

    Let’s see what we are able to interpret from the ROC curve alone, regardless of AUC, since we are going to focus on AUC later.

    Within the above plot, the y-axis represents the True Optimistic Price, which implies what number of precise positives the mannequin appropriately identifies, and the x-axis represents the False Optimistic Price, which implies what number of false positives it generates.

    Within the ROC curve, we observe how the mannequin behaves by various thresholds. We wish the True Optimistic Price to be as excessive as doable whereas protecting the False Optimistic Price low, which implies the curve ought to rise towards the top-left nook.

    If the curve is close to or alongside the diagonal, the mannequin is actually making random guesses, and its efficiency isn’t passable.

    If the curve lies under the diagonal, the mannequin’s efficiency could be very poor.

    This fashion, we are able to get an concept of the mannequin’s efficiency throughout varied thresholds.

    Now let’s focus on about Space Below the Curve (AUC).

    We’ve already seen that, for our information AUC is 0.81.

    Picture by Creator

    An AUC of 0.81 signifies that in the event you decide one worker who left and one other who stayed, there’s an 81% likelihood that the mannequin assigns a better likelihood to the worker who left.

    Now, let’s use the pattern dataset to grasp how AUC is calculated.

    Now as soon as once more we return to the ROC curve that we generated utilizing our pattern information.

    Picture by Creator

    The shaded areas within the above plot represents the AUC.

    Now let’s proceed with calculation of AUC.

    From level (0.00, 0.33) to (0.33, 0.33), the world beneath the curve is represented by the orange rectangle.

    From level (0.33, 0.33) to (0.67, 0.33), the world beneath the curve is represented by the inexperienced rectangle.

    From level (0.67, 0.33) to (1.00, 0.33), the world beneath the curve is represented by the purple rectangle.

    Now to search out the AUC, we have to calculate the areas of rectangles and add them.

    $$
    textual content{Orange Rectangle: } l occasions b = 0.33 occasions 0.33 = 0.11
    $$

    $$
    textual content{Inexperienced Rectangle: } l occasions b = 0.34 occasions 0.33 = 0.11
    $$

    $$
    textual content{Purple Rectangle: } l occasions b = 0.33 occasions 0.33 = 0.11
    $$

    $$
    textual content{Whole AUC} = 0.11 + 0.11 + 0.11 = 0.33
    $$

    This fashion we calculate AUC.

    Within the above pattern, we are able to additionally discover the world with out dividing it into point-by-point segments, however in the actual world we don’t get to see such ROC curves.

    Now let’s take into account an instance ROC curve that’s just like real-world instances and calculate the AUC.

    Picture by Creator

    Now let’s discover AUC for this ROC curve.

    Right here we now have three segments. Two of them are trapezoids and one seems to be like a triangle. Nonetheless, we don’t use separate formulation for every form, since rectangles and triangles are not often fashioned.

    We solely use the trapezoid space components.

    $$
    textual content{Space} = tfrac{1}{2} occasions (y_1 + y_2) occasions (x_2 – x_1)
    $$

    Now, utilizing this components, let’s discover the AUC.

    $$
    textual content{Section 1: } (0.0,0.0) ;rightarrow; (0.2,0.4)
    textual content{Space} = tfrac{1}{2} occasions (0.0 + 0.4) occasions (0.2 – 0.0) = 0.04
    $$

    Right here, the trapezoid components routinely reduces to the components for the world of a triangle.

    $$
    textual content{Section 2: } (0.2,0.4) ;rightarrow; (0.6,0.8)
    textual content{Space} = tfrac{1}{2} occasions (0.4 + 0.8) occasions (0.6 – 0.2) = 0.24
    $$

    $$
    textual content{Section 3: } (0.6,0.8) ;rightarrow; (1.0,1.0)
    textual content{Space} = tfrac{1}{2} occasions (0.8 + 1.0) occasions (1.0 – 0.6) = 0.36
    $$

    $$
    textual content{Whole AUC} = 0.04 + 0.24 + 0.36 = 0.64
    $$

    That is how the AUC is calculated. We now perceive how we acquired an AUC of 0.81 for our HR dataset.


    However there’s additionally a second methodology to calculate AUC.

    Once more, we return to our pattern dataset.

    Picture by Creator

    Positives (1’s): [0.9799, 0.6592, 0.6337]

    Negatives (0’s): [0.9709, 0.8737, 0.8718]

    Right here we now have whole 9 positive-negative pairs.

    Now we examine every optimistic with every unfavorable to see whether or not the optimistic is ranked larger or the unfavorable.

    $$
    0.9799 ;(textual content{optimistic}) > 0.9709 ;(textual content{unfavorable}) ;;;Rightarrow;; textual content{optimistic ranked larger}
    $$

    $$
    0.9799 ;(textual content{optimistic}) > 0.8737 ;(textual content{unfavorable}) ;;;Rightarrow;; textual content{optimistic ranked larger}
    $$

    $$
    0.9799 ;(textual content{optimistic}) > 0.8718 ;(textual content{unfavorable}) ;;;Rightarrow;; textual content{optimistic ranked larger}
    $$

    $$
    0.6592 ;(textual content{optimistic}) < 0.9709 ;(textual content{unfavorable}) ;;;Rightarrow;; textual content{optimistic ranked decrease}
    $$

    $$
    0.6592 ;(textual content{optimistic}) < 0.8737 ;(textual content{unfavorable}) ;;;Rightarrow;; textual content{optimistic ranked decrease}
    $$

    $$
    0.6592 ;(textual content{optimistic}) < 0.8718 ;(textual content{unfavorable}) ;;;Rightarrow;; textual content{optimistic ranked decrease}
    $$

    $$
    0.6337 ;(textual content{optimistic}) < 0.9709 ;(textual content{unfavorable}) ;;;Rightarrow;; textual content{optimistic ranked decrease}
    $$

    $$
    0.6337 ;(textual content{optimistic}) < 0.8737 ;(textual content{unfavorable}) ;;;Rightarrow;; textual content{optimistic ranked decrease}
    $$

    $$
    0.6337 ;(textual content{optimistic}) < 0.8718 ;(textual content{unfavorable}) ;;;Rightarrow;; textual content{optimistic ranked decrease}
    $$

    $$
    textual content{Appropriately ranked pairs} = 3, quad textual content{Whole pairs} = 9, quad textual content{AUC} = tfrac{3}{9} = 0.33
    $$

    That is referred to as as Rating Technique for AUC.

    We obtained the identical worth of 0.33 utilizing each strategies for pattern information.

    We are able to perceive that

    $$
    textual content{AUC} = frac{textual content{Variety of appropriately ranked pairs}}{textual content{Whole variety of pairs}}
    $$

    We are able to interpret AUC because the likelihood {that a} randomly chosen optimistic is ranked larger than a randomly chosen unfavorable.


    Now that we now have an concept of find out how to generate the ROC curve and calculate AUC, let’s focus on the significance of ROC-AUC.

    We used ROC-AUC once we discovered that accuracy is deceptive. However as a substitute of ROC-AUC, we might ask: why not run a loop over all threshold values, calculate accuracy and different metrics, after which choose the very best threshold?

    Sure, that’s doable. Nonetheless, once we examine two fashions, we can’t examine them primarily based on their finest thresholds, since totally different fashions might have totally different finest thresholds.

    ROC-AUC provides us a single quantity that summarizes a mannequin’s efficiency and permits comparability throughout totally different fashions.

    One other level is that the very best threshold is determined by the metric we select.

    The “finest” threshold modifications relying on whether or not we optimize for accuracy, precision, recall, or F1-score. ROC-AUC is threshold-independent, making it a extra normal measure of mannequin high quality.

    Lastly, ROC-AUC captures the rating means of the mannequin, which makes it particularly helpful for imbalanced datasets.


    Dataset

    The IBM HR Analytics Employee Attrition dataset used on this article is from Kaggle, which is licensed beneath CC0 (Public Area), making it secure to be used on this evaluation and publication.


    I hope you discovered this text useful.

    Be happy to share your ideas.

    Thanks for studying!



    Source link

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleDe-risking investment in AI agents
    Next Article Deploying AI Safely and Responsibly
    ProfitlyAI
    • Website

    Related Posts

    Artificial Intelligence

    Creating AI that matters | MIT News

    October 21, 2025
    Artificial Intelligence

    Scaling Recommender Transformers to a Billion Parameters

    October 21, 2025
    Artificial Intelligence

    Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know

    October 21, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Top Posts

    How do you teach an AI model to give therapy?

    April 3, 2025

    Att säga ”Snälla” och ”Tack” till ChatGPT kostar OpenAI miljontals dollar i datorkraft

    April 21, 2025

    OpenAI kommer att tillåta erotik för vuxna användare

    October 16, 2025

    Economic Cycle Synchronization with Dynamic Time Warping

    June 26, 2025

    Conversational AI Guide – Types, Advantages, Challenges & Use Cases

    April 7, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    Most Popular

    Fine-Tune Your Topic Modeling Workflow with BERTopic

    August 12, 2025

    Google May Lose Chrome, And OpenAI’s First in Line to Grab It

    April 25, 2025

    Boost 2-Bit LLM Accuracy with EoRA

    May 15, 2025
    Our Picks

    Creating AI that matters | MIT News

    October 21, 2025

    Scaling Recommender Transformers to a Billion Parameters

    October 21, 2025

    Hidden Gems in NumPy: 7 Functions Every Data Scientist Should Know

    October 21, 2025
    Categories
    • AI Technology
    • AI Tools & Technologies
    • Artificial Intelligence
    • Latest AI Innovations
    • Latest News
    • Privacy Policy
    • Disclaimer
    • Terms and Conditions
    • About us
    • Contact us
    Copyright © 2025 ProfitlyAI All Rights Reserved.

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