How to use Classification Metrics in Python?
0

How to use Classification Metrics in Python?

This recipe helps you use Classification Metrics in Python
In [1]:
## How to use Classification Metrics in Python
## DataSet: skleran.load_breast_cancer()
def Snippet_181():
    print()
    print(format('How to use Classification Metrics in Python','*^82'))
    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from sklearn import datasets
    from sklearn import tree, model_selection, metrics
    from sklearn.model_selection import train_test_split
    import matplotlib.pyplot as plt

    plt.style.use('ggplot')

    # load the datasets
    seed = 42
    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.25)
    kfold = model_selection.KFold(n_splits=10, random_state=seed)
    # fit a tree.DecisionTreeClassifier() model to the data
    model = tree.DecisionTreeClassifier()

    # metrics -> Accuracy
    scoring = 'accuracy'
    results = model_selection.cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
    print(); print("Accuracy: ", results.mean()); print("Standard Deviation: ", results.std())

    # metrics -> Logarithmic loss
    scoring = 'neg_log_loss'
    results = model_selection.cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
    print(); print("Logloss: ", results.mean()); print("Standard Deviation: ", results.std())

    # metrics -> Area Under ROC Curve
    scoring = 'roc_auc'
    results = model_selection.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('tree.DecisionTreeClassifier(): ')
    print(); print(metrics.classification_report(expected_y, predicted_y,
                   target_names=dataset.target_names))
    print(); print(metrics.confusion_matrix(expected_y, predicted_y))
Snippet_181()
*******************How to use Classification Metrics in Python********************

Accuracy:  0.9507751937984497
Standard Deviation:  0.03968014020061886

Logloss:  -1.9449783945305377
Standard Deviation:  1.261563364205318

AUC:  0.9512045745169315
Standard Deviation:  0.030736881451303377

tree.DecisionTreeClassifier():

              precision    recall  f1-score   support

   malignant       0.85      0.90      0.88        52
      benign       0.94      0.91      0.93        91

   micro avg       0.91      0.91      0.91       143
   macro avg       0.90      0.91      0.90       143
weighted avg       0.91      0.91      0.91       143


[[47  5]
 [ 8 83]]