slise.slise
This script contains the main SLISE functions, and classes.
The library can both be used "sk-learn" style with SliseRegression(...).fit(X, y)
and SliseExplanation(...).explain(index)
, or in a more functional style with
regression(...)
and explain(...)
.
SliseRegression
Class for holding the result from using SLISE for regression. Can also be used sklearn-style to do regression.
Source code in slise/slise.py
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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
|
coefficients: np.ndarray
property
Get the coefficients of the linear model.
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The coefficients of the linear model (the first scalar in the vector is the intercept). |
scaled_epsilon: float
property
Espilon fitting unnormalised data (if the data is normalised).
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Scaled epsilon. |
__init__(epsilon, lambda1=0, lambda2=0, intercept=True, normalise=False, initialisation=initialise_candidates, beta_max=20, max_approx=1.15, max_iterations=300, debug=False, num_threads=1)
Use SLISE for robust regression.
In robust regression we fit regression models that can handle data that contains outliers. SLISE accomplishes this by fitting a model such that the largest possible subset of the data items have an error less than a given value. All items with an error larger than that are considered potential outliers and do not affect the resulting model.
This constructor prepares the parameters, call fit
to fit a robust regression to a dataset.
It is highly recommended that you normalise the data, either before using SLISE or by setting normalise = TRUE.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epsilon |
float
|
Error tolerance. |
required |
lambda1 |
float
|
L1 regularisation strength. Defaults to 0. |
0
|
lambda2 |
float
|
L2 regularisation strength. Defaults to 0. |
0
|
intercept |
bool
|
Add an intercept term. Defaults to True. |
True
|
normalise |
bool
|
Should X and Y be normalised (note that epsilon will not be scaled). Defaults to False. |
False
|
initialisation |
Callable[[ndarray, ndarray, float, Optional[ndarray]], Tuple[ndarray, float]]
|
Function that takes |
initialise_candidates
|
beta_max |
float
|
The stopping sigmoid steepness. Defaults to 20. |
20
|
max_approx |
float
|
Approximation ratio when selecting the next beta. Defaults to 1.15. |
1.15
|
max_iterations |
int
|
Maximum number of OWL-QN iterations. Defaults to 300. |
300
|
debug |
bool
|
Print debug statements each graduated optimisation step. Defaults to False. |
False
|
num_threads |
int
|
The number of numba threads. Set to -1 to use numba defaults. Values >1 sometimes cause unexpectedly large overhead on some CPUs. Defaults to 1. |
1
|
Source code in slise/slise.py
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 |
|
fit(X, Y, weight=None, init=None)
Robustly fit a linear regression to a dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Response vector. |
required |
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
init |
Union[None, ndarray, Tuple[ndarray, float]]
|
Use this alpha (and beta) value instead of the initialisation function. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
SliseRegression |
SliseRegression
|
|
Source code in slise/slise.py
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 |
|
get_params(normalised=False)
Get the coefficients of the linear model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normalised |
bool
|
If the data is normalised within SLISE, return a linear model ftting the normalised data. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The coefficients of the linear model. |
Source code in slise/slise.py
336 337 338 339 340 341 342 343 344 345 346 |
|
normalised(all_columns=True)
Get coefficients for normalised data (if the data is normalised within SLISE).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
all_columns |
bool
|
Add coefficients for constant columns. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
Optional[ndarray]
|
Optional[np.ndarray]: The normalised coefficients or None. |
Source code in slise/slise.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
predict(X=None)
Use the fitted model to predict new responses.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[ndarray, None]
|
Data matrix to predict, or None for using the fitted dataset. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Predicted response. |
Source code in slise/slise.py
390 391 392 393 394 395 396 397 398 399 400 401 402 |
|
score(X=None, Y=None)
Calculate the loss. Lower is better and it should usually be negative (unless the regularisation is very (too?) strong).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[ndarray, None]
|
Data matrix, or None for using the fitted dataset. Defaults to None. |
None
|
Y |
Union[ndarray, None]
|
Response vector, or None for using the fitted dataset. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The loss. |
Source code in slise/slise.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
|
subset(X=None, Y=None)
Get the subset (of non-outliers) used for the robust regression model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[ndarray, None]
|
Data matrix, or None for using the fitted dataset. Defaults to None. |
None
|
Y |
Union[ndarray, None]
|
Response vector, or None for using the fitted dataset. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The selected subset as a boolean mask. |
Source code in slise/slise.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
|
print(variables=None, decimals=3, num_var=10)
Print the current robust regression result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variables |
Union[List[str], None]
|
Names of the variables/columns in X. Defaults to None. |
None
|
num_var |
int
|
Exclude zero weights if there are too many variables. Defaults to 10. |
10
|
decimals |
int
|
Precision to use for printing. Defaults to 3. |
3
|
Source code in slise/slise.py
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 |
|
plot_2d(title='SLISE Regression', label_x='x', label_y='y', decimals=3, fig=None)
Plot the regression in a 2D scatter plot with a line for the regression model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
Title of the plot. Defaults to "SLISE Regression". |
'SLISE Regression'
|
label_x |
str
|
X-axis label. Defaults to "x". |
'x'
|
label_y |
str
|
Y-axis label. Defaults to "y". |
'y'
|
decimals |
int
|
Number of decimals when writing numbers. Defaults to 3. |
3
|
fig |
Union[Figure, None]
|
Pyplot figure to plot on, if None then a new plot is created and shown. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
SliseException
|
If the data has too many dimensions. |
Source code in slise/slise.py
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 |
|
plot_dist(title='SLISE Regression', variables=None, order=None, decimals=3, fig=None)
Plot the regression with density distributions for the dataset and a barplot for the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
Title of the plot. Defaults to "SLISE Explanation". |
'SLISE Regression'
|
variables |
list
|
Names for the variables. Defaults to None. |
None
|
order |
Union[None, int, Sequence[int]]
|
Select variables (None: all, int: largest, selected). Defaults to all. |
None
|
decimals |
int
|
Number of decimals to write. Defaults to 3. |
3
|
fig |
Union[Figure, None]
|
Pyplot figure to plot on, if None then a new plot is created and shown. Defaults to None. |
None
|
Source code in slise/slise.py
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 |
|
plot_subset(title='Response Distribution', decimals=0, fig=None)
Plot a density distributions for response and the response of the subset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
Title of the plot. Defaults to "Response Distribution". |
'Response Distribution'
|
decimals |
int
|
Number of decimals when writing the subset size. Defaults to 0. |
0
|
fig |
Union[Figure, None]
|
Pyplot figure to plot on, if None then a new plot is created and shown. Defaults to None. |
None
|
Source code in slise/slise.py
544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
|
SliseExplainer
Class for holding the result from using SLISE as an explainer. Can also be used sklearn-style to create explanations.
Source code in slise/slise.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 593 594 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 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 |
|
coefficients: np.ndarray
property
Get the explanation as the coefficients of a linear model (approximating the black box model).
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The coefficients of the linear model (the first scalar in the vector is the intercept). |
scaled_epsilon: float
property
Espilon fitting unnormalised data (if the data is normalised).
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
Scaled epsilon. |
__init__(X, Y, epsilon, lambda1=0, lambda2=0, logit=False, normalise=False, initialisation=initialise_candidates, beta_max=20, max_approx=1.15, max_iterations=300, debug=False, num_threads=1)
Use SLISE for explaining outcomes from black box models.
SLISE can also be used to provide local model-agnostic explanations for outcomes from black box models. To do this we replace the ground truth response vector with the predictions from the complex model. Furthermore, we force the model to fit a selected item (making the explanation local). This gives us a local approximation of the complex model with a simpler linear model. In contrast to other methods SLISE creates explanations using real data (not some discretised and randomly sampled data) so we can be sure that all inputs are valid (i.e. in the correct data manifold, and follows the constraints used to generate the data, e.g., the laws of physics).
This prepares the dataset used for the explanations, call explain
on this object to explain outcomes.
It is highly recommended that you normalise the data, either before using SLISE or by setting normalise = TRUE.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Vector of predictions. |
required |
epsilon |
float
|
Error tolerance. |
required |
lambda1 |
float
|
L1 regularisation strength. Defaults to 0. |
0
|
lambda2 |
float
|
L2 regularisation strength. Defaults to 0. |
0
|
logit |
bool
|
Do a logit transformation on the Y vector, this is recommended opnly if Y consists of probabilities. Defaults to False. |
False
|
normalise |
bool
|
Should X and Y be normalised (note that epsilon will not be scaled). Defaults to False. |
False
|
initialisation |
Callable[[ndarray, ndarray, float, Optional[ndarray]], Tuple[ndarray, float]]
|
Function that takes |
initialise_candidates
|
beta_max |
float
|
The final sigmoid steepness. Defaults to 20. |
20
|
max_approx |
float
|
Approximation ratio when selecting the next beta. Defaults to 1.15. |
1.15
|
max_iterations |
int
|
Maximum number of OWL-QN iterations. Defaults to 300. |
300
|
debug |
bool
|
Print debug statements each graduated optimisation step. Defaults to False. |
False
|
num_threads |
int
|
The number of numba threads. Set to -1 to use numba defaults. Values >1 sometimes cause unexpectedly large overhead on some CPUs. Defaults to 1. |
1
|
Source code in slise/slise.py
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 593 594 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 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
|
explain(x, y=None, weight=None, init=None)
Explain an outcome from a black box model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Union[ndarray, int]
|
Data item to explain, or an index to get the item from self.X |
required |
y |
Union[float, None]
|
Prediction to explain. If x is an index then this should be None (y is taken from self.Y). Defaults to None. |
None
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
init |
Union[None, ndarray, Tuple[ndarray, float]]
|
Use this alpha (and beta) value instead of the initialisation function. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
SliseExplainer |
SliseExplainer
|
|
Source code in slise/slise.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 |
|
get_params(normalised=False)
Get the explanation as the coefficients of a linear model (approximating the black box model).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normalised |
bool
|
If the data is normalised within SLISE, return a linear model fitting the normalised data. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The coefficients of the linear model (the first scalar in the vector is the intercept). |
Source code in slise/slise.py
749 750 751 752 753 754 755 756 757 758 759 |
|
normalised(all_columns=True)
Get coefficients for normalised data (if the data is normalised within SLISE).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
all_columns |
bool
|
Add coefficients for constant columns. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
Optional[ndarray]
|
Optional[np.ndarray]: The normalised coefficients or None. |
Source code in slise/slise.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 |
|
predict(X=None)
Use the approximating linear model to predict new outcomes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[ndarray, None]
|
Sata matrix to predict, or None for using the fitted dataset. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: Prediction vector. |
Source code in slise/slise.py
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 |
|
score(X=None, Y=None)
Calculate the loss. Lower is better and it should usually be negative (unless the regularisation is very (/too?) strong).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[ndarray, None]
|
Data matrix, or None for using the fitted dataset. Defaults to None. |
None
|
Y |
Union[ndarray, None]
|
Response vector, or None for using the fitted dataset. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
float |
float
|
The loss. |
Source code in slise/slise.py
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 |
|
subset(X=None, Y=None)
Get the subset / neighbourhood used for the approximation (explanation).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
Union[ndarray, None]
|
Data matrix, or None for using the fitted dataset. Defaults to None. |
None
|
Y |
Union[ndarray, None]
|
Response vector, or None for using the fitted dataset. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The subset as a boolean mask. |
Source code in slise/slise.py
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 |
|
get_terms(normalised=False, x=None)
Get the "terms" of different variables on the outcome. The terms are the (normalised) coefficients times the (normalised) values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
normalised |
bool
|
Return the normalised terms (if normalisation is used). Defaults to False. |
False
|
x |
Union[None, ndarray]
|
The item to calculate the terms for (uses the explained item if None). Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The terms vector. |
Source code in slise/slise.py
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
|
print(variables=None, classes=None, num_var=10, decimals=3)
Print the current explanation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variables |
Union[List[str], None]
|
Names of the (columns/) variables. Defaults to None. |
None
|
classes |
Union[List[str], None]
|
Names of the classes, if explaining a classifier. Defaults to None. |
None
|
num_var |
int
|
Exclude zero weights if there are too many variables. Defaults to 10. |
10
|
decimals |
int
|
Precision to use for printing. Defaults to 3. |
3
|
Source code in slise/slise.py
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 |
|
plot_2d(title='SLISE Explanation', label_x='x', label_y='y', decimals=3, fig=None)
Plot the explanation in a 2D scatter plot (where the explained item is marked) with a line for the approximating model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
Title of the plot. Defaults to "SLISE Explanation". |
'SLISE Explanation'
|
label_x |
str
|
x-axis label. Defaults to "x". |
'x'
|
label_y |
str
|
Y-axis label. Defaults to "y". |
'y'
|
decimals |
int
|
Number of decimals when writing numbers. Defaults to 3. |
3
|
fig |
Union[Figure, None]
|
Pyplot figure to plot on, if None then a new plot is created and shown. Defaults to None. |
None
|
Raises:
Type | Description |
---|---|
SliseException
|
If the data has too many dimensions. |
Source code in slise/slise.py
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 |
|
plot_image(width, height, saturated=True, title='SLISE Explanation', classes=None, decimals=3, fig=None)
Plot the current explanation for a black and white image (e.g. MNIST).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
width |
int
|
Width of the image. |
required |
height |
int
|
Height of the image. |
required |
saturated |
bool
|
Should the explanation be more saturated. Defaults to True. |
True
|
title |
str
|
Title of the plot. Defaults to "SLISE Explanation". |
'SLISE Explanation'
|
classes |
Union[List, str, None]
|
List of class names (first the negative, then the positive), or a single (positive) class name. Defaults to None. |
None
|
decimals |
int
|
Number of decimals to write. Defaults to 3. |
3
|
fig |
Union[Figure, None]
|
Pyplot figure to plot on, if None then a new plot is created and shown. Defaults to None. |
None
|
Source code in slise/slise.py
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 |
|
plot_dist(title='SLISE Explanation', variables=None, order=None, decimals=3, fig=None)
Plot the current explanation with density distributions for the dataset and a barplot for the model.
The barplot contains both the approximating linear model (where the weights can be loosely interpreted as the importance of the different variables and their sign) and the "terms", which is the (scaled) model time the (scaled) item values. The terms demonstrates how the explained item interacts with the approximating linear model, since a negative weight times a negative value actually supports a positive prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
Title of the plot. Defaults to "SLISE Explanation". |
'SLISE Explanation'
|
variables |
list
|
Names for the variables. Defaults to None. |
None
|
order |
Union[None, int, Sequence[int]]
|
Select variables (None: all, int: largest, selected). Defaults to all. |
None
|
decimals |
int
|
Number of decimals to write. Defaults to 3. |
3
|
fig |
Union[Figure, None]
|
Pyplot figure to plot on, if None then a new plot is created and shown. Defaults to None. |
None
|
Source code in slise/slise.py
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 |
|
plot_subset(title='Prediction Distribution', decimals=0, fig=None)
Plot a density distributions for predictions and the predictions of the subset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
Title of the plot. Defaults to "Prediction Distribution". |
'Prediction Distribution'
|
decimals |
int
|
Number of decimals when writing the subset size. Defaults to 0. |
0
|
fig |
Union[Figure, None]
|
Pyplot figure to plot on, if None then a new plot is created and shown. Defaults to None. |
None
|
Source code in slise/slise.py
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 |
|
regression(X, Y, epsilon, lambda1=0, lambda2=0, weight=None, intercept=True, normalise=False, init=None, initialisation=initialise_candidates, beta_max=20, max_approx=1.15, max_iterations=300, debug=False, num_threads=1)
Use SLISE for robust regression
In robust regression we fit regression models that can handle data that contains outliers. SLISE accomplishes this by fitting a model such that the largest possible subset of the data items have an error less than a given value. All items with an error larger than that are considered potential outliers and do not affect the resulting model.
It is highly recommended that you normalise the data, either before using SLISE or by setting normalise = TRUE.
This is a wrapper around slise.slise.SliseRegression that is equivalent to SliseRegression(epsilon, **kwargs).fit(X, Y)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Response vector. |
required |
epsilon |
float
|
Error tolerance. |
required |
lambda1 |
float
|
L1 regularisation strength. Defaults to 0. |
0
|
lambda2 |
float
|
L2 regularisation strength. Defaults to 0. |
0
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
intercept |
bool
|
Add an intercept term. Defaults to True. |
True
|
normalise |
bool
|
Should X aclasses not be scaled). Defaults to False. |
False
|
init |
Union[None, ndarray, Tuple[ndarray, float]]
|
Use this alpha (and beta) value instead of the initialisation function. Defaults to None. |
None
|
initialisation |
Callable[[ndarray, ndarray, float, Optional[ndarray]], Tuple[ndarray, float]]
|
Function that takes |
initialise_candidates
|
beta_max |
float
|
The stopping sigmoid steepness. Defaults to 20. |
20
|
max_approx |
float
|
Approximation ratio when selecting the next beta. Defaults to 1.15. |
1.15
|
max_iterations |
int
|
Maximum number of OWL-QN iterations. Defaults to 300. |
300
|
debug |
bool
|
Print debug statements each graduated optimisation step. Defaults to False. |
False
|
num_threads |
int
|
The number of numba threads. Set to -1 to use numba defaults. Values >1 sometimes cause unexpectedly large overhead on some CPUs. Defaults to 1. |
1
|
Returns:
Name | Type | Description |
---|---|---|
SliseRegression |
SliseRegression
|
Object containing the regression result. |
Source code in slise/slise.py
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 |
|
explain(X, Y, epsilon, x, y=None, lambda1=0, lambda2=0, weight=None, normalise=False, logit=False, init=None, initialisation=initialise_candidates, beta_max=20, max_approx=1.15, max_iterations=300, debug=False, num_threads=1)
Use SLISE for explaining outcomes from black box models.
SLISE can also be used to provide local model-agnostic explanations for outcomes from black box models. To do this we replace the ground truth response vector with the predictions from the complex model. Furthermore, we force the model to fit a selected item (making the explanation local). This gives us a local approximation of the complex model with a simpler linear model. In contrast to other methods SLISE creates explanations using real data (not some discretised and randomly sampled data) so we can be sure that all inputs are valid (i.e. in the correct data manifold, and follows the constraints used to generate the data, e.g., the laws of physics).
It is highly recommended that you normalise the data, either before using SLISE or by setting normalise = TRUE.
This is a wrapper around slise.slise.SliseExplainer that is equivalent to SliseExplainer(X, Y, epsilon, **kwargs).explain(x, y)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
ndarray
|
Data matrix. |
required |
Y |
ndarray
|
Vector of predictions. |
required |
epsilon |
float
|
Error tolerance. |
required |
x |
Union[ndarray, int]
|
The data item to explain, or an index to get the item from self.X |
required |
y |
Union[float, None]
|
The outcome to explain. If x is an index then this should be None (y is taken from self.Y). Defaults to None. |
None
|
lambda1 |
float
|
L1 regularisation strength. Defaults to 0. |
0
|
lambda2 |
float
|
L2 regularisation strength. Defaults to 0. |
0
|
weight |
Optional[ndarray]
|
Weight vector for the data items. Defaults to None. |
None
|
normalise |
bool
|
Should X and Y be normalised (note that epsilon will not be scaled). Defaults to False. |
False
|
logit |
bool
|
Do a logit transformation on the Y vector, this is recommended only if Y consists of probabilities. Defaults to False. |
False
|
init |
Union[None, ndarray, Tuple[ndarray, float]]
|
Use this alpha (and beta) value instead of the initialisation function. Defaults to None. |
None
|
initialisation |
Callable[[ndarray, ndarray, float, Optional[ndarray]], Tuple[ndarray, float]]
|
Function that takes |
initialise_candidates
|
beta_max |
float
|
The final sigmoid steepness. Defaults to 20. |
20
|
max_approx |
float
|
Approximation ratio when selecting the next beta. Defaults to 1.15. |
1.15
|
max_iterations |
int
|
Maximum number of OWL-QN iterations. Defaults to 300. |
300
|
debug |
bool
|
Print debug statements each graduated optimisation step. Defaults to False. |
False
|
num_threads |
int
|
The number of numba threads. Set to -1 to use numba defaults. Values >1 sometimes cause unexpectedly large overhead on some CPUs. Defaults to 1. |
1
|
Returns:
Name | Type | Description |
---|---|---|
SliseExplainer |
SliseExplainer
|
Object containing the explanation. |
Source code in slise/slise.py
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 |
|