How to plot roc curve for random forest python

Note

Click here to download the full example code or to run this example in your browser via Binder

Scikit-learn defines a simple API for creating visualizations for machine learning. The key features of this API is to allow for quick plotting and visual adjustments without recalculation. In this example, we will demonstrate how to use the visualization API by comparing ROC curves.

Load Data and Train a SVC¶

First, we load the wine dataset and convert it to a binary classification problem. Then, we train a support vector classifier on a training dataset.

import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import RocCurveDisplay
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split

X, y = load_wine(return_X_y=True)
y = y == 2

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
svc = SVC(random_state=42)
svc.fit(X_train, y_train)

SVC(random_state=42)

In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Plotting the ROC Curve¶

Next, we plot the ROC curve with a single call to sklearn.metrics.RocCurveDisplay.from_estimator. The returned svc_disp object allows us to continue using the already computed ROC curve for the SVC in future plots.

How to plot roc curve for random forest python

Training a Random Forest and Plotting the ROC Curve¶

We train a random forest classifier and create a plot comparing it to the SVC ROC curve. Notice how svc_disp uses plot to plot the SVC ROC curve without recomputing the values of the roc curve itself. Furthermore, we pass alpha=0.8 to the plot functions to adjust the alpha values of the curves.

How to plot roc curve for random forest python

Total running time of the script: ( 0 minutes 0.142 seconds)

Gallery generated by Sphinx-Gallery

Note

Click here to download the full example code or to run this example in your browser via Binder

Scikit-learn defines a simple API for creating visualizations for machine learning. The key features of this API is to allow for quick plotting and visual adjustments without recalculation. In this example, we will demonstrate how to use the visualization API by comparing ROC curves.

Load Data and Train a SVC¶

First, we load the wine dataset and convert it to a binary classification problem. Then, we train a support vector classifier on a training dataset.

Out:

Plotting the ROC Curve¶

Next, we plot the ROC curve with a single call to sklearn.metrics.plot_roc_curve. The returned svc_disp object allows us to continue using the already computed ROC curve for the SVC in future plots.

How to plot roc curve for random forest python

Training a Random Forest and Plotting the ROC Curve¶

We train a random forest classifier and create a plot comparing it to the SVC ROC curve. Notice how svc_disp uses plot to plot the SVC ROC curve without recomputing the values of the roc curve itself. Furthermore, we pass alpha=0.8 to the plot functions to adjust the alpha values of the curves.

How to plot roc curve for random forest python

Total running time of the script: ( 0 minutes 0.567 seconds)

Estimated memory usage: 12 MB

Gallery generated by Sphinx-Gallery

Can we use ROC curve for random forest?

randomForest and many of its implementations can output probabilities or pseudo-probabilities to construct ROC curves with. It is fairly common.

How do you plot a ROC curve in Python?

How to Plot a ROC Curve in Python (Step-by-Step).
Step 1: Import Necessary Packages. First, we'll import the packages necessary to perform logistic regression in Python: import pandas as pd import numpy as np from sklearn. ... .
Step 2: Fit the Logistic Regression Model. ... .
Step 3: Plot the ROC Curve. ... .
Step 4: Calculate the AUC..

How do you plot a ROC curve?

To plot the ROC curve, we need to calculate the TPR and FPR for many different thresholds (This step is included in all relevant libraries as scikit-learn ). For each threshold, we plot the FPR value in the x-axis and the TPR value in the y-axis. We then join the dots with a line. That's it!