{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open in Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/justmarkham/scikit-learn-tips/master?filepath=notebooks%2F32_multiclass_auc.ipynb)\n", "\n", "[![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/justmarkham/scikit-learn-tips/blob/master/notebooks/32_multiclass_auc.ipynb)\n", "\n", "# 🤖⚡ scikit-learn tip #32 ([video](https://www.youtube.com/watch?v=-s-KdkYmCaA&list=PL5-da3qGB5ID7YYAqireYEew2mWVvgmj6&index=32))\n", "\n", "AUC is an excellent evaluation metric for binary classification, especially if you have class imbalance.\n", "\n", "New in scikit-learn 0.22: AUC can be used with multiclass problems! Supports \"one-vs-one\" and \"one-vs-rest\" strategies.\n", "\n", "See example 👇" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from sklearn.datasets import load_wine\n", "X, y = load_wine(return_X_y=True)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# only keep two features in order to make this problem harder\n", "X = X[:, 0:2]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from sklearn.linear_model import LogisticRegression\n", "clf = LogisticRegression()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiclass AUC with train/test split" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "from sklearn.metrics import roc_auc_score" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\n", "clf.fit(X_train, y_train)\n", "y_score = clf.predict_proba(X_test)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9399801587301587" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# use 'ovo' (One-vs-One) or 'ovr' (One-vs-Rest)\n", "roc_auc_score(y_test, y_score, multi_class='ovo')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Multiclass AUC with cross-validation" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import cross_val_score" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.9086960878627546" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# use 'roc_auc_ovo' (One-vs-One) or 'roc_auc_ovr' (One-vs-Rest)\n", "cross_val_score(clf, X, y, cv=5, scoring='roc_auc_ovo').mean()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Want more tips? [View all tips on GitHub](https://github.com/justmarkham/scikit-learn-tips) or [Sign up to receive 2 tips by email every week](https://scikit-learn.tips) 💌\n", "\n", "© 2020 [Data School](https://www.dataschool.io). All rights reserved." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.4" } }, "nbformat": 4, "nbformat_minor": 4 }