Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

A dictionary can be utilized to plot ROC AUC for multinomial logistic regression by first creating a dictionary that stores the results of the logistic regression model. The keys of the dictionary can be set as the different classes, while the values can be a list of the predicted probability scores for each class.

Then, for each class in the dictionary, the true labels can be compared to the predicted probability scores to calculate the ROC AUC score. These scores can then be plotted on a graph to visualize the performance of the model for each class.

Here's an example code snippet to illustrate this process:

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt
import numpy as np

# Generate a multi-class classification dataset
X, y = make_classification(n_samples=1000, n_classes=3, random_state=42)

# Fit a multinomial logistic regression model
clf = LogisticRegression(multi_class='multinomial', solver='lbfgs')
clf.fit(X, y)

# Create a dictionary to store results
results_dict = {}

# Predict probabilities for each class
proba = clf.predict_proba(X)
for i, target_name in enumerate(clf.classes_):
    results_dict[target_name] = proba[:, i]

# Plot ROC AUC for each class
plt.figure(figsize=(8, 6))
for target_name, proba_scores in results_dict.items():
    y_true = [1 if y_val == target_name else 0 for y_val in y]
    roc_auc = roc_auc_score(y_true, proba_scores)
    plt.plot(*zip([0, 0], [1, 1]), linestyle='--', color='gray')
    plt.plot(*zip([0, 1]), *zip([0, 1]), linestyle='--', color='gray')
    plt.plot(*roc_curve(y_true, proba_scores)[:2], label=f'{target_name} (AUC={roc_auc:.2f})')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC AUC for Multinomial Logistic Regression')
plt.legend(loc='best')
plt.show()

In this code snippet, we first generate a multi-class classification dataset using make_classification. We then fit a multinomial logistic regression model using LogisticRegression. We create a dictionary to store the predicted probability scores for each class, and then loop through each class to calculate the ROC AUC score using roc_auc_score. Finally, we plot the ROC AUC curve for each class using plt.plot and display the results using plt.show().