How to use classification metrics in Scikit learn

Classification matrices implementation in Scikit learn.

 import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split,cross_val_score,KFold
from sklearn.metrics import classification_report,confusion_matrix
load the datasets
seed = 40
dataset = datasets.load_breast_cancer()
X = dataset.data;
y = dataset.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)
kfold = KFold(n_splits=10, random_state=seed)
model = LogisticRegression()
scoring = 'accuracy'
results = cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
print(); print("Accuracy: ", results.mean()); print("Standard Deviation: ", results.std())
scoring = 'neg_log_loss'
results = cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
print(); print("Logloss: ", results.mean()); print("Standard Deviation: ", results.std())
scoring = 'roc_auc'
results = cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
print(); print("AUC: ", results.mean()); print("Standard Deviation: ", results.std())
make predictions with fitted model
model.fit(X_train, y_train)
expected_y = y_test
predicted_y = model.predict(X_test)
summarize the fit of the model
print(); print('LogisticRegression(): ')
print(); print(classification_report(expected_y, predicted_y,target_names=dataset.target_names))
print(); print(confusion_matrix(expected_y, predicted_y))

Classification Reports

Accuracy:  0.9520512820512821
Standard Deviation:  0.03684509214607995

Logloss:  -0.13717043183867528
Standard Deviation:  0.10792771287386453

AUC:  0.9890149284388251
Standard Deviation:  0.015908110662735626

LogisticRegression(): 

              precision    recall  f1-score   support

   malignant       0.95      0.91      0.93        64
      benign       0.95      0.97      0.96       107

    accuracy                           0.95       171
   macro avg       0.95      0.94      0.94       171
weighted avg       0.95      0.95      0.95       171


[[ 58   6]
 [  3 104]]

About Mitra N Mishra 34 Articles
Mitra N Mishra is working as a full-stack data scientist.

2 Comments

  1. I抦 impressed, I have to say. Really rarely do I encounter a weblog that抯 both educative and entertaining, and let me let you know, you have hit the nail on the head. Your idea is outstanding; the problem is one thing that not sufficient persons are talking intelligently about. I’m very glad that I stumbled throughout this in my seek for one thing regarding this.

Leave a Reply

Your email address will not be published.


*