<jupyter_start><jupyter_text>Demonstration of AUC of ROC<jupyter_code>%matplotlib notebook
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve

sns.set(font_scale=1.5)
sns.set_style("whitegrid", {'grid.linestyle':'--'})<jupyter_output><empty_output><jupyter_text>Here we are going to use the breast cancer data to build a simple logistic regression model<jupyter_code>cancer_data = pd.read_csv("https://raw.githubusercontent.com/changyaochen/MECE4520/master/lectures/lecture_4/breast_cancer_data.csv")
cancer_data["label"] = cancer_data["diagnosis"].apply(lambda x: 0 if x == "B" else 1)
cancer_data.head()
# fit the logistic regression model
lr_model = LogisticRegression(penalty="none", random_state=42)
lr_model.fit(
    X=cancer_data[["radius_mean"]],
    y=cancer_data["label"],
)
# make prediction on the training dataset
prediction_results = pd.DataFrame(
    data={
        "probability": lr_model.predict_proba(cancer_data[["radius_mean"]])[:, 1],
        "label": cancer_data["label"],
    }
)

# take a look some randome samples
prediction_results.sample(10, random_state=123).sort_values("probability")
# calculate the values needed for the auc of roc
fpr, tpr, thresholds = roc_curve(
    y_true=prediction_results["label"],
    y_score=prediction_results["probability"],
)

roc_data = pd.DataFrame(
    data={
        "fpr": fpr,
        "tpr": tpr,
        "thresholds": thresholds,
    },
)

roc_data.tail()
# make plot
plt.figure()
# plot the ROC
sns.lineplot(x=fpr, y=tpr)
# plot the diagonal line
sns.lineplot(x=[0, 1], y=[0, 1], linestyle="--")


plt.gca().set_aspect("equal")
plt.gca().set_xlabel("False Positive Rate")
plt.gca().set_ylabel("True Positive Rate")
plt.gca().set_xlim([-.1, 1.1])
plt.gca().set_ylim([-.1, 1.1])
plt.tight_layout()
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup