0% found this document useful (0 votes)
7 views3 pages

Comparing AUC and Accuracy Scores

The document outlines a practical implementation of a Decision Tree Classifier using the Iris dataset. It includes steps for data loading, model training, prediction, evaluation metrics such as accuracy, confusion matrix, classification report, k-fold cross-validation, and AUC score calculation. The model achieved an accuracy of 1.0 on the test set and an average cross-validation accuracy of approximately 0.95.
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)
7 views3 pages

Comparing AUC and Accuracy Scores

The document outlines a practical implementation of a Decision Tree Classifier using the Iris dataset. It includes steps for data loading, model training, prediction, evaluation metrics such as accuracy, confusion matrix, classification report, k-fold cross-validation, and AUC score calculation. The model achieved an accuracy of 1.0 on the test set and an average cross-validation accuracy of approximately 0.95.
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

Practical 9

In [1]: # Import necessary libraries


import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score
from [Link] import DecisionTreeClassifier
from [Link] import accuracy_score, confusion_matrix, classification_report, roc_auc_score
from [Link] import load_iris
from [Link] import LabelBinarizer
from sklearn.model_selection import cross_val_score

# Load the Iris dataset


iris = load_iris()
X = [Link]
y = [Link]

# Split the dataset into training and testing sets (80% training, 20% testing)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize the Decision Tree Classifier


dt_classifier = DecisionTreeClassifier(random_state=42)

# Train the model


dt_classifier.fit(X_train, y_train)

# Make predictions on the test set


y_pred = dt_classifier.predict(X_test)

# Evaluate the model


accuracy = accuracy_score(y_test, y_pred)
conf_matrix = confusion_matrix(y_test, y_pred)

# Print confusion matrix


print("Confusion Matrix:")
print(conf_matrix)

# Calculate metrics for each class


# Accuracy
print("\nAccuracy: ", accuracy)

# Error
error = 1 - accuracy
print("Error: ", error)

# Classification report
print("\nClassification Report:")
print(classification_report(y_test, y_pred))

# Perform k-fold cross-validation (let's use 5-fold cross-validation)


cross_val_accuracy = cross_val_score(dt_classifier, X, y, cv=5, scoring='accuracy')

# Print the k-fold cross-validation results


print("\nCross-Validation Accuracy Scores (5 folds):", cross_val_accuracy)
print("Average Cross-Validation Accuracy: ", [Link](cross_val_accuracy))

# AUC - multi-class AUC using One-vs-Rest (OvR) strategy


# For multi-class AUC, we need to binarize the labels
y_test_bin = LabelBinarizer().fit_transform(y_test)
y_pred_bin = LabelBinarizer().fit_transform(y_pred)
auc = roc_auc_score(y_test_bin, y_pred_bin, average='weighted', multi_class='ovr')

# Print the AUC score


print("\nAUC Score: ", auc)
Confusion Matrix:
[[10 0 0]
[ 0 9 0]
[ 0 0 11]]

Accuracy: 1.0
Error: 0.0

Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 10


1 1.00 1.00 1.00 9
2 1.00 1.00 1.00 11

accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30

Cross-Validation Accuracy Scores (5 folds): [0.96666667 0.96666667 0.9 0.93333333 1. ]


Average Cross-Validation Accuracy: 0.9533333333333334

AUC Score: 1.0

Common questions

Powered by AI

A high AUC score is crucial as it measures the model's ability to differentiate between classes, with a higher score indicating better performance. It provides insight into the model's capability to handle classification tasks across multiple thresholds. The Decision Tree Classifier in this case achieved an AUC score of 1.0, signifying its exceptional performance in distinguishing between the classes in the Iris dataset .

The LabelBinarizer was used to convert multi-class labels into a binary format necessary for calculating multi-class AUC with the OvR strategy. This transformation is vital as AUC requires binary inputs; thus, the LabelBinarizer facilitated the computation by encoding each class separately. Its importance lies in enabling the use of AUC for evaluating the decision tree's multi-class classification performance, which was perfect with a score of 1.0 .

The classification report is significant as it summarizes precision, recall, F1-score, and support for each class, providing a detailed evaluation of model performance and potential biases. For the Decision Tree Classifier on the Iris dataset, the classification report indicated perfect scores for precision, recall, and F1-score across all classes, reflecting outstanding classifier performance without bias .

K-fold cross-validation improves classifier assessment by partitioning the data into k subsets, training on k-1 subsets, and testing on the remaining one, thereby reducing overfitting and providing a more robust estimate of model performance. In this case, a 5-fold cross-validation was used, yielding accuracy scores of [0.9667, 0.9667, 0.9, 0.9333, 1.0], with an average cross-validation accuracy of 0.9533, indicating consistent and reliable model performance across different data partitions .

The train-test split affects model evaluation by determining the size of the dataset used for training versus testing, influencing model learning and performance assessment. A larger training set generally yields better model performance, whereas a larger test set provides more reliable evaluation. In this scenario, an 80%-20% train-test split was used, balancing sufficient data for training the Decision Tree Classifier and reliable evaluation of its performance .

The One-vs-Rest (OvR) strategy is significant for calculating multi-class AUC because it simplifies multi-class problems into binary classification by considering each class against all others. This approach allows for evaluating the model's ability to distinguish between each class when compared to the rest, thus facilitating a comprehensive assessment of classifier performance across all classes. In this context, the AUC score was a perfect 1.0, indicating excellent discriminatory power in each classification scenario .

The confusion matrix for the Decision Tree model on the Iris dataset revealed perfect classification, with actual and predicted counts aligned across all three classes: 10 for class 0, 9 for class 1, and 11 for class 2, resulting in no misclassifications. This indicates that the model perfectly distinguished among the various species of iris flowers .

Precision, recall, and F1-score are vital for evaluating a classification model by providing insights into different aspects: precision measures the accuracy of positive predictions, recall (sensitivity) measures the ability to identify all positive instances, and F1-score is the harmonic mean of precision and recall, balancing both metrics. The Decision Tree Classifier achieved perfect scores of 1.0 for precision, recall, and F1-score across all classes, indicating flawless performance on the Iris dataset .

Performing k-fold cross-validation reveals the robustness and reliability of the Decision Tree Classifier by testing it on multiple data partitions, minimizing overfitting risks. This approach contributes to understanding the model's generalization ability by providing an average performance measure across different permutations of train-test splits. The Decision Tree Classifier showed strong generalization ability with a cross-validation accuracy of 0.9533, suggesting it performs well across diverse data subsets .

Decision tree classifiers are advantageous as they are easy to interpret, require little data preprocessing, and handle both numerical and categorical data. The model's performance on the Iris dataset was evaluated using accuracy, error rate, a confusion matrix, classification report, cross-validation accuracy, and AUC score. The model achieved a perfect accuracy score of 1.0 on the test data and an average cross-validation accuracy of 0.9533, which indicates high reliability. The AUC score was also 1.0, reflecting strong discrimination ability among classes .

You might also like