Gradient-Boosting anything (alert: high performance)

This article was first published on T. Moudiki's Webpage - Python , and kindly contributed to python-bloggers. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

We’ve always been told that decision trees are best for Gradient Boosting Machine Learning. I’ve always wanted to see for myself. AdaBoostClassifier is working well, but is relatively slow (by my own standards). A few days ago, I noticed that my Cython implementation of LSBoost in Python package mlsauce was already quite generic (never noticed before), and I decided to adapt it to any machine learning model with fit and predict methods. It’s worth mentioning that only regression algorithms are accepted as base learners, and classification is regression-based. The results are promising indeed; I’ll let you see for yourself below. All the algorithms, including xgboost and RandomForest, are used with their default hyperparameters. Which means, there’s still a room for improvement.

Install mlsauce (version 0.20.3) from GitHub:

!pip install git+https://github.com/Techtonique/mlsauce.git --verbose --upgrade --no-cache-dir
import os
import pandas as pd
import mlsauce as ms
from sklearn.datasets import load_breast_cancer, load_iris, load_wine, load_digits
from sklearn.model_selection import train_test_split
from time import time

load_models = [load_breast_cancer, load_wine, load_iris]

for model in load_models:

    data = model()
    X = data.data
    y= data.target
    X = pd.DataFrame(X, columns=data.feature_names)

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state = 13)

    clf = ms.LazyBoostingClassifier(verbose=0, ignore_warnings=True,
                                    custom_metric=None, preprocess=False)

    start = time()
    models, predictions = clf.fit(X_train, X_test, y_train, y_test)
    print(f"\nElapsed: {time() - start} seconds\n")

    display(models)

2it [00:01,  1.52it/s]
100%|██████████| 30/30 [00:21<00:00,  1.38it/s]


Elapsed: 23.019137859344482 seconds
AccuracyBalanced AccuracyROC AUCF1 ScoreTime Taken
Model
GenericBooster(LinearRegression)0.990.990.990.990.35
GenericBooster(Ridge)0.990.990.990.990.27
GenericBooster(RidgeCV)0.990.990.990.991.07
GenericBooster(TransformedTargetRegressor)0.990.990.990.990.40
GenericBooster(KernelRidge)0.970.960.960.972.05
XGBClassifier0.960.960.960.960.91
GenericBooster(ExtraTreeRegressor)0.940.940.940.940.25
RandomForestClassifier0.920.930.930.920.40
GenericBooster(RANSACRegressor)0.900.860.860.9015.22
GenericBooster(DecisionTreeRegressor)0.870.880.880.870.98
GenericBooster(KNeighborsRegressor)0.870.890.890.870.49
GenericBooster(ElasticNet)0.850.760.760.840.10
GenericBooster(Lasso)0.820.710.710.790.09
GenericBooster(LassoLars)0.820.710.710.790.10
GenericBooster(DummyRegressor)0.680.500.500.560.01


2it [00:00,  8.29it/s]
100%|██████████| 30/30 [00:15<00:00,  1.92it/s]


Elapsed: 15.911818265914917 seconds
AccuracyBalanced AccuracyROC AUCF1 ScoreTime Taken
Model
RandomForestClassifier1.001.00None1.000.18
GenericBooster(ExtraTreeRegressor)1.001.00None1.000.16
GenericBooster(KernelRidge)1.001.00None1.000.38
GenericBooster(LinearRegression)1.001.00None1.000.23
GenericBooster(Ridge)1.001.00None1.000.17
GenericBooster(RidgeCV)1.001.00None1.000.24
GenericBooster(TransformedTargetRegressor)1.001.00None1.000.26
XGBClassifier0.970.96None0.970.06
GenericBooster(Lars)0.940.94None0.950.99
GenericBooster(DecisionTreeRegressor)0.920.92None0.920.23
GenericBooster(KNeighborsRegressor)0.920.93None0.920.21
GenericBooster(RANSACRegressor)0.810.81None0.8012.63
GenericBooster(ElasticNet)0.610.53None0.530.04
GenericBooster(DummyRegressor)0.420.33None0.250.01
GenericBooster(Lasso)0.420.33None0.250.02
GenericBooster(LassoLars)0.420.33None0.250.01


2it [00:00,  5.14it/s]
100%|██████████| 30/30 [00:15<00:00,  1.92it/s]


Elapsed: 16.0275661945343 seconds
AccuracyBalanced AccuracyROC AUCF1 ScoreTime Taken
Model
GenericBooster(Ridge)1.001.00None1.000.23
GenericBooster(RidgeCV)1.001.00None1.000.25
RandomForestClassifier0.970.97None0.970.26
XGBClassifier0.970.97None0.970.12
GenericBooster(DecisionTreeRegressor)0.970.97None0.970.27
GenericBooster(ExtraTreeRegressor)0.970.97None0.970.22
GenericBooster(LinearRegression)0.970.97None0.970.15
GenericBooster(TransformedTargetRegressor)0.970.97None0.970.37
GenericBooster(KNeighborsRegressor)0.930.95None0.931.52
GenericBooster(KernelRidge)0.870.83None0.850.63
GenericBooster(RANSACRegressor)0.630.59None0.6110.86
GenericBooster(Lars)0.500.46None0.480.99
GenericBooster(DummyRegressor)0.270.33None0.110.01
GenericBooster(ElasticNet)0.270.33None0.110.01
GenericBooster(Lasso)0.270.33None0.110.01
GenericBooster(LassoLars)0.270.33None0.110.01


!pip install shap
import shap

best_model = clf.get_best_model()

# load JS visualization code to notebook
shap.initjs()

# explain all the predictions in the test set
explainer = shap.KernelExplainer(best_model.predict_proba, X_train)
shap_values = explainer.shap_values(X_test)
# this is multiclass so we only visualize the contributions to first class (hence index 0)
shap.force_plot(explainer.expected_value[0], shap_values[..., 0], X_test)

xxx

Gradient-Boosting anything (alert: high performance)

WARNING:shap:Using 120 background data samples could cause slower run times. Consider using shap.sample(data, K) or shap.kmeans(data, K) to summarize the background as K samples.



  0%|          | 0/30 [00:00<?, ?it/s]
Visualization omitted, Javascript library not loaded!
Have you run `initjs()` in this notebook? If this notebook was from another
user you must also trust this notebook (File -> Trust notebook). If you are viewing
this notebook on github the Javascript has been stripped for security. If you are using
JupyterLab this error is because a JupyterLab extension has not yet been written.

To leave a comment for the author, please follow the link and comment on their blog: T. Moudiki's Webpage - Python .

Want to share your content on python-bloggers? click here.