How to use Classification Metrics in Python?
MACHINE LEARNING RECIPES

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

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.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

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.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.