How to plot roc auc curve python
Recipe ObjectiveWhile working on a classification model, we feel a need of a metric which can show us how our model is performing. A metric which can also give a graphical representation of the performance will be very helpful. Show
ROC curve can efficiently give us
the score that how our model is performing in classifing the labels. We can also plot graph between False Positive Rate and True Positive Rate with this ROC(Receiving Operating Characteristic) curve. The area under the ROC curve give is also a metric. Greater the area means better the performance. So this recipe is a short example of how to use ROC and AUC to see the performance of our model.Here we will use it on two models for better understanding. Table of Contents
Get Closer To Your Dream of Becoming a Data Scientist with 70+ Solved End-to-End ML Projects Step 1 - Import the library - GridSearchCv
Here we have imported various modules like: datasets from which we will get the dataset, DecisionTreeClassifier and LogisticRegression which we will use a models, roc_curve and roc_auc_score will be used to get the score and help us to plot the graph, train_test_split will split the data into two parts train and test and plt will be used to plot the graph. Step 2 - Setup the DataHere we have used datasets to load the inbuilt wine dataset and we have created objects X and y to store the data and the target value respectively. Step 3 - Spliting the data and Training the modelThe module train_test_split is used to split the data into two parts, one is train which is used to train the model and the other is test which is used to check how our model is working on unseen data. Here we are passing 0.3 as a parameter in the train_test_split which will split the data such that 30% of data will be in test part and rest 70% will be in the train part. Now
we are creating objects for classifier and training the classifier with the train split of the dataset i.e x_train and y_train. Step 5 - Using the models on test datasetAfter traing the classifier on test dataset, we are using the model to predict the target values for test dataset. We are storing the predicted class by both of the models and we will use it to get the ROC AUC score Step 6 - Creating False and True Positive Rates and printing ScoresWe have to get False Positive Rates and True Postive rates for the Classifiers because these will be used to plot the ROC Curve. This can be done by roc_curve module by passing the test dataset and the predicted data through it. Here we are doing this for both the classifier. Step 7 - Ploting ROC CurvesWe are ploting two ROC Curve as subplots one for DecisionTreeClassifier and another for LogisticRegression. Both have their respective False Positive Rate on X-axis and True Positive Rate on Y-axis. roc_auc_score for DecisionTree: 0.9539141414141414
roc_auc_score for Logistic Regression: 0.9875140291806959
How do you plot a ROC graph in Python?Use the make_classification() method. Split arrays or matrices into random trains, using train_test_split() method. Fit the SVM model according to the given training data, using fit() method. Plot Receiver operating characteristic (ROC) curve, using plot_roc_curve() method.
How do you graph 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!
How do you plot a ROC curve for several models?How to Plot Multiple ROC Curves in Python (With Example). Step 1: Import Necessary Packages. First, we'll import several necessary packages in Python: from sklearn import metrics from sklearn import datasets from sklearn. ... . Step 2: Create Fake Data. ... . Step 3: Fit Multiple Models & Plot ROC Curves.. What is ROC curve in Python?Another common metric is AUC, area under the receiver operating characteristic (ROC) curve. The Reciever operating characteristic curve plots the true positive (TP) rate versus the false positive (FP) rate at different classification thresholds.
|