Multiclass API¶
The generated items below use NumPy-style parsing because this evaluator's public docstrings follow that convention.
Evaluator¶
imvpy.multi_imv.MulticlassIMV
¶
Multinomial IMV for multi-class classification problems.
This class extends IMV to classification tasks with multiple classes, providing both one-vs-all IMV scores and pairwise IMV confusion matrices.
Parameters:
-
data(DataFrame) –The dataset containing features and outcome variable
-
outcome_variable(str) –Name of the outcome/target column
-
model_creator(callable) –Zero-argument function returning a fresh classifier with
fitandpredict_proba; fitted models must expose alignedclasses_arrays. -
n_splits(int, default:10) –Number of folds for k-fold cross-validation
-
optional_explanatory_variables(list, default:None) –List of feature column names. If None, uses all columns except outcome
-
random_state(int, default:None) –Random seed for reproducibility
-
stratified(bool, default:False) –False preserves the legacy shuffled
KFoldbehavior. True selectsStratifiedKFold, which is the right choice for a new analysis on imbalanced classes and guarantees no fold omits a class. Switching changes the result and must be reported. -
verbose(bool, default:False) –Print per-fold progress and results.
Source code in src/imvpy/multi_imv/evaluator.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | |
Pairwise single-fold matrix¶
imvpy.multi_imv.MulticlassIMV.multinominal_imv_matrix
¶
Calculate pairwise IMV confusion matrix for all class combinations.
Creates a matrix showing information gain for each pair of classes. Element (i,j) represents IMV when discriminating class i from class j. Diagonal elements are zero (no discrimination within same class).
Parameters:
-
data(DataFrame) –Test data containing outcome variable
-
outcome_variable(str) –Name of outcome/target column
-
p_base((array - like, shape(n_samples, n_classes))) –Predicted probabilities from null model (intercept only)
-
p_enhanced((array - like, shape(n_samples, n_classes))) –Predicted probabilities from model with features
-
classes(array - like, default:None) –Label of each probability column, in column order (
model.classes_). Required whenever data may not contain every class the model was trained on; otherwise columns are matched to the wrong labels.
Returns:
-
(DataFrame, shape(n_classes, n_classes))–Pairwise IMV matrix indexed by classes. Pairs for which this fold holds no samples of one or both classes are NaN.
-
Process–- For each class pair (i, j) where i ≠ j:
- Filter data to only samples of class i or j
- Normalize probabilities for binary comparison
- Compute IMV comparing base vs enhanced models
- Store IMV(i vs j) at position [i, j]
-
Interpretation–- High IMV(i,j): Features help distinguish class i from class j
- Low IMV(i,j): Little information gain for this class pair
- Matrix is exactly symmetric: IMV(i,j) == IMV(j,i). Pairwise renormalization gives p_j = 1 - p_i, and swapping i and j also flips the label, so ll() is unchanged because it is invariant under (y, p) -> (1-y, 1-p). This is unlike the ablation matrix, where the two models have independent likelihoods.
Source code in src/imvpy/multi_imv/evaluator.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | |
One-vs-rest single fold¶
imvpy.multi_imv.MulticlassIMV.one_vs_all_single_fold
¶
Calculate one-vs-all IMV for a single fold.
For each class, calculates IMV treating it as positive class vs all others.
Parameters:
-
data(DataFrame) –Data with outcome variable
-
outcome_variable(str) –Name of outcome column
-
p_base((array - like, shape(n_samples, n_classes))) –Predicted probabilities from base model
-
p_enhanced((array - like, shape(n_samples, n_classes))) –Predicted probabilities from enhanced model
-
classes(array - like, default:None) –Label of each probability column, in column order (
model.classes_). Required whenever data may not contain every class the model was trained on; otherwise columns are matched to the wrong labels.
Returns:
-
DataFrame–DataFrame with class labels and their IMV scores. Classes absent from this fold score NaN, since one-vs-rest is unmeasurable without positives.
Source code in src/imvpy/multi_imv/evaluator.py
Cross-validated one-vs-rest¶
imvpy.multi_imv.MulticlassIMV.k_fold_one_vs_all
¶
Perform k-fold cross-validation for one-vs-all IMV evaluation.
Trains null and enhanced models across k folds and computes IMV for each class treated as positive vs all other classes combined as negative.
Returns¶
tuple of (imv_results, imv_average) imv_results : list of numpy arrays IMV scores for each fold, shape (n_folds, n_classes) imv_average : numpy array Mean IMV scores across all folds, shape (n_classes,)
Process
- Split data into k folds
- For each fold:
- Train null model (constant only) on train set
- Train enhanced model (with features) on train set
- Compute one-vs-all IMV on test set
- Average IMV scores across all folds
Side Effects
Prints IMV results for all folds when verbose=True.
Example Output
IMV results across folds: [[0.15, 0.23, 0.18], [0.14, 0.21, 0.19], ...]
Note
Uses random_state for reproducible fold splits if specified.
Source code in src/imvpy/multi_imv/evaluator.py
Cross-validated pairwise matrix¶
imvpy.multi_imv.MulticlassIMV.k_fold_imv_matrix
¶
Perform k-fold cross-validation for pairwise IMV confusion matrix.
Trains models across k folds and computes pairwise IMV matrices, then averages to get stable estimates of class discrimination ability.
Returns¶
tuple of (imv_matrices_list, imv_matrices_average) imv_matrices_list : list of numpy arrays IMV confusion matrix for each fold, shape (n_folds, n_classes, n_classes) imv_matrices_average : pd.DataFrame Average IMV matrix across folds, shape (n_classes, n_classes) with class labels as index/columns
Process
- Split data into k folds
- For each fold:
- Train null model (constant only) on train set
- Train enhanced model (with features) on train set
- Compute pairwise IMV matrix on test set
- Average matrices element-wise across all folds
Side Effects
Prints the averaged IMV matrix when verbose=True.
Example Output
Average IMV Matrix: 0 1 2 0 0.000 0.145 0.123 1 0.145 0.000 0.098 2 0.123 0.098 0.000
Note
- Diagonal elements are always 0 (no self-discrimination)
- Matrix is exactly symmetric (see multinominal_imv_matrix)
- Uses random_state for reproducible splits
Source code in src/imvpy/multi_imv/evaluator.py
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 | |
Evaluator plotting methods¶
imvpy.multi_imv.MulticlassIMV.multinomial_IMV_heatmap
¶
Create heatmap visualization of pairwise IMV confusion matrix.
Visualizes the IMV matrix as a colored heatmap with annotations. Useful for identifying which class pairs are most distinguishable.
Parameters¶
imv_matrix : pd.DataFrame or array-like, shape (n_classes, n_classes) Pairwise IMV matrix to visualize (from k_fold_imv_matrix) ax : matplotlib.axes.Axes, optional Existing axis to plot on. If None, creates new figure. figsize : tuple, default=(6, 6) Figure size (width, height) if creating new figure
Returns¶
tuple or matplotlib.axes.Axes - If ax=None: Returns (fig, ax) tuple - If ax provided: Returns ax
Visualization Details
- Color scheme: canonical IMV navy-to-red publication palette
- Annotations: IMV values displayed in cells (3 decimal places)
- Labels: "Outcome1", "Outcome2", etc. for rows and columns
- Diagonal: Always 0 (no self-discrimination)
Example
imv_matrices, imv_avg = evaluator.k_fold_imv_matrix() fig, ax = evaluator.multinomial_IMV_heatmap(imv_avg) plt.tight_layout() plt.show()
Source code in src/imvpy/multi_imv/evaluator.py
imvpy.multi_imv.MulticlassIMV.multinomial_IMV_boxplot
¶
Create boxplot visualization of one-vs-all IMV distribution across folds.
Shows the distribution and variability of IMV scores for each class across k-fold cross-validation. Useful for assessing stability and comparing class-wise information gain.
Parameters¶
imv_results : list of arrays, shape (n_folds, n_classes) One-vs-all IMV results from k_fold_one_vs_all() figsize : tuple, default=(6, 6) Figure size (width, height) if creating new figure ax : matplotlib.axes.Axes, optional Existing axis to plot on. If None, creates new figure.
Returns¶
tuple or matplotlib.axes.Axes - If ax=None: Returns (fig, ax) tuple - If ax provided: Returns ax
Visualization Details
- One boxplot per class showing distribution across folds
- Box: Interquartile range (IQR) Q1-Q3
- Whiskers: Extend to 1.5*IQR or data extremes
- Median line: Shown within each box
- Labels: "Outcome1", "Outcome2", etc. for each class
Example
imv_results, imv_avg = evaluator.k_fold_one_vs_all() fig, ax = evaluator.multinomial_IMV_boxplot(imv_results) plt.tight_layout() plt.show()
Note
Narrow boxes indicate stable IMV across folds. Wide boxes suggest fold-dependent performance.
Source code in src/imvpy/multi_imv/evaluator.py
MultinomialIMV is an identity alias of MulticlassIMV.
multinominal_imv_matrix intentionally retains its historical misspelling.