slisemap.metrics
Module that contains functions that can be used to evaluate SLISEMAP solutions.
The functions take a solution (plus other arguments) and returns a single float. This float should either be minimised or maximised for best results (see individual functions).
nanmean(x)
Compute the mean, ignoring any nan.
Source code in slisemap/metrics.py
24 25 26 27 28 29 30 31 32 |
|
euclidean_nearest_neighbours(D, index, k=0.1, include_self=True)
Find the k nearest neighbours using euclidean distance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
D |
Tensor
|
Distance matrix. |
required |
index |
int
|
The item (row), for which to find neighbours. |
required |
k |
Union[int, float]
|
The number of neighbours to find. Defaults to 0.1. |
0.1
|
include_self |
bool
|
include the item in its' neighbourhood. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
LongTensor
|
Vector of indices for the neighbours. |
Source code in slisemap/metrics.py
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 |
|
kernel_neighbours(D, index, epsilon=1.0, include_self=True)
Find the neighbours using a softmax kernel.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
D |
Tensor
|
Distance matrix. |
required |
index |
int
|
The item for which we want to find neighbours. |
required |
epsilon |
float
|
Treshold for selecting the neighbourhood (will be divided by |
1.0
|
include_self |
bool
|
include the item in its' neighbourhood. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
LongTensor
|
Vector of indices for the neighbours. |
Source code in slisemap/metrics.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
cluster_neighbours(D, index, clusters, include_self=True)
Find the neighbours with given clusters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
D |
Tensor
|
Distance matrix (ignored). |
required |
index |
int
|
The item for which we want to find neighbours. |
required |
clusters |
LongTensor
|
Cluster id:s for the data items. |
required |
include_self |
bool
|
include the item in its' neighbourhood. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
LongTensor
|
Vector of indices for the neighbours. |
Source code in slisemap/metrics.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
radius_neighbours(D, index, radius=None, quantile=0.2, include_self=True)
Find the neighbours within a radius.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
D |
Tensor
|
Distance matrix (ignored). |
required |
index |
int
|
The item for which we want to find neighbours. |
required |
radius |
Optional[float]
|
The radius of the neighbourhood. Defaults to None. |
None
|
quantile |
float
|
If radius is None then radius is set to the quantile of D. Defaults to 0.2. |
0.2
|
include_self |
bool
|
include the item in its' neighbourhood. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
LongTensor
|
Vector of indices for the neighbours. |
Source code in slisemap/metrics.py
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 |
|
Neighbours = Union[None, np.ndarray, torch.LongTensor, Callable[[torch.Tensor, int], torch.LongTensor]]
module-attribute
Type annotation for specifying neighbouring items. Used in the get_neighbours function.
- If None, every item is or is not a neighbour.
- If a vector of cluster id:s, take neighbours from the same cluter.
- Or a function that gives neighbours (that takes a distance matrix and an index), primarily:
- euclidean_nearest_neighbours
- kernel_neighbours
- cluster_neighbours
- radius_neighbours
get_neighbours(sm, neighbours, full_if_none=False, **kwargs)
Create a function that takes the index of an item and returns the indices of its neighbours.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Union[Slisemap, Tensor]
|
Trained Slisemap solution or an embedding vector (like Slisemap.Z). |
required |
neighbours |
Neighbours
|
Either None (return self), a vector of cluster id:s (take neighbours from the same cluter), or a function that gives neighbours (that takes a distance matrix and an index). |
required |
full_if_none |
bool
|
If |
False
|
Other Parameters:
Name | Type | Description |
---|---|---|
**kwargs |
Any
|
Arguments passed on to |
Returns:
Type | Description |
---|---|
Callable[[int], LongTensor]
|
Function that takes an index and returns neighbour indices. |
Source code in slisemap/metrics.py
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 |
|
slisemap_loss(sm)
Evaluate a SLISEMAP solution by calculating the loss.
Smaller is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
Returns:
Type | Description |
---|---|
float
|
The loss value. |
Source code in slisemap/metrics.py
198 199 200 201 202 203 204 205 206 207 208 209 |
|
entropy(sm, aggregate=True, numpy=True)
Compute row-wise entropy of the W
matrix induced by Z
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
aggregate |
bool
|
Aggregate the row-wise entropies into one scalar. Defaults to True. |
True
|
numpy |
bool
|
Return a |
True
|
Returns:
Type | Description |
---|---|
Union[float, ndarray, Tensor]
|
The entropy. |
Source code in slisemap/metrics.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
slisemap_entropy(sm)
Evaluate a SLISEMAP solution by calculating the entropy. DEPRECATED.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
Returns:
Type | Description |
---|---|
float
|
The embedding entropy. |
Deprecated
1.4: Use entropy instead.
Source code in slisemap/metrics.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|
fidelity(sm, neighbours=None, **kwargs)
Evaluate a SLISEMAP solution by calculating the fidelity (loss per item/neighbourhood).
Smaller is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
neighbours |
Neighbours
|
Either None (only corresponding local model), a vector of cluster id:s, or a function that gives neighbours (see get_neighbours). |
None
|
Other Parameters:
Name | Type | Description |
---|---|---|
**kwargs |
Any
|
Arguments passed on to |
Returns:
Type | Description |
---|---|
float
|
The mean loss. |
Source code in slisemap/metrics.py
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 |
|
coverage(sm, max_loss, neighbours=None, **kwargs)
Evaluate a SLISEMAP solution by calculating the coverage.
Larger is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
max_loss |
float
|
Maximum tolerable loss. |
required |
neighbours |
Neighbours
|
Either None (all), a vector of cluster id:s, or a function that gives neighbours (see get_neighbours). |
None
|
Other Parameters:
Name | Type | Description |
---|---|---|
**kwargs |
Any
|
Arguments passed on to |
Returns:
Type | Description |
---|---|
float
|
The mean fraction of items within the error bound. |
Source code in slisemap/metrics.py
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 |
|
median_loss(sm, neighbours=None, **kwargs)
Evaluate a SLISEMAP solution by calculating the median loss.
Smaller is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
neighbours |
Neighbours
|
Either None (all), a vector of cluster id:s, or a function that gives neighbours (see get_neighbours). |
None
|
Other Parameters:
Name | Type | Description |
---|---|---|
**kwargs |
Any
|
Arguments passed on to |
Returns:
Type | Description |
---|---|
float
|
The mean median loss. |
Source code in slisemap/metrics.py
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 |
|
coherence(sm, neighbours=None, **kwargs)
Evaluate a SLISEMAP solution by calculating the coherence (max change in prediction divided by the change in variable values).
Smaller is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
neighbours |
Neighbours
|
Either None (all), a vector of cluster id:s, or a function that gives neighbours (see get_neighbours). |
None
|
Other Parameters:
Name | Type | Description |
---|---|---|
**kwargs |
Any
|
Arguments passed on to |
Returns:
Type | Description |
---|---|
float
|
The mean coherence. |
Source code in slisemap/metrics.py
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 |
|
stability(sm, neighbours=None, **kwargs)
Evaluate a SLISEMAP solution by calculating the stability (max change in the local model divided by the change in variable values).
Smaller is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
neighbours |
Neighbours
|
Either None (all), a vector of cluster id:s, or a function that gives neighbours (see get_neighbours). |
None
|
Other Parameters:
Name | Type | Description |
---|---|---|
**kwargs |
Any
|
Arguments passed on to |
Returns:
Type | Description |
---|---|
float
|
The mean stability. |
Source code in slisemap/metrics.py
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 |
|
kmeans_matching(sm, clusters=range(2, 10), **kwargs)
Evaluate SLISE by measuring how well clusters in Z and B overlap (using kmeans to find the clusters).
The overlap is measured by finding the best matching clusters and dividing the size of intersect by the size of the union of each cluster pair. Larger is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
clusters |
Union[int, Sequence[int]]
|
The number of clusters. Defaults to range(2, 10). |
range(2, 10)
|
Other Parameters:
Name | Type | Description |
---|---|---|
**kwargs |
Any
|
Additional arguments to |
Returns:
Type | Description |
---|---|
float
|
The mean cluster matching. |
Source code in slisemap/metrics.py
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 |
|
cluster_purity(sm, clusters)
Evaluate a SLISEMAP solution by calculating how many items in the same cluster are neighbours.
Larger is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Union[Slisemap, ToTensor]
|
Trained Slisemap solution or embedding matrix. |
required |
clusters |
Union[ndarray, LongTensor]
|
Cluster ids. |
required |
Returns:
Type | Description |
---|---|
float
|
The mean number of items sharing cluster that are neighbours. |
Source code in slisemap/metrics.py
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 |
|
kernel_purity(sm, clusters, epsilon=1.0, losses=False)
Evaluate a SLISEMAP solution by calculating how many neighbours are in the same cluster.
Larger is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
clusters |
Union[ndarray, LongTensor]
|
Cluster ids. |
required |
epsilon |
float
|
Treshold for being a neighbour ( |
1.0
|
losses |
bool
|
Use losses instead of embedding distances. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
float
|
The mean number of neighbours that are in the same cluster. |
Source code in slisemap/metrics.py
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 |
|
recall(sm, epsilon_D=1.0, epsilon_L=1.0)
Evaluate a SLISEMAP solution by calculating the recall.
We define recall as the intersection between the loss and embedding neighbourhoods divided by the loss neighbourhood.
Larger is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
epsilon_D |
float
|
Treshold for being an embedding neighbour ( |
1.0
|
epsilon_L |
float
|
Treshold for being a loss neighbour ( |
1.0
|
Returns:
Type | Description |
---|---|
float
|
The mean recall. |
Source code in slisemap/metrics.py
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 |
|
precision(sm, epsilon_D=1.0, epsilon_L=1.0)
Evaluate a SLISEMAP solution by calculating the recall.
We define recall as the intersection between the loss and embedding neighbourhoods divided by the embedding neighbourhood.
Larger is better.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
epsilon_D |
float
|
Treshold for being an embedding neighbour ( |
1.0
|
epsilon_L |
float
|
Treshold for being a loss neighbour ( |
1.0
|
Returns:
Type | Description |
---|---|
float
|
The mean precision. |
Source code in slisemap/metrics.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
|
relevance(sm, pred_fn, change)
Evaluate a SLISEMAP solution by calculating the relevance.
Smaller is better.
TODO: This does not (currently) work for multi-class predictions
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
pred_fn |
Callable
|
Function that gives y:s for new x:s (the "black box model"). |
required |
change |
float
|
How much should the prediction change? |
required |
Returns:
Type | Description |
---|---|
float
|
The mean number of mutated variables required to cause a large enough change in the prediction. |
Source code in slisemap/metrics.py
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
|
accuracy(sm, X=None, Y=None, fidelity=True, optimise=False, fit_new=False, **kwargs)
Evaluate a SLISEMAP solution by checking how well the fitted models work on new points.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sm |
Slisemap
|
Trained Slisemap solution. |
required |
X |
Optional[ToTensor]
|
New data matrix (uses the training data if None). Defaults to None. |
None
|
Y |
Optional[ToTensor]
|
New target matrix (uses the training data if None). Defaults to None. |
None
|
fidelity |
bool
|
Return the mean local loss (fidelity) instead of the mean embedding weighted loss. Defaults to True. |
True
|
optimise |
bool
|
If |
False
|
fit_new |
bool
|
Use |
False
|
Other Parameters:
Name | Type | Description |
---|---|---|
**kwargs |
Any
|
Optional keyword arguments to Slisemap.predict. |
Returns:
Type | Description |
---|---|
float
|
Mean loss for the new points. |
Deprecated
1.6: fit_new
, fidelity
, optimise
.
Source code in slisemap/metrics.py
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
|