Models
This section provides an overview of the implementation of IF (class IsolationForest), EIF,EIF+ and EXIFFI (all implemebted in class ExtendedIsolationForest). In order to achieve a speed up in the computations the numba Pyhton compiler is used.
ExtendedIsolationForest
Class that represents the Extended Isolation Forest model.
Attributes:
| Name | Type | Description |
|---|---|---|
n_estimators |
int
|
Number of trees in the model. Defaults to 400 |
max_samples |
int
|
Maximum number of samples in a node. Defaults to 256 |
max_depth |
int
|
Maximum depth of the trees. Defaults to "auto" |
plus |
bool
|
Boolean flag to indicate if the model is a |
name |
str
|
Name of the model |
ids |
array
|
Leaf node ids for each data point in the dataset. Defaults to None |
X |
array
|
Input dataset. Defaults to None |
eta |
float
|
Eta value for the model. Defaults to 1.5 |
avg_number_of_nodes |
int
|
Average number of nodes in the trees |
Source code in model_reboot/EIF_reboot.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 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 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
avg_number_of_nodes: float
property
Compute the average number of nodes in the trees.
Returns:
| Type | Description |
|---|---|
float
|
The average number of nodes in the trees. |
compute_ids(X)
Compute the leaf node ids for each data point in the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Input dataset |
required |
Returns:
| Type | Description |
|---|---|
None
|
The method computes the leaf node ids and does not return any value. |
Source code in model_reboot/EIF_reboot.py
468 469 470 471 472 473 474 475 476 477 478 479 480 481 | |
fit(X, locked_dims=None)
Fit the model to the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Input dataset |
required |
locked_dims |
int
|
Number of dimensions to be locked in the model. Defaults to None |
None
|
Returns:
| Type | Description |
|---|---|
None
|
The method fits the model and does not return any value. |
Source code in model_reboot/EIF_reboot.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 | |
global_importances(X, p=0.1)
Compute the global importances of the features for the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Input dataset |
required |
p |
float
|
Proportion of outliers (i.e. threshold for the anomaly score). Defaults to 0.1 |
0.1
|
Returns:
| Type | Description |
|---|---|
array
|
Global importances of the features for the dataset. |
Source code in model_reboot/EIF_reboot.py
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | |
local_importances(X)
Compute the local importances of the features for the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Input dataset |
required |
Returns:
| Type | Description |
|---|---|
array
|
Local importances of the features for the dataset. |
Source code in model_reboot/EIF_reboot.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
predict(X)
Predict the anomaly score for each data point in the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Input dataset |
required |
Returns:
| Type | Description |
|---|---|
array
|
Anomaly score for each data point in the dataset. |
Source code in model_reboot/EIF_reboot.py
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | |
ExtendedTree
Class that represents an Isolation Tree in the Extended Isolation Forest model.
Attributes:
| Name | Type | Description |
|---|---|---|
plus |
bool
|
Boolean flag to indicate if the model is a |
locked_dims |
int
|
Number of dimensions to be locked in the model. Defaults to 0 |
max_depth |
int
|
Maximum depth of the tree |
min_sample |
int
|
Minimum number of samples in a node. Defaults to 1 |
n |
int
|
Number of samples in the dataset |
d |
int
|
Number of dimensions in the dataset |
node_count |
int
|
Counter for the number of nodes in the tree |
max_nodes |
int
|
Maximum number of nodes in the tree. Defaults to 10000 |
path_to |
array
|
Array to store the path to the leaf nodes |
path_to_Right_Left |
array
|
Array to store the path to the leaf nodes with directions |
child_left |
array
|
Array to store the left child nodes |
child_right |
array
|
Array to store the right child nodes |
normals |
array
|
Array to store the normal vectors of the splitting hyperplanes |
intercepts |
array
|
Array to store the intercept values of the splitting hyperplanes |
node_size |
array
|
Array to store the size of the nodes |
depth |
array
|
Array to store the depth of the nodes |
corrected_depth |
array
|
Array to store the corrected depth of the nodes |
importances_right |
array
|
Array to store the importances of the right child nodes |
importances_left |
array
|
Array to store the importances of the left child nodes |
eta |
float
|
Eta value for the model. Defaults to 1.5 |
Source code in model_reboot/EIF_reboot.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
apply(X)
Update the path_to attribute with the path to the leaf nodes for each data point in the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Input dataset |
required |
Returns:
| Type | Description |
|---|---|
None
|
The method updates |
Source code in model_reboot/EIF_reboot.py
350 351 352 353 354 355 356 357 358 359 360 | |
create_new_node(parent_id, direction)
Create a new node in the tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parent_id |
int
|
Parent node id |
required |
direction |
int
|
Direction to the new node |
required |
Returns:
| Type | Description |
|---|---|
int
|
New node id |
Source code in model_reboot/EIF_reboot.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
extend_tree(node_id, X, depth)
Extend the tree to the given node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id |
int
|
Node id |
required |
X |
NDArray
|
Input dataset |
required |
depth |
int
|
Depth of the node |
required |
Returns:
| Type | Description |
|---|---|
None
|
The method extends the tree and does not return any value. |
Source code in model_reboot/EIF_reboot.py
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 | |
fit(X)
Fit the model to the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Input dataset |
required |
Returns:
| Type | Description |
|---|---|
None
|
The method fits the model and does not return any value. |
Source code in model_reboot/EIF_reboot.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
importances(ids)
Compute the importances of the features for the given leaf node ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids |
array
|
Leaf node ids for each data point in the dataset. |
required |
Returns:
| Type | Description |
|---|---|
tuple[array, array]
|
Importances of the features for the given leaf node ids and the normal vectors. |
Source code in model_reboot/EIF_reboot.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
leaf_ids(X)
Get the leaf node ids for each data point in the dataset.
This is a stub method of get_leaf_ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Input dataset |
required |
Returns:
| Type | Description |
|---|---|
array
|
Leaf node ids for each data point in the dataset. |
Source code in model_reboot/EIF_reboot.py
335 336 337 338 339 340 341 342 343 344 345 346 347 | |
predict(ids)
Predict the anomaly score for each data point in the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ids |
array
|
Leaf node ids for each data point in the dataset. |
required |
Returns:
| Type | Description |
|---|---|
array
|
Anomaly score for each data point in the dataset. |
Source code in model_reboot/EIF_reboot.py
362 363 364 365 366 367 368 369 370 371 372 373 374 | |
IsolationForest
Bases: ExtendedIsolationForest
Class that represents the Isolation Forest model.
This is a subclass of ExtendedIsolationForest with the plus attribute set to False and the
locked_dims attribute set to the number of dimensions minus one.
Attributes:
| Name | Type | Description |
|---|---|---|
n_estimators |
int
|
Number of trees in the model. Defaults to 400 |
max_depth |
Union[str, int]
|
Maximum depth of the trees. Defaults to "auto" |
max_samples |
Union[str, int]
|
Maximum number of samples in a node. Defaults to "auto" |
Source code in model_reboot/EIF_reboot.py
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 | |
decision_function_single_tree(tree_idx, X, p=0.1)
Predict the anomaly score for each data point in the dataset using a single tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tree_idx |
int
|
Index of the tree |
required |
X |
array
|
Input dataset |
required |
p |
float
|
Proportion of outliers (i.e. threshold for the anomaly score). Defaults to 0.1 |
0.1
|
Returns:
| Type | Description |
|---|---|
tuple[array, array]
|
Anomaly score for each data point in the dataset and the predicted class for each data point in the dataset. |
Source code in model_reboot/EIF_reboot.py
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 | |
fit(X)
Fit the model to the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Input dataset |
required |
Returns:
| Type | Description |
|---|---|
None
|
The method fits the model and does not return any value. |
Source code in model_reboot/EIF_reboot.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 | |
c_factor(n)
Average path length of unsuccesful search in a binary search tree given n points. This is a constant factor that will be used as a normalization factor in the Anomaly Score computation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n |
int
|
Number of data points for the BST. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Average path length of unsuccesful search in a BST |
Source code in model_reboot/EIF_reboot.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
calculate_importances(paths, directions, importances_left, importances_right, normals, d)
Calculate the importances of the features for the given paths and directions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths |
ndarray
|
Paths to the leaf nodes |
required |
directions |
ndarray
|
Directions to the leaf nodes |
required |
importances_left |
ndarray
|
Importances of the left child nodes |
required |
importances_right |
ndarray
|
Importances of the right child nodes |
required |
normals |
ndarray
|
Normal vectors of the splitting hyperplanes |
required |
d |
int
|
Number of dimensions in the dataset |
required |
Returns:
| Type | Description |
|---|---|
tuple[array, array]
|
Importances of the features for the given paths and directions and the normal vectors. |
Source code in model_reboot/EIF_reboot.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 | |
get_leaf_ids(X, child_left, child_right, normals, intercepts)
Get the leaf node ids for each data point in the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X |
array
|
Data points |
required |
child_left |
array
|
Left child node ids |
required |
child_right |
array
|
Right child node ids |
required |
normals |
array
|
Normal vectors of the splitting hyperplanes |
required |
intercepts |
array
|
Intercept values of the splitting hyperplanes |
required |
Returns:
| Type | Description |
|---|---|
array
|
Leaf node ids for each data point in the dataset. |
Source code in model_reboot/EIF_reboot.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 | |
make_rand_vector(df, dimensions)
Generate a random unitary vector in the unit ball with a maximum number of dimensions. This vector will be successively used in the generation of the splitting hyperplanes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
int
|
Degrees of freedom |
required |
dimensions |
int
|
number of dimensions of the feature space |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the degree of freedom does not match with the dataset dimensions |
Returns:
| Name | Type | Description |
|---|---|---|
vec |
NDArray[float64]
|
Random unitary vector in the unit ball |
Source code in model_reboot/EIF_reboot.py
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 | |