Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- from sklearn import datasets
- from sklearn.naive_bayes import GaussianNB
- from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score
- from sklearn.metrics import roc_curve, auc
- # Load an iris dataset
- iris = datasets.load_iris()
- data = iris.data[:, [0, 1]]
- target = iris.target
- target[100:125] = 1
- target[125:150] = 0
- # Split into training and testing samples
- xtrain = np.concatenate((data[0:40], data[50:90], data[100:140]))
- ytrain = np.concatenate((target[0:40], target[50:90], target[100:140]))
- xtest = np.concatenate((data[40:50], data[90:100], data[140:150]))
- ytest = np.concatenate((target[40:50], target[90:100], target[140:150]))
- # Gaussian Naive Bayes Classifier
- clf = GaussianNB()
- clf.fit(xtrain, ytrain)
- pred = clf.predict(xtest)
- pred_proba = clf.predict_proba(xtest)
- print("ytest = " + str(ytest))
- print("pred = " + str(pred))
- print()
- print("Confusion Matrix = ")
- print(confusion_matrix(ytest, pred))
- print()
- print("Accuracy: ", accuracy_score(ytest, pred))
- print("Precision: ", precision_score(ytest, pred, pos_label=1))
- print("Recall: ", recall_score(ytest, pred, pos_label=1))
- print("F1 Score: ", f1_score(ytest, pred, pos_label=1))
- print()
- print()
- # ROC Curve
- fpr, tpr, thresholds = roc_curve(ytest, pred_proba[:, 1])
- # Area under curve
- AUC = auc(fpr, tpr)
- print("AUC = " + str(AUC))
- # Plots
- plt.title('Receiver Operating Characteristic')
- plt.plot(fpr, tpr, 'b', label = 'AUC = %0.2f' % auc(fpr, tpr))
- plt.legend(loc = 'lower right')
- plt.plot([0, 1], [0, 1],'r--')
- plt.xlim([0, 1])
- plt.ylim([0, 1])
- plt.xlabel('False Positive Rate')
- plt.ylabel('True Positive Rate')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement