Commit ce82d60
Fix CategoricalNB predict for datasets where num_features != num_classes
joint_log_likelihood sized its accumulator from x, as
{num_samples, num_features}, but each loop iteration contributes a
{num_samples, num_classes} term (feature_log_probability is
{num_features, num_classes, num_categories}, so the class axis is 1).
The two only agree when num_features happens to equal num_classes,
which is the case in every existing doctest and test - x is
Nx.iota({4, 3}) with num_classes: 3 - so the whole predict path was
only ever exercised in the one shape where the bug is invisible. Any
other dataset raised "cannot broadcast tensor of dimensions {n, c} to
{n, f}".
Same num_features/num_classes confusion as the check_dim bug. Also
pins the squeeze to axis 0 so a model with a single class or a single
category does not lose an extra axis.
Verified against sklearn.naive_bayes.CategoricalNB on a 3-feature,
2-class dataset across default/min_categories/class_prior/fit_prior:
predictions identical and probabilities within f32 precision.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>1 parent 17bdea7 commit ce82d60
2 files changed
Lines changed: 30 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
14 | 13 | | |
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | | - | |
| 19 | + | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
| |||
128 | 127 | | |
129 | 128 | | |
130 | 129 | | |
131 | | - | |
132 | | - | |
| 130 | + | |
133 | 131 | | |
134 | 132 | | |
135 | 133 | | |
| |||
155 | 153 | | |
156 | 154 | | |
157 | 155 | | |
158 | | - | |
159 | | - | |
| 156 | + | |
160 | 157 | | |
161 | 158 | | |
162 | 159 | | |
| |||
291 | 288 | | |
292 | 289 | | |
293 | 290 | | |
294 | | - | |
295 | | - | |
| 291 | + | |
296 | 292 | | |
297 | 293 | | |
298 | 294 | | |
| |||
347 | 343 | | |
348 | 344 | | |
349 | 345 | | |
350 | | - | |
351 | | - | |
| 346 | + | |
| 347 | + | |
352 | 348 | | |
353 | 349 | | |
354 | 350 | | |
| |||
377 | 373 | | |
378 | 374 | | |
379 | 375 | | |
380 | | - | |
381 | | - | |
| 376 | + | |
| 377 | + | |
382 | 378 | | |
383 | 379 | | |
384 | 380 | | |
| |||
399 | 395 | | |
400 | 396 | | |
401 | 397 | | |
402 | | - | |
403 | | - | |
| 398 | + | |
| 399 | + | |
404 | 400 | | |
405 | 401 | | |
406 | 402 | | |
| |||
425 | 421 | | |
426 | 422 | | |
427 | 423 | | |
428 | | - | |
429 | | - | |
| 424 | + | |
430 | 425 | | |
431 | 426 | | |
432 | 427 | | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | 428 | | |
440 | 429 | | |
441 | 430 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
33 | 38 | | |
34 | 39 | | |
35 | 40 | | |
| |||
235 | 240 | | |
236 | 241 | | |
237 | 242 | | |
238 | | - | |
| 243 | + | |
239 | 244 | | |
240 | 245 | | |
241 | 246 | | |
| |||
333 | 338 | | |
334 | 339 | | |
335 | 340 | | |
336 | | - | |
| 341 | + | |
| 342 | + | |
337 | 343 | | |
338 | 344 | | |
339 | 345 | | |
| |||
477 | 483 | | |
478 | 484 | | |
479 | 485 | | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
480 | 493 | | |
481 | | - | |
| 494 | + | |
| 495 | + | |
482 | 496 | | |
483 | 497 | | |
484 | 498 | | |
485 | 499 | | |
486 | 500 | | |
487 | | - | |
| 501 | + | |
488 | 502 | | |
489 | 503 | | |
490 | 504 | | |
| |||
0 commit comments