0% found this document useful (0 votes)
4 views2 pages

Diabetes Data Analysis: AUC & Confusion Matrix

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Diabetes Data Analysis: AUC & Confusion Matrix

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment 9

WAP to construct for a diabetes data set.


a. AUC-ROC curve
b. Confusion matrix

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from sklearn.linear_model import LogisticRegression
from [Link] import confusion_matrix, roc_curve, auc, ConfusionMatrixDisplay
import [Link] as plt
import seaborn as sns

# Load the dataset


file_path = '[Link]' # Replace with the correct path if different
data = pd.read_csv(file_path)

# Assuming the target variable is named 'Outcome' and it's binary (0/1)
X = [Link](columns='Outcome') # Features
y = data['Outcome'] # Target

# Split the dataset into training and test sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42, stratify=y)

# Feature scaling (standardizing the data)


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = [Link](X_test)

# Train a Logistic Regression model


model = LogisticRegression()
[Link](X_train, y_train)

# Make predictions on the test set


y_pred = [Link](X_test)

# Calculate the confusion matrix


conf_matrix = confusion_matrix(y_test, y_pred)

# Mark the TP, TN, FP, FN in the confusion matrix


group_names = ['True Negatives (TN)', 'False Positives (FP)', 'False Negatives (FN)',
'True Positives (TP)']
group_counts = ["{0:0.0f}".format(value) for value in conf_matrix.flatten()]
group_percentages = ["{0:.2%}".format(value) for value in
conf_matrix.flatten()/[Link](conf_matrix)]

labels = [f"{name}\n{count}\n{percentage}" for name, count, percentage in


zip(group_names, group_counts, group_percentages)]
labels = [Link](labels).reshape(2, 2)

# Plot the confusion matrix with labels


[Link](figsize=(8, 6))
[Link](conf_matrix, annot=labels, fmt='', cmap='Blues', cbar=False,
annot_kws={"size": 12})
[Link]('Confusion Matrix with TP, TN, FP, FN')
[Link]('Predicted Label')
[Link]('True Label')
[Link]()

# Predict probabilities for the test set


y_prob = model.predict_proba(X_test)[:, 1]

# Compute the ROC curve


fpr, tpr, thresholds = roc_curve(y_test, y_prob)

# Compute the AUC


roc_auc = auc(fpr, tpr)

# Plot the ROC curve


[Link]()
[Link](fpr, tpr, color='navy', lw=2, label=f'ROC curve (area = {roc_auc:.2f})')
plt.fill_between(fpr, tpr, alpha=0.3, color='orange')
[Link]([0, 1], [0, 1], color='navy', lw=2, linestyle='--')
[Link]([0.0, 1.0])
[Link]([0.0, 1.05])
[Link]('False Positive Rate')
[Link]('True Positive Rate')
[Link]('Receiver Operating Characteristic (ROC) Curve')
[Link](loc="lower right")
[Link]()

Common questions

Powered by AI

Feature scaling, specifically standardization, is crucial for logistic regression because it ensures that each feature contributes equally to the distance computations within the algorithm. In the diabetes dataset, features are standardized to have a mean of zero and a standard deviation of one, which prevents features with larger scales from disproportionately influencing the model and helps in achieving faster convergence .

Logistic regression is used for binary classification tasks and assumes a linear relationship between dependent and independent variables via a logit transformation. In the diabetes dataset context, this implies that the underlying decision boundary is linear, which may not adequately capture complex patterns in the data. Limitations include sensitivity to overfitting with small datasets, and the assumption that predictor variables are independent of each other. Therefore, feature engineering or more complex models may be considered if these assumptions are violated .

Stratified sampling ensures that each class is proportionally represented in both the training and testing datasets, which is especially important for imbalanced classes like those in the diabetes dataset. It helps prevent bias towards the majority class and ensures that the model learns a balanced representation of both positive and negative outcomes, improving the model’s generalization ability and evaluation validity .

Generating a seaborn heatmap involves using the confusion matrix data to plot the true positives, true negatives, false positives, and false negatives in a visual format. In the diabetes dataset, the heatmap annotation includes classifying each count with its respective category (e.g., True/False Positives/Negatives) and proportion. This visualization lets analysts quickly assess where the model performs well and where misclassifications occur, enabling easier communication of the model's strengths and weaknesses to stakeholders .

The dataset is split into training and testing sets to evaluate the model's performance on unseen data, which helps in detecting overfitting. In the diabetes dataset, the data is split in a 70-30 ratio, where 70% of the data is used to train the model and 30% is used to test it. Stratification is used to maintain the same proportion of classes in both training and test sets, ensuring that the model learns the class distributions effectively .

The ROC curve provides a comprehensive evaluation by illustrating the trade-off between the true positive rate and false positive rate across various thresholds, unlike accuracy which considers only one threshold. For the diabetes dataset, the ROC curve can reveal the model's performance in distinguishing between classes at different sensitivities and specificities. This is particularly important in medical diagnostics, where the costs of false negatives and false positives are not equal and need careful balancing .

The confusion matrix details the true and false positives and negatives made by the model, highlighting the model's strengths and weaknesses in classifying each class. Insights into specific errors like high false negatives or false positives help in strategizing improvements. For instance, if there is a high false negative rate, it indicates the model is missing positive cases of diabetes, which could be addressed by altering the classification threshold, implementing better feature selection, or using more complex algorithms to enhance sensitivity .

The AUC-ROC curve helps evaluate the classification model by plotting the true positive rate against the false positive rate at various threshold settings, providing a graphical representation of a model's diagnostic ability. The AUC (Area Under the Curve) value summarizes the aggregate performance of the model across all possible classification thresholds. For the diabetes dataset, a higher AUC value indicates better model performance, meaning that the model is good at distinguishing between the positive class (diabetes) and the negative class (non-diabetes).

A confusion matrix is used to evaluate the performance of a classification model by calculating the number of true positives, true negatives, false positives, and false negatives. For a binary classification problem like the diabetes dataset, the confusion matrix is constructed by comparing the true labels of the test set with the predicted labels produced by the model. It is typically a 2x2 matrix, where each cell corresponds to one of the possible outcomes: True Negatives (TN), False Positives (FP), False Negatives (FN), and True Positives (TP).

In logistic regression, predict probabilities for the test set refers to calculating the likelihood that each instance belongs to a particular class, in this case, having diabetes. These probabilities are used to plot the ROC curve and compute the AUC, providing insights into the model's ability to distinguish between classes across thresholds, beyond just binary labels. These probabilities allow for adjusting decision thresholds to align with clinical or operational requirements .

You might also like