How to use Classification Metrics in Python?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to use Classification Metrics in Python?

How to use Classification Metrics in Python?

This recipe helps you use Classification Metrics in Python

1
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]]

Relevant Projects

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.