pitci.lightgbm.LGBMBoosterLeafNodeScaledConformalPredictor¶
-
class
pitci.lightgbm.LGBMBoosterLeafNodeScaledConformalPredictor(model)[source]¶ Bases:
pitci.base.LeafNodeScaledConformalPredictorConformal interval predictor for an underlying
lgb.Boostermodel using absolute error scaled by leaf node counts as the nonconformity measure.Class implements inductive conformal intervals where a calibration dataset is used to learn the information that is used when generating intervals for new instances.
The predictor outputs varying width intervals for every new instance. This is done by multiplying the
baseline_intervalby a scaling factor that depends on the inputdata. The scaling function uses the reciporcal of the number of times that the leaf nodes used in making each prediction were visited on the calibration dataset, or when the underlying model was trained - see thetrain_dataargument for thecalibrate()method for more information.The intuition behind this is that for rows that have higher leaf node counts from the calibration set - the model will be more ‘familiar’ with hence the interval for these rows should be smaller. The inverse is true for rows that have lower leaf node counts from the calibration set.
The currently supported lightgbm objective functions, given the nonconformity measure that is based on absolute error, are defined in the
SUPPORTED_OBJECTIVESattribute.- Parameters
model (
lgb.Booster) – Underlyinglgb.Boostermodel to generate prediction intervals with.
-
__version__¶ The version of the
pitcipackage that generated the object.- Type
str
-
model¶ The underlying
lgb.Boostermodel passed in initialising the object.- Type
lgb.Booster
-
leaf_node_counts¶ The number of times each leaf node in each tree was visited when making predictions on the calibration dataset. Each item in the list is a
dictgiving a mapping between leaf node index and counts for a given tree. The length of the list corresponds to the number of trees inmodel.- Type
list
-
baseline_interval¶ The default or baseline conformal half interval width. Will be scaled for each prediction generated.
- Type
float
-
alpha¶ The confidence level of the conformal intervals that will be produced. Attribute is set when the
calibrate()method is run.- Type
int or float
-
SUPPORTED_OBJECTIVES¶ Booster supported objectives. If a lgb.Booster with a non-supported objective is passed when initialising the class object an error will be raised.
- Type
list
Methods
__init__(model)Initialize self.
calibrate(data, response[, alpha, train_data])Calibrate conformal intervals to a given sample of
dataat a given confidence level,alpha, between 0 and 1.predict_with_interval(data)Generate predictions with conformal intervals using the underlying
model.-
calibrate(data, response, alpha=0.95, train_data=None)[source]¶ Calibrate conformal intervals to a given sample of
dataat a given confidence level,alpha, between 0 and 1.This method must be run before
predict_with_interval()can be used to generate predictions.There are 2 items to be calibrated; the leaf node counts stored in the
leaf_node_countsattribute and the half interval width stored in thebaseline_intervalattribute.The user has the option to specify the training sample that was used to buid the model in the
train_dataargument. This is to allow theleaf_node_countsto be calibrated on the same data the underlying model was built on, rather than a separate calibration set which is what will be passed in thedataargument. The default interval width for a givenalphahas to be set on a separate sample to what was used to build the model. If not, the errors will be smaller than they otherwise would be, on a sample the underlying model has not seen before. However for theleaf_node_counts, ideally we want counts from the train sample - we’re not ‘learning’ anything new here, just recreating stats from when the model was built originally.- Parameters
data (np.ndarray or pd.DataFrame) – Dataset to use to set baselines.
response (np.ndarray or pd.Series) – The response values for the records in
data.alpha (int or float, default = 0.95) – Confidence level for the intervals.
train_data (np.ndarray, pd.DataFrame or None, default = None) – Optional dataset that can be passed to set baseline
leaf_node_countsfrom, separate to thedataargument used to setbaseline_intervalwidth.
-
predict_with_interval(data)[source]¶ Generate predictions with conformal intervals using the underlying
model.- Parameters
data (np.ndarray or pd.DataFrame) – Dataset to generate predictions with intervals on.
- Returns
predictions_with_interval – Array of predictions with intervals for each row in
data. Output array will have 3 columns where the first is the lower interval, second are the predictions and the third is the upper interval.- Return type
np.ndarray