Commit 3492bdb
Fix DiceGenetic.compute_proximity_loss for all-categorical datasets
When the dataset has no continuous features,
`continuous_feature_indexes` is empty, so `feature_weights` is an empty
np.array and the original implementation hit either:
* `proximity_loss / sum(feature_weights)` ⇒ ZeroDivisionError /
RuntimeWarning + NaN losses (the symptom @kburchfiel reported in #276
with the original quoted snippet), or
* `product.reshape(-1, product.shape[-1])` ⇒ ValueError on a 0-sized
array, depending on input shape.
Both paths poison `compute_loss` with NaN/exceptions and break the
genetic search for legitimate all-categorical use cases.
Proximity is conceptually undefined when there are no continuous
distances to weigh, so short-circuit with a zero loss vector matching
the population shape. The categorical penalty in `compute_loss`
already accounts for categorical sparsity, so dropping the proximity
contribution is the correct semantic — and it matches what users
expect when they explicitly set up a categorical-only `dice_ml.Data`.
Adds `TestComputeProximityLossNoContinuousFeatures` covering the
all-categorical path. The test fails on `origin/main` with the
ValueError reshape variant of this bug.
Closes #276.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>1 parent 8a3aea4 commit 3492bdb
2 files changed
Lines changed: 69 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
370 | 370 | | |
371 | 371 | | |
372 | 372 | | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
373 | 381 | | |
374 | 382 | | |
375 | 383 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
261 | 261 | | |
262 | 262 | | |
263 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
0 commit comments