API reference
Feature Effect Methods
effector.global_effect_ale.ALEBase
Bases: GlobalEffectBase
Source code in /home/runner/work/effector/effector/effector/global_effect_ale.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
eval(feature, xs, heterogeneity=False, centering=False)
Evalueate the (RH)ALE feature effect of feature feature
at points xs
.
Notes
This is a common method inherited by both ALE and RHALE.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature |
int
|
index of feature of interest |
required |
xs |
np.ndarray
|
the points along the s-th axis to evaluate the FE plot
- |
required |
heterogeneity |
bool
|
whether to return heterogeneity:
|
False
|
centering |
typing.Union[bool, str]
|
whether to center the plot:
|
False
|
Returns:
Type | Description |
---|---|
typing.Union[np.ndarray, typing.Tuple[np.ndarray, np.ndarray]]
|
the mean effect |
Source code in /home/runner/work/effector/effector/effector/global_effect_ale.py
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 |
|
plot(feature, heterogeneity=False, centering=False, scale_x=None, scale_y=None, show_avg_output=False, y_limits=None, dy_limits=None)
Plot the (RH)ALE feature effect of feature feature
.
Notes
This is a common method inherited by both ALE and RHALE.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature |
int
|
the feature to plot |
required |
heterogeneity |
bool
|
whether to plot the heterogeneity
|
False
|
centering |
Union[bool, str]
|
whether to center the plot:
|
False
|
scale_x |
Optional[dict]
|
None or Dict with keys ['std', 'mean']
|
None
|
scale_y |
Optional[dict]
|
None or Dict with keys ['std', 'mean']
|
None
|
show_avg_output |
bool
|
if True, the average output will be shown as a horizontal line. |
False
|
y_limits |
Optional[List]
|
None or tuple, the limits of the y-axis
|
None
|
dy_limits |
Optional[List]
|
None or tuple, the limits of the dy-axis
|
None
|
Source code in /home/runner/work/effector/effector/effector/global_effect_ale.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 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 |
|
effector.global_effect_ale.ALE
Bases: ALEBase
Source code in /home/runner/work/effector/effector/effector/global_effect_ale.py
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 |
|
__init__(data, model, nof_instances='all', axis_limits=None, avg_output=None, feature_names=None, target_name=None)
Constructor for the ALE plot.
Definition
ALE is defined as: $$ \hat{f}^{ALE}(x_s) = TODO $$
The heterogeneity is: $$ TODO $$
The std of the bin-effects is: $$ TODO $$
Notes
- The required parameters are
data
andmodel
. The rest are optional.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
the design matrix
|
required |
model |
callable
|
the black-box model. Must be a
|
required |
nof_instances |
Union[int, str]
|
the number of instances to use for the explanation
|
'all'
|
axis_limits |
Optional[np.ndarray]
|
The limits of the feature effect plot along each axis
|
None
|
avg_output |
Optional[float]
|
the average output of the model on the data
|
None
|
feature_names |
Optional[List]
|
The names of the features
|
None
|
target_name |
Optional[str]
|
The name of the target variable
|
None
|
Source code in /home/runner/work/effector/effector/effector/global_effect_ale.py
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 |
|
fit(features='all', binning_method='fixed', centering='zero_integral')
Fit the ALE plot.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
typing.Union[int, str, list]
|
the features to fit. If set to "all", all the features will be fitted. |
'all'
|
binning_method |
typing.Union[str, bm.Fixed]
|
|
'fixed'
|
centering |
typing.Union[bool, str]
|
whether to compute the normalization constant for centering the plot:
|
'zero_integral'
|
Source code in /home/runner/work/effector/effector/effector/global_effect_ale.py
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 |
|
effector.global_effect_ale.RHALE
Bases: ALEBase
Source code in /home/runner/work/effector/effector/effector/global_effect_ale.py
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 558 559 560 |
|
__init__(data, model, model_jac=None, nof_instances='all', axis_limits=None, data_effect=None, avg_output=None, feature_names=None, target_name=None)
Constructor for RHALE.
Definition
RHALE is defined as: $$ \hat{f}^{RHALE}(x_s) = TODO $$
The heterogeneity is: $$ TODO $$
The std of the bin-effects is: $$ TODO $$
Notes
The required parameters are data
and model
. The rest are optional.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
the design matrix
|
required |
model |
callable
|
the black-box model. Must be a
|
required |
model_jac |
typing.Union[None, callable]
|
the Jacobian of the model. Must be a
|
None
|
nof_instances |
typing.Union[int, str]
|
the number of instances to use for the explanation
|
'all'
|
axis_limits |
typing.Optional[np.ndarray]
|
The limits of the feature effect plot along each axis
|
None
|
data_effect |
typing.Optional[np.ndarray]
|
|
None
|
avg_output |
typing.Optional[float]
|
the average output of the model on the data
|
None
|
feature_names |
typing.Optional[list]
|
The names of the features
|
None
|
target_name |
typing.Optional[str]
|
The name of the target variable
|
None
|
Source code in /home/runner/work/effector/effector/effector/global_effect_ale.py
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 |
|
fit(features='all', binning_method='greedy', centering=False)
Fit the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
int, str, list
|
the features to fit.
|
'all'
|
binning_method |
str
|
the binning method to use.
|
'greedy'
|
centering |
typing.Union[bool, str]
|
whether to compute the normalization constant for centering the plot:
|
False
|
Source code in /home/runner/work/effector/effector/effector/global_effect_ale.py
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 558 559 560 |
|
effector.global_effect_pdp.PDPBase
Bases: GlobalEffectBase
Source code in /home/runner/work/effector/effector/effector/global_effect_pdp.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
fit(features='all', centering=True, points_for_centering=100, use_vectorized=True)
Fit the PDP or d-PDP.
Notes
You can use .eval
or .plot
without calling .fit
explicitly.
The only thing that .fit
does is to compute the normalization constant for centering the PDP and ICE plots.
This will be automatically done when calling eval
or plot
, so there is no need to call fit
explicitly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
Union[int, str, list]
|
the features to fit. - If set to "all", all the features will be fitted. |
'all'
|
centering |
Union[bool, str]
|
whether to center the plot:
|
True
|
points_for_centering |
int
|
number of linspaced points along the feature axis used for centering.
|
100
|
use_vectorized |
bool
|
whether to use the vectorized version of the PDP computation |
True
|
Source code in /home/runner/work/effector/effector/effector/global_effect_pdp.py
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 |
|
eval(feature, xs, heterogeneity=False, centering=False, return_all=False, use_vectorized=True)
Evaluate the effect of the s-th feature at positions xs
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature |
int
|
index of feature of interest |
required |
xs |
np.ndarray
|
the points along the s-th axis to evaluate the FE plot
|
required |
heterogeneity |
bool
|
whether to return the heterogeneity measures.
|
False
|
centering |
typing.Union[bool, str]
|
whether to center the PDP
|
False
|
return_all |
bool
|
whether to return PDP and ICE plots evaluated at
|
False
|
use_vectorized |
bool
|
whether to use the vectorized version of the PDP computation |
True
|
Returns:
Type | Description |
---|---|
typing.Union[np.ndarray, typing.Tuple[np.ndarray, np.ndarray]]
|
the mean effect |
Source code in /home/runner/work/effector/effector/effector/global_effect_pdp.py
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 |
|
plot(feature, heterogeneity=False, centering=False, nof_points=30, scale_x=None, scale_y=None, nof_ice='all', show_avg_output=False, y_limits=None, use_vectorized=True)
Plot the PDP or d-PDP.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature |
int
|
index of the plotted feature |
required |
heterogeneity |
Union[bool, str]
|
whether to output the heterogeneity of the SHAP values
|
False
|
centering |
Union[bool, str]
|
whether to center the PDP
|
False
|
nof_points |
int
|
number of points to evaluate the SDP plot |
30
|
scale_x |
Optional[dict]
|
dictionary with keys "mean" and "std" for scaling the x-axis |
None
|
scale_y |
Optional[dict]
|
dictionary with keys "mean" and "std" for scaling the y-axis |
None
|
nof_ice |
Union[int, str]
|
number of shap values to show on top of the SHAP curve |
'all'
|
show_avg_output |
bool
|
whether to show the average output of the model |
False
|
y_limits |
Optional[List]
|
limits of the y-axis |
None
|
use_vectorized |
bool
|
whether to use the vectorized version of the PDP computation |
True
|
Source code in /home/runner/work/effector/effector/effector/global_effect_pdp.py
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 |
|
effector.global_effect_pdp.PDP
Bases: PDPBase
Source code in /home/runner/work/effector/effector/effector/global_effect_pdp.py
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 |
|
__init__(data, model, axis_limits=None, nof_instances=300, avg_output=None, feature_names=None, target_name=None)
Constructor of the PDP class.
Definition
PDP is defined as: $$ \hat{f}^{PDP}(x_s) = {1 \over N} \sum_{i=1}^N f(x_s, x_C^{(i)})b $$
The ICE plots are: $$ \hat{f}^{(i)}(x_s) = f(x_s, x_C^{(i)}), \quad i=1, \dots, N $$
The heterogeneity is: $$ \mathcal{H}^{PDP}(x_s) = \sqrt {{1 \over N} \sum_{i=1}^N ( \hat{f}^{(i)}(x_s) - \hat{f}^{PDP}(x_s) )^2} $$
Notes
The required parameters are data
and model
. The rest are optional.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
the design matrix
|
required |
model |
Callable
|
the black-box model. Must be a
|
required |
axis_limits |
Optional[np.ndarray]
|
The limits of the feature effect plot along each axis
|
None
|
nof_instances |
Union[int, str]
|
maximum number of instances to be used for PDP.
|
300
|
avg_output |
Optional[float]
|
The average output of the model.
|
None
|
feature_names |
Optional[List]
|
The names of the features
|
None
|
target_name |
Optional[str]
|
The name of the target variable
|
None
|
Source code in /home/runner/work/effector/effector/effector/global_effect_pdp.py
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 |
|
effector.global_effect_pdp.DerPDP
Bases: PDPBase
Source code in /home/runner/work/effector/effector/effector/global_effect_pdp.py
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 |
|
__init__(data, model, model_jac=None, axis_limits=None, nof_instances=300, avg_output=None, feature_names=None, target_name=None)
Constructor of the DerivativePDP class.
Definition
d-PDP is defined as: $$ \hat{f}^{d-PDP}(x_s) = {1 \over N} \sum_{i=1}^N {df \over d x_s} (x_s, x_C^i) $$
The d-ICE plots are: $$ \hat{f}^i(x_s) = {df \over d x_s}(x_s, x_C^i), \quad i=1, \dots, N $$
The heterogeneity is: $$ \mathcal{H}^{d-PDP}(x_s) = \sqrt {{1 \over N} \sum_{i=1}^N ( \hat{f}^i(x_s) - \hat{f}^{d-PDP}(x_s) )^2} $$
Notes
- The required parameters are
data
andmodel
. The rest are optional. - The
model_jac
is the Jacobian of the model. IfNone
, the Jacobian will be computed numerically.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
the design matrix
|
required |
model |
Callable
|
the black-box model. Must be a
|
required |
model_jac |
Optional[Callable]
|
the black-box model Jacobian. Must be a
|
None
|
axis_limits |
Optional[np.ndarray]
|
The limits of the feature effect plot along each axis
|
None
|
nof_instances |
Union[int, str]
|
maximum number of instances to be used for PDP.
|
300
|
avg_output |
Optional[float]
|
The average output of the model.
|
None
|
feature_names |
Optional[List]
|
The names of the features
|
None
|
target_name |
Optional[str]
|
The name of the target variable
|
None
|
Source code in /home/runner/work/effector/effector/effector/global_effect_pdp.py
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 |
|
effector.global_effect_shap.ShapDP
Bases: GlobalEffectBase
Source code in /home/runner/work/effector/effector/effector/global_effect_shap.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
__init__(data, model, axis_limits=None, nof_instances=100, avg_output=None, feature_names=None, target_name=None)
Constructor of the SHAPDependence class.
Definition
The value of a coalition of \(S\) features is estimated as: $$ \hat{v}(S) = {1 \over N} \sum_{i=1}^N f(x_S \cup x_C^i) - f(x^i) $$ The value of a coalition \(S\) quantifies what the values \(\mathbf{x}_S\) of the features in \(S\) contribute to the output of the model. It is the average (over all instances) difference on the output between setting features in \(S\) to be \(x_S\), i.e., \(\mathbf{x} = (\mathbf{x}_S, \mathbf{x}_C^i)\) and leaving the instance as it is, i.e., \(\mathbf{x}^i = (\mathbf{x}_S^i, \mathbf{x}_C^i)\).
The contribution of a feature \(j\) added to a coalition \(S\) is estimated as: $$ \hat{\Delta}_{S, j} = \hat{v}(S \cup {j}) - \hat{v}(S) $$
The SHAP value of a feature \(j\) with value \(x_j\) is the average contribution of feature \(j\) across all possible coalitions with a weight \(w_{S, j}\):
where \(w_{S, j}\) assures that the contribution of feature \(j\) is the same for all coalitions of the same size. For example, there are \(D-1\) ways for \(x_j\) to enter a coalition of \(|S| = 1\) feature, so \(w_{S, j} = {1 \over D (D-1)}\) for each of them. In contrast, there is only one way for \(x_j\) to enter a coaltion of \(|S|=0\) (to be the first specified feature), so \(w_{S, j} = {1 \over D}\).
The SHAP Dependence Plot (SHAP-DP) is a spline \(\hat{f}^{SDP}_j(x_j)\) fit to the dataset \(\{(x_j^i, \hat{\phi}_j(x_j^i))\}_{i=1}^N\) using the UnivariateSpline
function from scipy.interpolate
.
Notes
- The required parameters are
data
andmodel
. The rest are optional. - SHAP values are computed using the
shap
package, using the classExplainer
. - SHAP values are centered by default, i.e., the average SHAP value is subtracted from the SHAP values.
- More details on the SHAP values can be found in the original paper and in the book Interpreting Machine Learning Models with SHAP
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
the design matrix
|
required |
model |
Callable
|
the black-box model. Must be a
|
required |
axis_limits |
Optional[np.ndarray]
|
The limits of the feature effect plot along each axis
|
None
|
nof_instances |
Union[int, str]
|
maximum number of instances to be used for SHAP estimation.
|
100
|
avg_output |
Optional[float]
|
The average output of the model.
|
None
|
feature_names |
Optional[List[str]]
|
The names of the features
|
None
|
target_name |
Optional[str]
|
The name of the target variable
|
None
|
Source code in /home/runner/work/effector/effector/effector/global_effect_shap.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
fit(features='all', centering=False, points_for_centering=100)
Fit the SHAP Dependence Plot to the data.
Notes
The SHAP Dependence Plot (SDP) \(\hat{f}^{SDP}_j(x_j)\) is a spline fit to
the dataset \(\{(x_j^i, \hat{\phi}_j(x_j^i))\}_{i=1}^N\)
using the UnivariateSpline
function from scipy.interpolate
.
The SHAP standard deviation, \(\hat{\sigma}^{SDP}_j(x_j)\), is a spline fit to the absolute value of the residuals, i.e., to the dataset \(\{(x_j^i, |\hat{\phi}_j(x_j^i) - \hat{f}^{SDP}_j(x_j^i)|)\}_{i=1}^N\), using the UnivariateSpline
function from scipy.interpolate
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
Union[int, str, List]
|
the features to fit. - If set to "all", all the features will be fitted. |
'all'
|
centering |
Union[bool, str]
|
|
False
|
points_for_centering |
Union[int, str]
|
number of linspaced points along the feature axis used for centering.
|
100
|
Notes
SHAP values are by default centered, i.e., \(\sum_{i=1}^N \hat{\phi}_j(x_j^i) = 0\). This does not mean that the SHAP curve is centered around zero; this happens only if the \(s\)-th feature of the dataset instances, i.e., the set \(\{x_s^i\}_{i=1}^N\) is uniformly distributed along the \(s\)-th axis. So, use:
centering=False
, to leave the SHAP values as they are.centering=True
orcentering=zero_integral
, to center the SHAP curve around they
axis.centering=zero_start
, to start the SHAP curve fromy=0
.
SHAP values are expensive to compute.
To speed up the computation consider using a subset of the dataset
points for computing the SHAP values and for centering the spline.
The default values (points_for_fitting_spline=100
and points_for_centering=100
) are a moderate choice.
Source code in /home/runner/work/effector/effector/effector/global_effect_shap.py
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 |
|
eval(feature, xs, heterogeneity=False, centering=False)
Evaluate the effect of the s-th feature at positions xs
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature |
int
|
index of feature of interest |
required |
xs |
np.ndarray
|
the points along the s-th axis to evaluate the FE plot
|
required |
heterogeneity |
bool
|
whether to return the heterogeneity measures.
|
False
|
centering |
typing.Union[bool, str]
|
whether to center the plot
|
False
|
Returns:
Type | Description |
---|---|
typing.Union[np.ndarray, typing.Tuple[np.ndarray, np.ndarray]]
|
the mean effect |
Source code in /home/runner/work/effector/effector/effector/global_effect_shap.py
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 |
|
plot(feature, heterogeneity=False, centering=False, nof_points=30, scale_x=None, scale_y=None, nof_shap_values='all', show_avg_output=False, y_limits=None)
Plot the SHAP Dependence Plot (SDP) of the s-th feature.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature |
int
|
index of the plotted feature |
required |
heterogeneity |
Union[bool, str]
|
whether to output the heterogeneity of the SHAP values
|
False
|
centering |
Union[bool, str]
|
whether to center the SDP
|
False
|
nof_points |
int
|
number of points to evaluate the SDP plot |
30
|
scale_x |
Optional[dict]
|
dictionary with keys "mean" and "std" for scaling the x-axis |
None
|
scale_y |
Optional[dict]
|
dictionary with keys "mean" and "std" for scaling the y-axis |
None
|
nof_shap_values |
Union[int, str]
|
number of shap values to show on top of the SHAP curve |
'all'
|
show_avg_output |
bool
|
whether to show the average output of the model |
False
|
y_limits |
Optional[List]
|
limits of the y-axis |
None
|
Source code in /home/runner/work/effector/effector/effector/global_effect_shap.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 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 |
|
Regional Effect Methods
effector.regional_effect.RegionalEffectBase
Source code in /home/runner/work/effector/effector/effector/regional_effect.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
eval(feature, node_idx, xs, heterogeneity=False, centering=False)
Evaluate the regional effect for a given feature and node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature |
the feature to evaluate |
required | |
node_idx |
the node corresponding to the subregion to evaluate |
required | |
xs |
the points at which to evaluate the regional effect |
required | |
heterogeneity |
whether to return the heterogeneity.
|
False
|
|
centering |
whether to center the regional effect. The following options are available:
|
False
|
Returns:
Type | Description |
---|---|
the mean effect |
Source code in /home/runner/work/effector/effector/effector/regional_effect.py
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 |
|
plot(feature, node_idx, heterogeneity=False, centering=False, scale_x_list=None, scale_y=None, y_limits=None)
Source code in /home/runner/work/effector/effector/effector/regional_effect.py
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 |
|
get_node_info(feature, node_idx)
Source code in /home/runner/work/effector/effector/effector/regional_effect.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
effector.regional_effect_ale.RegionalALE
Bases: RegionalEffectBase
Source code in /home/runner/work/effector/effector/effector/regional_effect_ale.py
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 |
|
__init__(data, model, nof_instances='all', axis_limits=None, feature_types=None, cat_limit=10, feature_names=None, target_name=None)
Regional RHALE constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
X matrix (N,D). |
required |
model |
callable
|
the black-box model (N,D) -> (N, ) |
required |
model_jac |
the black-box model Jacobian (N,D) -> (N,D) |
required | |
axis_limits |
typing.Union[None, np.ndarray]
|
axis limits for the FE plot [2, D] or None. If None, axis limits are computed from the data. |
None
|
feature_types |
typing.Union[list, None]
|
list of feature types (categorical or numerical) |
None
|
cat_limit |
typing.Union[int, None]
|
the minimum number of unique values for a feature to be considered categorical |
10
|
feature_names |
typing.Union[list, None]
|
list of feature names |
None
|
Source code in /home/runner/work/effector/effector/effector/regional_effect_ale.py
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 |
|
fit(features, heter_pcg_drop_thres=0.1, heter_small_enough=0.1, max_depth=1, nof_candidate_splits_for_numerical=20, min_points_per_subregion=10, candidate_conditioning_features='all', split_categorical_features=False, binning_method=binning_methods.Fixed(nof_bins=20, min_points_per_bin=0), centering=False)
Find the Regional RHALE for a list of features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
typing.Union[int, str, list]
|
list of features to fit |
required |
heter_pcg_drop_thres |
float
|
heterogeneity drop threshold for a split to be considered important |
0.1
|
heter_small_enough |
float
|
heterogeneity threshold for a region to be considered homogeneous (splitting stops) |
0.1
|
binning_method |
typing.Union[str, binning_methods.Fixed]
|
binning method to use |
binning_methods.Fixed(nof_bins=20, min_points_per_bin=0)
|
max_depth |
int
|
maximum number of splits to perform (depth of the tree) |
1
|
nof_candidate_splits_for_numerical |
int
|
number of candidate splits to consider for numerical features |
20
|
min_points_per_subregion |
int
|
minimum allowed number of points in a subregion (otherwise the split is not considered as valid) |
10
|
candidate_conditioning_features |
typing.Union[str, list]
|
list of features to consider as conditioning features for the candidate splits |
'all'
|
Source code in /home/runner/work/effector/effector/effector/regional_effect_ale.py
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 |
|
effector.regional_effect_ale.RegionalRHALE
Bases: RegionalEffectBase
Source code in /home/runner/work/effector/effector/effector/regional_effect_ale.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
__init__(data, model, model_jac=None, instance_effects=None, nof_instances='all', axis_limits=None, feature_types=None, cat_limit=10, feature_names=None, target_name=None)
Regional RHALE constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
X matrix (N,D). |
required |
model |
Callable
|
the black-box model (N,D) -> (N, ) |
required |
model_jac |
Optional[Callable]
|
the black-box model Jacobian (N,D) -> (N,D) |
None
|
axis_limits |
Optional[np.ndarray]
|
axis limits for the FE plot [2, D] or None. If None, axis limits are computed from the data. |
None
|
feature_types |
Optional[List]
|
list of feature types (categorical or numerical) |
None
|
cat_limit |
Optional[int]
|
the minimum number of unique values for a feature to be considered categorical |
10
|
feature_names |
Optional[List]
|
list of feature names |
None
|
Source code in /home/runner/work/effector/effector/effector/regional_effect_ale.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
fit(features='all', heter_pcg_drop_thres=0.1, heter_small_enough=0.1, max_depth=1, nof_candidate_splits_for_numerical=20, min_points_per_subregion=10, candidate_conditioning_features='all', split_categorical_features=False, binning_method='greedy', centering=False)
Find the Regional RHALE for a list of features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
typing.Union[int, str, list]
|
list of features to fit |
'all'
|
heter_pcg_drop_thres |
float
|
heterogeneity drop threshold for a split to be considered important |
0.1
|
heter_small_enough |
float
|
heterogeneity threshold for a region to be considered homogeneous (splitting stops) |
0.1
|
binning_method |
typing.Union[str, binning_methods.Fixed, binning_methods.DynamicProgramming, binning_methods.Greedy]
|
binning method to use |
'greedy'
|
max_depth |
int
|
maximum number of splits to perform (depth of the tree) |
1
|
nof_candidate_splits_for_numerical |
int
|
number of candidate splits to consider for numerical features |
20
|
min_points_per_subregion |
int
|
minimum allowed number of points in a subregion (otherwise the split is not considered as valid) |
10
|
candidate_conditioning_features |
typing.Union[str, list]
|
list of features to consider as conditioning features for the candidate splits |
'all'
|
Source code in /home/runner/work/effector/effector/effector/regional_effect_ale.py
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 |
|
effector.regional_effect_pdp.RegionalPDPBase
Bases: RegionalEffectBase
Source code in /home/runner/work/effector/effector/effector/regional_effect_pdp.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
fit(features='all', heter_pcg_drop_thres=0.1, heter_small_enough=0.1, max_depth=1, nof_candidate_splits_for_numerical=20, min_points_per_subregion=10, candidate_conditioning_features='all', split_categorical_features=False, centering=False, nof_instances='all', points_for_centering=100, use_vectorized=True)
Find the Regional PDP for a list of features.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
typing.Union[int, str, list]
|
list of features to fit |
'all'
|
heter_pcg_drop_thres |
float
|
heterogeneity drop threshold for a split to be considered important |
0.1
|
heter_small_enough |
float
|
heterogeneity threshold for a region to be considered homogeneous (splitting stops) |
0.1
|
max_depth |
int
|
maximum number of splits to perform (depth of the tree) |
1
|
nof_candidate_splits_for_numerical |
int
|
number of candidate splits to consider for numerical features |
20
|
min_points_per_subregion |
int
|
minimum allowed number of points in a subregion (otherwise the split is not considered as valid) |
10
|
candidate_conditioning_features |
typing.Union[str, list]
|
list of features to consider as conditioning features for the candidate splits |
'all'
|
Source code in /home/runner/work/effector/effector/effector/regional_effect_pdp.py
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 |
|
effector.regional_effect_pdp.RegionalPDP
Bases: RegionalPDPBase
Source code in /home/runner/work/effector/effector/effector/regional_effect_pdp.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
__init__(data, model, nof_instances=1000, axis_limits=None, feature_types=None, cat_limit=10, feature_names=None, target_name=None)
Source code in /home/runner/work/effector/effector/effector/regional_effect_pdp.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
effector.regional_effect_pdp.RegionalDerPDP
Bases: RegionalPDPBase
Source code in /home/runner/work/effector/effector/effector/regional_effect_pdp.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
__init__(data, model, model_jac=None, nof_instances=1000, axis_limits=None, feature_types=None, cat_limit=10, feature_names=None, target_name=None)
Source code in /home/runner/work/effector/effector/effector/regional_effect_pdp.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
effector.regional_effect_shap.RegionalShapDP
Bases: RegionalEffectBase
Source code in /home/runner/work/effector/effector/effector/regional_effect_shap.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
__init__(data, model, axis_limits=None, nof_instances=100, feature_types=None, cat_limit=10, feature_names=None, target_name=None)
Regional SHAP constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
the design matrix
|
required |
model |
Callable
|
the black-box model. Must be a
|
required |
axis_limits |
Optional[np.ndarray]
|
The limits of the feature effect plot along each axis
|
None
|
nof_instances |
Union[int, str]
|
maximum number of instances to be used for PDP.
|
100
|
feature_types |
Optional[List[str]]
|
The feature types.
|
None
|
cat_limit |
Optional[int]
|
the minimum number of unique values for a feature to be considered categorical |
10
|
feature_names |
Optional[List[str]]
|
The names of the features
|
None
|
target_name |
Optional[str]
|
The name of the target variable
|
None
|
Source code in /home/runner/work/effector/effector/effector/regional_effect_shap.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
fit(features, heter_pcg_drop_thres=0.1, heter_small_enough=0.1, max_depth=1, nof_candidate_splits_for_numerical=20, min_points_per_subregion=10, candidate_conditioning_features='all', split_categorical_features=False, centering=False, points_for_centering=100)
Fit the regional SHAP.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
typing.Union[int, str, list]
|
the features to fit. - If set to "all", all the features will be fitted. |
required |
heter_pcg_drop_thres |
float
|
threshold for the percentage drop in heterogeneity to consider a split valid |
0.1
|
heter_small_enough |
float
|
heterogeneity threshold for a region to be considered homogeneous (splitting stops) |
0.1
|
max_depth |
int
|
maximum number of splits to perform (depth of the tree) |
1
|
nof_candidate_splits_for_numerical |
int
|
number of candidate splits to consider for numerical features |
20
|
min_points_per_subregion |
int
|
minimum allowed number of points in a subregion (otherwise the split is not considered as valid) |
10
|
candidate_conditioning_features |
typing.Union[str, list]
|
list of features to consider as conditioning features for the candidate splits |
'all'
|
split_categorical_features |
bool
|
whether to search for subregions in categorical features |
False
|
centering |
typing.Union[bool, str]
|
whether to center the SHAP dependence plots before estimating the heterogeneity |
False
|
points_for_centering |
int
|
number of points to use for centering |
100
|
Source code in /home/runner/work/effector/effector/effector/regional_effect_shap.py
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 |
|
Binning Methods
effector.binning_methods.Fixed
Source code in /home/runner/work/effector/effector/effector/binning_methods.py
31 32 33 34 35 36 37 38 39 |
|
__init__(nof_bins=100, min_points_per_bin=10, cat_limit=15)
Source code in /home/runner/work/effector/effector/effector/binning_methods.py
32 33 34 35 36 37 38 39 |
|
effector.binning_methods.Greedy
Source code in /home/runner/work/effector/effector/effector/binning_methods.py
18 19 20 21 22 23 24 25 26 27 28 |
|
__init__(init_nof_bins=100, min_points_per_bin=10, discount=0.3, cat_limit=15)
Source code in /home/runner/work/effector/effector/effector/binning_methods.py
19 20 21 22 23 24 25 26 27 28 |
|
effector.binning_methods.DynamicProgramming
Source code in /home/runner/work/effector/effector/effector/binning_methods.py
6 7 8 9 10 11 12 13 14 15 |
|
__init__(max_nof_bins=20, min_points_per_bin=10, discount=0.3, cat_limit=15)
Source code in /home/runner/work/effector/effector/effector/binning_methods.py
7 8 9 10 11 12 13 14 15 |
|
Utils
compute_accumulated_effect(x, limits, bin_effect, dx, square=False)
Compute the accumulated effect at each point x
.
Notes
The function implements the following formula:
Notes
if square=True
, then the formula is:
$$
\mathtt{full_bin_acc} = \sum_{i=0}^{k_x - 1} \mathtt{dx}^2[i] * \mathtt{bin_effect}[i]
$$
Examples:
>>> x = np.array([-1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0])
>>> limits = np.array([0, 1.5, 2.0])
>>> bin_effect = np.array([1.0, -1.0])
>>> dx = np.array([1.5, 0.5])
>>> compute_accumulated_effect(x, limits, bin_effect, dx)
array([0. , 0. , 0. , 0.5, 1. , 1.5, 1. , 1. , 1. ])
>>> x = np.array([-1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0])
>>> limits = np.array([0, 1.5, 2.0])
>>> bin_effect = np.array([1.0, 1.0])
>>> dx = np.array([1.5, 0.5])
>>> compute_accumulated_effect(x, limits, bin_effect, dx)
array([0. , 0. , 0. , 0.5, 1. , 1.5, 2. , 2. , 2. ])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
np.ndarray
|
The points we want to evaluate at, (T) |
required |
limits |
np.ndarray
|
The bin limits, (K+1) |
required |
bin_effect |
np.ndarray
|
The effect in each bin, (K) |
required |
dx |
np.ndarray
|
The bin-widths, (K) |
required |
square |
bool
|
Whether to square the width. If true, the effect is bin_effect * dx^2, otherwise bin_effect * dx |
False
|
Returns:
Name | Type | Description |
---|---|---|
y |
np.ndarray
|
The accumulated effect at each point, (T) |
Source code in /home/runner/work/effector/effector/effector/utils.py
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 |
|
compute_ale_params(xs, df_dxs, limits)
Compute all important parameters for the ALE plot.
Examples:
>>> # Example without interpolation
>>> xs = np.array([0.5, 1.2, 2, 2.3])
>>> df_dxs = np.array([30, 34, 15, 17])
>>> limits = np.array([0, 1.5, 3.])
>>> compute_ale_params(xs, df_dxs, limits)
{'limits': array([0. , 1.5, 3. ]), 'dx': array([1.5, 1.5]), 'points_per_bin': array([2, 2]), 'bin_effect': array([32., 16.]), 'bin_variance': array([4., 1.]), 'bin_estimator_variance': array([2. , 0.5])}
>>> # Example with interpolation
>>> xs = np.array([1, 2, 2.8, 4])
>>> df_dxs = np.array([31, 34, 37, 40])
>>> limits = np.array([1, 3, 4])
>>> compute_ale_params(xs, df_dxs, limits)
{'limits': array([1, 3, 4]), 'dx': array([2, 1]), 'points_per_bin': array([3, 1]), 'bin_effect': array([34., 40.]), 'bin_variance': array([6., 6.]), 'bin_estimator_variance': array([2., 2.])}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xs |
np.ndarray
|
The values of s-th feature, (N) |
required |
df_dxs |
np.ndarray
|
The effect wrt the s-th feature, (N) |
required |
limits |
np.ndarray
|
The bin limits, (K+1) |
required |
Returns:
Name | Type | Description |
---|---|---|
parameters |
dict
|
dict |
Source code in /home/runner/work/effector/effector/effector/utils.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 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|
compute_bin_effect(xs, df_dxs, limits)
Compute the mean effect in each bin.
Notes
The function (a) allocates the instances in the bins and (b) aggregates the instance-level effects to compute the average bin-effect. If no instances lie in a bin, then the bin effect is NaN.
Examples:
>>> n = 100
>>> xs = np.ones([n]) - 0.5
>>> df_dxs = np.ones_like(xs) * 10
>>> limits = np.array([0., 1., 2.0])
>>> bin_effects, ppb = compute_bin_effect(xs, df_dxs, limits)
>>> bin_effects
array([10., nan])
>>> ppb
array([100, 0])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xs |
np.ndarray
|
The s-th feature of the instances, (N) |
required |
df_dxs |
np.ndarray
|
The effect wrt the s-th feature for each instance, (N) |
required |
limits |
np.ndarray
|
The bin limits, (K+1) |
required |
Returns:
Name | Type | Description |
---|---|---|
bin_effects |
np.ndarray
|
The average effect per bin, (K) |
points_per_bin |
np.ndarray
|
The number of points per bin, (K) |
Source code in /home/runner/work/effector/effector/effector/utils.py
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 |
|
compute_bin_variance(xs, df_dxs, limits, bin_effect_mean)
Compute the variance of the effect in each bin.
Notes
The function (a) allocates the points in the bins and (b) computes the variance and the variance/nof points. If less than two points in a bin, NaN is passed.
Examples:
>>> n = 100
>>> xs = np.ones([n]) - 0.5
>>> df_dxs = np.ones_like(xs) * 10
>>> limits = np.array([0., 1., 2.0])
>>> bin_effect_mean, ppb = compute_bin_effect(xs, df_dxs, limits)
>>> bin_variance, bin_estimator_variance = compute_bin_variance(xs, df_dxs, limits, bin_effect_mean)
>>> bin_variance
array([ 0., nan])
>>> bin_estimator_variance
array([ 0., nan])
>>> xs = np.ones(4) * 0.5
>>> df_dxs = np.array([1.0, 3.0, 3.0, 5.0])
>>> limits = np.array([0, 1, 2.0])
>>> bin_effect_mean = np.array([np.mean(df_dxs), np.NaN])
>>> compute_bin_variance(xs, df_dxs, limits, bin_effect_mean)
(array([ 2., nan]), array([0.5, nan]))
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xs |
np.ndarray
|
The points we evaluate, (N) |
required |
df_dxs |
np.ndarray
|
The effect of each point, (N, ) |
required |
limits |
np.ndarray
|
The bin limits (K+1) |
required |
bin_effect_mean |
np.ndarray
|
Mean effect in each bin, (K) |
required |
Returns:
Name | Type | Description |
---|---|---|
bin_variance |
np.ndarray
|
The variance in each bin, (K, ) |
bin_estimator_variance |
np.ndarray
|
The variance of the estimator in each bin, (K, ) |
Source code in /home/runner/work/effector/effector/effector/utils.py
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 |
|
compute_jacobian_numerically(model, data, eps=1e-08)
Compute the Jacobian of the model using finite differences.
Notes
The function computes the Jacobian of the model using finite differences. The formula is:
Examples:
>>> data = np.array([[1, 2], [2, 3.0]])
>>> model = lambda x: np.sum(x, axis=1)
>>> compute_jacobian_numerically(model, data)
array([[1., 1.],
[1., 1.]])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
The dataset, (N, D) |
required |
model |
typing.Callable
|
The black-box model ((N, D) -> (N)) |
required |
eps |
float
|
The finite difference step |
1e-08
|
Returns:
Name | Type | Description |
---|---|---|
jacobian |
np.ndarray
|
The Jacobian of the model, (N, D) |
Source code in /home/runner/work/effector/effector/effector/utils.py
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 |
|
compute_local_effects(data, model, limits, feature)
Compute the local effects, permuting the feature of interest using the bin limits.
Notes
The function (a) allocates the points in the bins based on the feature of interest (foi) and (b) computes the effect as the difference when evaluating the output setting the foi at the right and the left limit of the bin.
Given that the bins are defined as a list [l_0, l_1, ..., l_k], and x_s of the i-th point belongs to the k-th bin:
Examples:
>>> data = np.array([[1, 2], [2, 3.0]])
>>> model = lambda x: np.sum(x, axis=1)
>>> limits = np.array([1.0, 2.0])
>>> data_effect = compute_local_effects(data, model, limits, feature=0)
>>> data_effect
array([1., 1.])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
The training set, (N, D) |
required |
model |
typing.Callable
|
The black-box model ((N, D) -> (N)) |
required |
limits |
np.ndarray
|
The bin limits, (K+1) |
required |
feature |
int
|
Index of the feature-of-interest |
required |
Returns:
Name | Type | Description |
---|---|---|
data_effect |
np.ndarray
|
The local effect of each data point, (N) |
Source code in /home/runner/work/effector/effector/effector/utils.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
fill_nans(x)
Replace NaNs with interpolated values.
Examples:
>>> x = np.array([1.0, np.NaN, 2.0])
>>> fill_nans(x)
array([1. , 1.5, 2. ])
>>> x = np.array([1.0, np.NaN, np.NaN, np.NaN, 2.0])
>>> fill_nans(x)
array([1. , 1.25, 1.5 , 1.75, 2. ])
>>> x = np.array([0.5, 1.0, np.NaN, np.NaN, np.NaN])
>>> fill_nans(x)
array([0.5, 1. , 1. , 1. , 1. ])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
np.ndarray
|
Time-series with NaNs, (T) |
required |
Returns:
Name | Type | Description |
---|---|---|
x |
np.ndarray
|
Time-series values without NaNs, (T) |
Source code in /home/runner/work/effector/effector/effector/utils.py
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 |
|
filter_points_in_bin(xs, df_dxs, limits)
Filter the points inside the bin defined by the limits
.
Notes
Filtering depends on whether xs
lies in the interval [limits[0], limits[1]], not df_dxs
.
Examples:
>>> xs = np.array([1, 2, 3])
>>> df_dxs = np.array([32, 34, 36])
>>> limits = np.array([1, 2])
>>> xs, df_dxs = filter_points_in_bin(xs, df_dxs, limits)
>>> xs
array([1, 2])
>>> df_dxs
array([32, 34])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xs |
np.ndarray
|
The instances, (N) |
required |
df_dxs |
typing.Union[np.ndarray, None]
|
The instance-effects (N) or None |
required |
limits |
np.ndarray
|
[Start, Stop] of the bin |
required |
Returns:
Name | Type | Description |
---|---|---|
data |
np.ndarray
|
The instances in the bin, (nof_points_in_bin, D) |
data_effect |
typing.Union[np.ndarray, None]
|
The instance-effects in the bin, (nof_points_in_bin, D) or None |
Source code in /home/runner/work/effector/effector/effector/utils.py
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 |
|
get_feature_types(data, categorical_limit=10)
Determine the type of each feature.
Notes
A feature is considered as categorical if it has less than cat_limit
unique values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
np.ndarray
|
The dataset, (N, D) |
required |
categorical_limit |
int
|
Maximum unique values for a feature to be considered as categorical |
10
|
Returns:
Name | Type | Description |
---|---|---|
types |
typing.List[str]
|
A list of strings, where each string is either |
Source code in /home/runner/work/effector/effector/effector/utils.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
|