Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

We’ll start with a probabilistic regression example on the diabetes dataset from sklearn:

[iter 0] loss=5.7687 val_loss=0.0000 scale=1.0000 norm=66.8065
[iter 100] loss=5.4122 val_loss=0.0000 scale=2.0000 norm=92.0139
[iter 200] loss=5.1742 val_loss=0.0000 scale=1.0000 norm=36.9730
[iter 300] loss=5.0397 val_loss=0.0000 scale=1.0000 norm=33.5210
[iter 400] loss=4.9400 val_loss=0.0000 scale=2.0000 norm=62.7756
Test MSE 3508.531873714694
Test NLL 6.035871970356794

Getting the estimated distributional parameters at a set of points is easy. This returns the predicted mean and standard deviation of the first five observations in the test set:

{'loc': array([192.20800911, 105.3969346 , 125.9068854 , 90.40939382, 74.68291579]), 'scale': array([37.28866 , 26.1406384 , 28.36160015, 34.12044 , 24.49221427])}

Distributions

NGBoost can be used with a variety of distributions, broken down into those for regression (support on an infinite set) and those for classification (support on a finite set).

Regression Distributions

DistributionParametersImplemented ScoresReference
Normalloc, scaleLogScore, CRPScorescipy.stats normal
LogNormals, scaleLogScore, CRPScorescipy.stats lognormal
ExponentialscaleLogScore, CRPScorescipy.stats exponential

Regression distributions can be used through the NGBRegressor() constructor by passing the appropriate class as the Dist argument. Normal is the default.

There are two prediction methods for NGBRegressor objects: predict(), which returns point predictions as one would expect from a standard regressor, and pred_dist(), which returns a distribution object representing the conditional distribution of Y∣X=xiY|X=x_i at the points xix_i in the test set.

array([ 85.68847007, 244.23240372, 165.14133044, 173.26063598, 248.59937868])
array([ 80.23765636, 236.49217244, 166.17076131, 139.26094569, 261.08260799])
{'scale': array([ 80.23765636, 236.49217244, 166.17076131, 139.26094569, 261.08260799])}

Survival Regression

NGBoost supports analyses of right-censored data. Any distribution that can be used for regression in NGBoost can also be used for survival analysis in theory, but this requires the implementation of the right-censored version of the appropriate score. At the moment, LogNormal and Exponential have these scores implemented. To do survival analysis, use NGBSurvival and pass both the time-to-event (or censoring) and event indicator vectors to fit():

[iter 0] loss=5.0123 val_loss=0.0000 scale=2.0000 norm=1.5198
[iter 100] loss=4.6344 val_loss=0.0000 scale=1.0000 norm=0.5617
[iter 200] loss=4.4640 val_loss=0.0000 scale=1.0000 norm=0.5131
[iter 300] loss=4.3313 val_loss=0.0000 scale=2.0000 norm=0.9611
[iter 400] loss=4.2191 val_loss=0.0000 scale=1.0000 norm=0.4611

The scores currently implemented assume that the censoring is independent of survival, conditional on the observed predictors.

Classification Distributions

DistributionParametersImplemented ScoresReference
k_categorical(K)p0, p1... p{K-1}LogScoreCategorical distribution on Wikipedia
BernoullipLogScoreBernoulli distribution on Wikipedia

Classification distributions can be used through the NGBClassifier() constructor by passing the appropriate class as the Dist argument. Bernoulli is the default and is equivalent to k_categorical(2).

When using NGBoost for classification, the outcome vector Y must consist only of integers from 0 to K-1, where K is the total number of classes. This is consistent with the classification standards in sklearn.

NGBClassifier objects have three prediction methods: predict() returns the most likely class, predict_proba() returns the class probabilities, and pred_dist() returns the distribution object.

array([0, 1, 1, 1, 0])
array([[9.48163772e-01, 6.77474734e-03, 4.50614804e-02], [3.28800280e-01, 6.48155593e-01, 2.30441272e-02], [3.59619610e-03, 9.96151763e-01, 2.52041150e-04], [3.59615491e-03, 9.96140351e-01, 2.63494289e-04], [9.91620820e-01, 7.08525332e-03, 1.29392628e-03]])
{'p0': array([0.94816377, 0.32880028, 0.0035962 , 0.00359615, 0.99162082]), 'p1': array([0.00677475, 0.64815559, 0.99615176, 0.99614035, 0.00708525]), 'p2': array([0.04506148, 0.02304413, 0.00025204, 0.00026349, 0.00129393])}

Scores

NGBoost supports the log score (LogScore, also known as negative log-likelihood) and CRPS (CRPScore), although each score may not be implemented for each distribution. The score is specified by the Score argument in the constructor.

Loading...

Base Learners

NGBoost can be used with any sklearn regressor as the base learner, specified with the Base argument. The default is a depth-3 regression tree.

Loading...

Other Arguments

The learning rate, number of estimators, minibatch fraction, and column subsampling are also easily adjusted:

[iter 0] loss=5.7068 val_loss=0.0000 scale=1.0000 norm=60.8766
Loading...

Sample weights (for training) are set using the sample_weight argument to fit.

[iter 0] loss=5.7231 val_loss=0.0000 scale=1.0000 norm=65.9016
Loading...