Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

COLLECTING

import numpy as np

np.random.seed(2018)

from sklearn.datasets import load_breast_cancer
from sklearn.metrics import roc_auc_score, roc_curve
from sklearn.model_selection import train_test_split

from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier
import matplotlib
import matplotlib.pyplot as plt

data = load_breast_cancer()

X = data.data
y = data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=17)

# Naive Bayes Classifier
nb_clf = GaussianNB()
nb_clf.fit(X_train, y_train)
nb_prediction_proba = nb_clf.predict_proba(X_test)[:, 1]

# Ranodm Forest Classifier
rf_clf = RandomForestClassifier(n_estimators=20)
rf_clf.fit(X_train, y_train)
rf_prediction_proba = rf_clf.predict_proba(X_test)[:, 1]

# Multi-layer Perceptron Classifier
mlp_clf = MLPClassifier(alpha=1, hidden_layer_sizes=150)
mlp_clf.fit(X_train, y_train)
mlp_prediction_proba = mlp_clf.predict_proba(X_test)[:, 1]


def roc_curve_and_score(y_test, pred_proba):
    fpr, tpr, _ = roc_curve(y_test.ravel(), pred_proba.ravel())
    roc_auc = roc_auc_score(y_test.ravel(), pred_proba.ravel())
    return fpr, tpr, roc_auc


plt.figure(figsize=(8, 6))
matplotlib.rcParams.update({'font.size': 14})
plt.grid()
fpr, tpr, roc_auc = roc_curve_and_score(y_test, rf_prediction_proba)
plt.plot(fpr, tpr, color='darkorange', lw=2,
         label='ROC AUC={0:.3f}'.format(roc_auc))
fpr, tpr, roc_auc = roc_curve_and_score(y_test, nb_prediction_proba)
plt.plot(fpr, tpr, color='green', lw=2,
         label='ROC AUC={0:.3f}'.format(roc_auc))
fpr, tpr, roc_auc = roc_curve_and_score(y_test, mlp_prediction_proba)
plt.plot(fpr, tpr, color='crimson', lw=2,
         label='ROC AUC={0:.3f}'.format(roc_auc))
plt.plot([0, 1], [0, 1], color='navy', lw=1, linestyle='--')
plt.legend(loc="lower right")
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('1 - Specificity')
plt.ylabel('Sensitivity')
plt.show()
Comment

PREVIOUS NEXT
Code Example
Python :: password protected mongo server 
Python :: ascending order in python using bubble sort 
Python :: comprehensive python cheat sheet 
Python :: datetime german format python 
Python :: total keywords in python 
Python :: python remove xa0 
Python :: select features and label from df 
Python :: gpg --verify Python-3.6.2.tgz.asc 
Python :: python which __divs__ are there 
Python :: Python Record live streams (TS FILES) 
Python :: requests session 
Python :: get value of a list of dictionary matching key 
Python :: what does << do in python 
Python :: pandas merge validate 
Python :: run python in background ubuntu 
Python :: python polymorphism 
Python :: join paths in python 
Python :: scikit decision tree regressor 
Python :: python check if key exist in dict 
Python :: k fold cross validation 
Python :: return key from value dictionary python 
Python :: how to convert user integer input to string in python 
Python :: sum 2d array in python 
Python :: formatting strings in python 
Python :: if then else python 
Python :: python get all numbers between two numbers 
Python :: pandas split groupby 
Python :: Issue AttributeError: ‘numpy.ndarray’ object has no attribute ‘index’ 
Python :: runtime errors in python 
Python :: hide password in python 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =