Skip to content

Commit 67e1545

Browse files
nqvillerpre-commit-ci[bot]CloseChoice
authored
FIX: Add support for categorical splits in lightgbm in C++ library (#4171)
* Init * Revert some accidental changes * Wrong revert * Yet another missing change * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add unit test * Fix failing unit test * update gpu extensions to accept threshold_types and emit warning * fix comment _cext_gpu * update tests and refactor code for lightgbm categories slightly --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tobias Pitters <[email protected]>
1 parent 7d2e348 commit 67e1545

6 files changed

Lines changed: 145 additions & 32 deletions

File tree

‎shap/cext/_cext.cc‎

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ static PyObject *_cext_dense_tree_shap(PyObject *self, PyObject *args)
117117
PyObject *children_default_obj;
118118
PyObject *features_obj;
119119
PyObject *thresholds_obj;
120+
PyObject *threshold_types_obj;
120121
PyObject *values_obj;
121122
PyObject *node_sample_weights_obj;
122123
int max_depth;
@@ -134,8 +135,8 @@ static PyObject *_cext_dense_tree_shap(PyObject *self, PyObject *args)
134135

135136
/* Parse the input tuple */
136137
if (!PyArg_ParseTuple(
137-
args, "OOOOOOOiOOOOOiOOiib", &children_left_obj, &children_right_obj, &children_default_obj,
138-
&features_obj, &thresholds_obj, &values_obj, &node_sample_weights_obj,
138+
args, "OOOOOOOOiOOOOOiOOiib", &children_left_obj, &children_right_obj, &children_default_obj,
139+
&features_obj, &thresholds_obj, &threshold_types_obj, &values_obj, &node_sample_weights_obj,
139140
&max_depth, &X_obj, &X_missing_obj, &y_obj, &R_obj, &R_missing_obj, &tree_limit, &base_offset_obj,
140141
&out_contribs_obj, &feature_dependence, &model_output, &interactions
141142
)) return NULL;
@@ -146,6 +147,7 @@ static PyObject *_cext_dense_tree_shap(PyObject *self, PyObject *args)
146147
PyArrayObject *children_default_array = (PyArrayObject*)PyArray_FROM_OTF(children_default_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
147148
PyArrayObject *features_array = (PyArrayObject*)PyArray_FROM_OTF(features_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
148149
PyArrayObject *thresholds_array = (PyArrayObject*)PyArray_FROM_OTF(thresholds_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
150+
PyArrayObject *threshold_types_array = (PyArrayObject*)PyArray_FROM_OTF(threshold_types_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
149151
PyArrayObject *values_array = (PyArrayObject*)PyArray_FROM_OTF(values_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
150152
PyArrayObject *node_sample_weights_array = (PyArrayObject*)PyArray_FROM_OTF(node_sample_weights_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
151153
PyArrayObject *X_array = (PyArrayObject*)PyArray_FROM_OTF(X_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
@@ -161,14 +163,15 @@ static PyObject *_cext_dense_tree_shap(PyObject *self, PyObject *args)
161163

162164
/* If that didn't work, throw an exception. Note that R and y are optional. */
163165
if (children_left_array == NULL || children_right_array == NULL ||
164-
children_default_array == NULL || features_array == NULL || thresholds_array == NULL ||
166+
children_default_array == NULL || features_array == NULL || thresholds_array == NULL || threshold_types_array == NULL ||
165167
values_array == NULL || node_sample_weights_array == NULL || X_array == NULL ||
166168
X_missing_array == NULL || out_contribs_array == NULL) {
167169
Py_XDECREF(children_left_array);
168170
Py_XDECREF(children_right_array);
169171
Py_XDECREF(children_default_array);
170172
Py_XDECREF(features_array);
171173
Py_XDECREF(thresholds_array);
174+
Py_XDECREF(threshold_types_array);
172175
Py_XDECREF(values_array);
173176
Py_XDECREF(node_sample_weights_array);
174177
Py_XDECREF(X_array);
@@ -195,6 +198,7 @@ static PyObject *_cext_dense_tree_shap(PyObject *self, PyObject *args)
195198
int *children_default = (int*)PyArray_DATA(children_default_array);
196199
int *features = (int*)PyArray_DATA(features_array);
197200
tfloat *thresholds = (tfloat*)PyArray_DATA(thresholds_array);
201+
int *threshold_types = (int*)PyArray_DATA(threshold_types_array);
198202
tfloat *values = (tfloat*)PyArray_DATA(values_array);
199203
tfloat *node_sample_weights = (tfloat*)PyArray_DATA(node_sample_weights_array);
200204
tfloat *X = (tfloat*)PyArray_DATA(X_array);
@@ -211,7 +215,7 @@ static PyObject *_cext_dense_tree_shap(PyObject *self, PyObject *args)
211215
// these are just a wrapper objects for all the pointers and numbers associated with
212216
// the ensemble tree model and the dataset we are explaining
213217
TreeEnsemble trees = TreeEnsemble(
214-
children_left, children_right, children_default, features, thresholds, values,
218+
children_left, children_right, children_default, features, thresholds, threshold_types, values,
215219
node_sample_weights, max_depth, tree_limit, base_offset,
216220
max_nodes, num_outputs
217221
);
@@ -228,6 +232,7 @@ static PyObject *_cext_dense_tree_shap(PyObject *self, PyObject *args)
228232
Py_XDECREF(children_default_array);
229233
Py_XDECREF(features_array);
230234
Py_XDECREF(thresholds_array);
235+
Py_XDECREF(threshold_types_array);
231236
Py_XDECREF(values_array);
232237
Py_XDECREF(node_sample_weights_array);
233238
Py_XDECREF(X_array);
@@ -252,6 +257,7 @@ static PyObject *_cext_dense_tree_predict(PyObject *self, PyObject *args)
252257
PyObject *children_default_obj;
253258
PyObject *features_obj;
254259
PyObject *thresholds_obj;
260+
PyObject *threshold_types_obj;
255261
PyObject *values_obj;
256262
int max_depth;
257263
int tree_limit;
@@ -264,8 +270,8 @@ static PyObject *_cext_dense_tree_predict(PyObject *self, PyObject *args)
264270

265271
/* Parse the input tuple */
266272
if (!PyArg_ParseTuple(
267-
args, "OOOOOOiiOiOOOO", &children_left_obj, &children_right_obj, &children_default_obj,
268-
&features_obj, &thresholds_obj, &values_obj, &max_depth, &tree_limit, &base_offset_obj, &model_output,
273+
args, "OOOOOOOiiOiOOOO", &children_left_obj, &children_right_obj, &children_default_obj,
274+
&features_obj, &thresholds_obj, &threshold_types_obj, &values_obj, &max_depth, &tree_limit, &base_offset_obj, &model_output,
269275
&X_obj, &X_missing_obj, &y_obj, &out_pred_obj
270276
)) return NULL;
271277

@@ -275,6 +281,7 @@ static PyObject *_cext_dense_tree_predict(PyObject *self, PyObject *args)
275281
PyArrayObject *children_default_array = (PyArrayObject*)PyArray_FROM_OTF(children_default_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
276282
PyArrayObject *features_array = (PyArrayObject*)PyArray_FROM_OTF(features_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
277283
PyArrayObject *thresholds_array = (PyArrayObject*)PyArray_FROM_OTF(thresholds_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
284+
PyArrayObject *threshold_types_array = (PyArrayObject*)PyArray_FROM_OTF(threshold_types_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
278285
PyArrayObject *values_array = (PyArrayObject*)PyArray_FROM_OTF(values_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
279286
PyArrayObject *base_offset_array = (PyArrayObject*)PyArray_FROM_OTF(base_offset_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
280287
PyArrayObject *X_array = (PyArrayObject*)PyArray_FROM_OTF(X_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
@@ -285,14 +292,15 @@ static PyObject *_cext_dense_tree_predict(PyObject *self, PyObject *args)
285292

286293
/* If that didn't work, throw an exception. Note that R and y are optional. */
287294
if (children_left_array == NULL || children_right_array == NULL ||
288-
children_default_array == NULL || features_array == NULL || thresholds_array == NULL ||
295+
children_default_array == NULL || features_array == NULL || thresholds_array == NULL || threshold_types_array == NULL ||
289296
values_array == NULL || X_array == NULL ||
290297
X_missing_array == NULL || out_pred_array == NULL) {
291298
Py_XDECREF(children_left_array);
292299
Py_XDECREF(children_right_array);
293300
Py_XDECREF(children_default_array);
294301
Py_XDECREF(features_array);
295302
Py_XDECREF(thresholds_array);
303+
Py_XDECREF(threshold_types_array);
296304
Py_XDECREF(values_array);
297305
Py_XDECREF(base_offset_array);
298306
Py_XDECREF(X_array);
@@ -320,6 +328,7 @@ static PyObject *_cext_dense_tree_predict(PyObject *self, PyObject *args)
320328
int *children_default = (int*)PyArray_DATA(children_default_array);
321329
int *features = (int*)PyArray_DATA(features_array);
322330
tfloat *thresholds = (tfloat*)PyArray_DATA(thresholds_array);
331+
int *threshold_types = (int*)PyArray_DATA(threshold_types_array);
323332
tfloat *values = (tfloat*)PyArray_DATA(values_array);
324333
tfloat *base_offset = (tfloat*)PyArray_DATA(base_offset_array);
325334
tfloat *X = (tfloat*)PyArray_DATA(X_array);
@@ -331,7 +340,7 @@ static PyObject *_cext_dense_tree_predict(PyObject *self, PyObject *args)
331340
// these are just wrapper objects for all the pointers and numbers associated with
332341
// the ensemble tree model and the dataset we are explaining
333342
TreeEnsemble trees = TreeEnsemble(
334-
children_left, children_right, children_default, features, thresholds, values,
343+
children_left, children_right, children_default, features, thresholds, threshold_types, values,
335344
NULL, max_depth, tree_limit, base_offset,
336345
max_nodes, num_outputs
337346
);
@@ -345,6 +354,7 @@ static PyObject *_cext_dense_tree_predict(PyObject *self, PyObject *args)
345354
Py_XDECREF(children_default_array);
346355
Py_XDECREF(features_array);
347356
Py_XDECREF(thresholds_array);
357+
Py_XDECREF(threshold_types_array);
348358
Py_XDECREF(values_array);
349359
Py_XDECREF(base_offset_array);
350360
Py_XDECREF(X_array);
@@ -366,6 +376,7 @@ static PyObject *_cext_dense_tree_update_weights(PyObject *self, PyObject *args)
366376
PyObject *children_default_obj;
367377
PyObject *features_obj;
368378
PyObject *thresholds_obj;
379+
PyObject *threshold_types_obj;
369380
PyObject *values_obj;
370381
int tree_limit;
371382
PyObject *node_sample_weight_obj;
@@ -374,8 +385,8 @@ static PyObject *_cext_dense_tree_update_weights(PyObject *self, PyObject *args)
374385

375386
/* Parse the input tuple */
376387
if (!PyArg_ParseTuple(
377-
args, "OOOOOOiOOO", &children_left_obj, &children_right_obj, &children_default_obj,
378-
&features_obj, &thresholds_obj, &values_obj, &tree_limit, &node_sample_weight_obj, &X_obj, &X_missing_obj
388+
args, "OOOOOOOiOOO", &children_left_obj, &children_right_obj, &children_default_obj,
389+
&features_obj, &thresholds_obj, &threshold_types_obj, &values_obj, &tree_limit, &node_sample_weight_obj, &X_obj, &X_missing_obj
379390
)) return NULL;
380391

381392
/* Interpret the input objects as numpy arrays. */
@@ -384,21 +395,23 @@ static PyObject *_cext_dense_tree_update_weights(PyObject *self, PyObject *args)
384395
PyArrayObject *children_default_array = (PyArrayObject*)PyArray_FROM_OTF(children_default_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
385396
PyArrayObject *features_array = (PyArrayObject*)PyArray_FROM_OTF(features_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
386397
PyArrayObject *thresholds_array = (PyArrayObject*)PyArray_FROM_OTF(thresholds_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
398+
PyArrayObject *threshold_types_array = (PyArrayObject*)PyArray_FROM_OTF(threshold_types_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
387399
PyArrayObject *values_array = (PyArrayObject*)PyArray_FROM_OTF(values_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
388400
PyArrayObject *node_sample_weight_array = (PyArrayObject*)PyArray_FROM_OTF(node_sample_weight_obj, NPY_DOUBLE, NPY_ARRAY_INOUT_ARRAY);
389401
PyArrayObject *X_array = (PyArrayObject*)PyArray_FROM_OTF(X_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
390402
PyArrayObject *X_missing_array = (PyArrayObject*)PyArray_FROM_OTF(X_missing_obj, NPY_BOOL, NPY_ARRAY_IN_ARRAY);
391403

392404
/* If that didn't work, throw an exception. */
393405
if (children_left_array == NULL || children_right_array == NULL ||
394-
children_default_array == NULL || features_array == NULL || thresholds_array == NULL ||
406+
children_default_array == NULL || features_array == NULL || thresholds_array == NULL || threshold_types_array == NULL ||
395407
values_array == NULL || node_sample_weight_array == NULL || X_array == NULL ||
396408
X_missing_array == NULL) {
397409
Py_XDECREF(children_left_array);
398410
Py_XDECREF(children_right_array);
399411
Py_XDECREF(children_default_array);
400412
Py_XDECREF(features_array);
401413
Py_XDECREF(thresholds_array);
414+
Py_XDECREF(threshold_types_array);
402415
Py_XDECREF(values_array);
403416
//PyArray_ResolveWritebackIfCopy(node_sample_weight_array);
404417
Py_XDECREF(node_sample_weight_array);
@@ -418,6 +431,7 @@ static PyObject *_cext_dense_tree_update_weights(PyObject *self, PyObject *args)
418431
int *children_default = (int*)PyArray_DATA(children_default_array);
419432
int *features = (int*)PyArray_DATA(features_array);
420433
tfloat *thresholds = (tfloat*)PyArray_DATA(thresholds_array);
434+
int *threshold_types = (int*)PyArray_DATA(threshold_types_array);
421435
tfloat *values = (tfloat*)PyArray_DATA(values_array);
422436
tfloat *node_sample_weight = (tfloat*)PyArray_DATA(node_sample_weight_array);
423437
tfloat *X = (tfloat*)PyArray_DATA(X_array);
@@ -426,7 +440,7 @@ static PyObject *_cext_dense_tree_update_weights(PyObject *self, PyObject *args)
426440
// these are just wrapper objects for all the pointers and numbers associated with
427441
// the ensemble tree model and the dataset we are explaining
428442
TreeEnsemble trees = TreeEnsemble(
429-
children_left, children_right, children_default, features, thresholds, values,
443+
children_left, children_right, children_default, features, thresholds, threshold_types, values,
430444
node_sample_weight, 0, tree_limit, 0, max_nodes, 0
431445
);
432446
ExplanationDataset data = ExplanationDataset(X, X_missing, NULL, NULL, NULL, num_X, M, 0);
@@ -439,6 +453,7 @@ static PyObject *_cext_dense_tree_update_weights(PyObject *self, PyObject *args)
439453
Py_XDECREF(children_default_array);
440454
Py_XDECREF(features_array);
441455
Py_XDECREF(thresholds_array);
456+
Py_XDECREF(threshold_types_array);
442457
Py_XDECREF(values_array);
443458
// PyArray_ResolveWritebackIfCopy(node_sample_weight_array);
444459
Py_XDECREF(node_sample_weight_array);
@@ -458,6 +473,7 @@ static PyObject *_cext_dense_tree_saabas(PyObject *self, PyObject *args)
458473
PyObject *children_default_obj;
459474
PyObject *features_obj;
460475
PyObject *thresholds_obj;
476+
PyObject *threshold_types_obj;
461477
PyObject *values_obj;
462478
int max_depth;
463479
int tree_limit;
@@ -471,8 +487,8 @@ static PyObject *_cext_dense_tree_saabas(PyObject *self, PyObject *args)
471487

472488
/* Parse the input tuple */
473489
if (!PyArg_ParseTuple(
474-
args, "OOOOOOiiOiOOOO", &children_left_obj, &children_right_obj, &children_default_obj,
475-
&features_obj, &thresholds_obj, &values_obj, &max_depth, &tree_limit, &base_offset_obj, &model_output,
490+
args, "OOOOOOOiiOiOOOO", &children_left_obj, &children_right_obj, &children_default_obj,
491+
&features_obj, &thresholds_obj, &threshold_types_obj, &values_obj, &max_depth, &tree_limit, &base_offset_obj, &model_output,
476492
&X_obj, &X_missing_obj, &y_obj, &out_pred_obj
477493
)) return NULL;
478494

@@ -482,6 +498,7 @@ static PyObject *_cext_dense_tree_saabas(PyObject *self, PyObject *args)
482498
PyArrayObject *children_default_array = (PyArrayObject*)PyArray_FROM_OTF(children_default_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
483499
PyArrayObject *features_array = (PyArrayObject*)PyArray_FROM_OTF(features_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
484500
PyArrayObject *thresholds_array = (PyArrayObject*)PyArray_FROM_OTF(thresholds_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
501+
PyArrayObject *threshold_types_array = (PyArrayObject*)PyArray_FROM_OTF(threshold_types_obj, NPY_INT, NPY_ARRAY_IN_ARRAY);
485502
PyArrayObject *values_array = (PyArrayObject*)PyArray_FROM_OTF(values_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
486503
PyArrayObject *base_offset_array = (PyArrayObject*)PyArray_FROM_OTF(base_offset_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
487504
PyArrayObject *X_array = (PyArrayObject*)PyArray_FROM_OTF(X_obj, NPY_DOUBLE, NPY_ARRAY_IN_ARRAY);
@@ -500,6 +517,7 @@ static PyObject *_cext_dense_tree_saabas(PyObject *self, PyObject *args)
500517
Py_XDECREF(children_default_array);
501518
Py_XDECREF(features_array);
502519
Py_XDECREF(thresholds_array);
520+
Py_XDECREF(threshold_types_array);
503521
Py_XDECREF(values_array);
504522
Py_XDECREF(base_offset_array);
505523
Py_XDECREF(X_array);
@@ -521,6 +539,7 @@ static PyObject *_cext_dense_tree_saabas(PyObject *self, PyObject *args)
521539
int *children_default = (int*)PyArray_DATA(children_default_array);
522540
int *features = (int*)PyArray_DATA(features_array);
523541
tfloat *thresholds = (tfloat*)PyArray_DATA(thresholds_array);
542+
int *threshold_types = (int*)PyArray_DATA(threshold_types_array);
524543
tfloat *values = (tfloat*)PyArray_DATA(values_array);
525544
tfloat *base_offset = (tfloat*)PyArray_DATA(base_offset_array);
526545
tfloat *X = (tfloat*)PyArray_DATA(X_array);
@@ -532,7 +551,7 @@ static PyObject *_cext_dense_tree_saabas(PyObject *self, PyObject *args)
532551
// these are just wrapper objects for all the pointers and numbers associated with
533552
// the ensemble tree model and the dataset we are explaining
534553
TreeEnsemble trees = TreeEnsemble(
535-
children_left, children_right, children_default, features, thresholds, values,
554+
children_left, children_right, children_default, features, thresholds, threshold_types, values,
536555
NULL, max_depth, tree_limit, base_offset,
537556
max_nodes, num_outputs
538557
);
@@ -546,6 +565,7 @@ static PyObject *_cext_dense_tree_saabas(PyObject *self, PyObject *args)
546565
Py_XDECREF(children_default_array);
547566
Py_XDECREF(features_array);
548567
Py_XDECREF(thresholds_array);
568+
Py_XDECREF(threshold_types_array);
549569
Py_XDECREF(values_array);
550570
Py_XDECREF(base_offset_array);
551571
Py_XDECREF(X_array);

0 commit comments

Comments
 (0)