Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

roc curve python

import sklearn.metrics as metrics
# calculate the fpr and tpr for all thresholds of the classification
probs = model.predict_proba(X_test)
preds = probs[:,1]
fpr, tpr, threshold = metrics.roc_curve(y_test, preds)
roc_auc = metrics.auc(fpr, tpr)

# method I: plt
import matplotlib.pyplot as plt
plt.title('Receiver Operating Characteristic')
plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % roc_auc)
plt.legend(loc = 'lower right')
plt.plot([0, 1], [0, 1],'r--')
plt.xlim([0, 1])
plt.ylim([0, 1])
plt.ylabel('True Positive Rate')
plt.xlabel('False Positive Rate')
plt.show()

# method II: ggplot
from ggplot import *
df = pd.DataFrame(dict(fpr = fpr, tpr = tpr))
ggplot(df, aes(x = 'fpr', y = 'tpr')) + geom_line() + geom_abline(linetype = 'dashed')
Comment

plotting roc curve

# Import necessary modules
from sklearn.metrics import roc_curve

# Compute predicted probabilities: y_pred_prob
y_pred_prob = logreg.predict_proba(X_test)[:,1]

# Generate ROC curve values: fpr, tpr, thresholds
fpr, tpr, thresholds = roc_curve(y_test, y_pred_prob)

# Plot ROC curve
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr, tpr)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve')
plt.show()
Comment

roc auc score plotting

import scikitplot as skplt
import matplotlib.pyplot as plt

y_true = # ground truth labels
y_probas = # predicted probabilities generated by sklearn classifier
skplt.metrics.plot_roc_curve(y_true, y_probas)
plt.show()
Comment

roc curve

# Import roc_curve
from sklearn.metrics import roc_curve

# Generate ROC curve values: fpr, tpr, thresholds
fpr, tpr, thresholds = roc_curve(y_test, y_pred_probs)

plt.plot([0, 1], [0, 1], 'k--')

# Plot tpr against fpr
plt.plot(fpr, tpr)
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC Curve for Diabetes Prediction')
plt.show()
Comment

roc curve

y_pred_logreg_proba = classifier_logreg.predict_proba(X_test)
from sklearn.metrics import roc_curve
fpr, tpr, thresholds = roc_curve(y_test, y_pred_logreg_proba[:,1])
plt.figure(figsize=(6,4))
plt.plot(fpr,tpr,'-g',linewidth=1)
plt.plot([0,1], [0,1], 'k--' )
plt.title('ROC curve for Logistic Regression Model')
plt.xlabel("False Positive Rate")
plt.ylabel('True Positive Rate')
plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: check is string is nan python 
Python :: get every item but the last item of python list 
Python :: python variable 
Python :: compare dates python 
Python :: Error: The file/path provided (flaskr) does not appear to exist. Please verify the path is correct. If app is not on PYTHONPATH, ensure the extension is .py 
Python :: python read values from file 
Python :: remove columns from dataframe 
Python :: python split string to sentences 
Python :: python next item in list 
Python :: python sockets 
Python :: python compare floats 
Python :: python split lines 
Python :: python index 2d array 
Python :: Python get all keys from nested dictionary 
Python :: django reverse function 
Python :: find max in a dataframe 
Python :: append to list py 
Python :: python get element from dictionary 
Python :: .describe() python 
Python :: find all indices of element in string python 
Python :: drop row with condition dataframe 
Python :: python easygui 
Python :: Read JSON files with automatic schema inference 
Python :: Handling categorical feature 
Python :: __delattr__ python 
Python :: pyautogui tab key 
Python :: python red table from pdf 
Python :: how to make a python file that prints out a random element from a list 
Python :: remove prefix in python 3.6 
Python :: how to know if a key is in a dictionary python 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =