sega_learn.trees.isolationForest

# Importing the required libraries

 
Modules
       
joblib
numpy

 
Classes
       
builtins.object
IsolationForest
IsolationTree
IsolationUtils

 
class IsolationForest(builtins.object)
    IsolationForest(n_trees=100, max_samples=None, max_depth=10, n_jobs=1, force_true_length=False)
 
IsolationForest is an implementation of the Isolation Forest algorithm for anomaly detection.
 
Attributes:
    n_trees (int): The number of isolation trees to build. Default is 100.
    max_samples (int or None): The maximum number of samples to draw for each tree. If None, defaults to the minimum of 256 or the number of samples in the dataset.
    max_depth (int): The maximum depth of each isolation tree. Default is 10.
    n_jobs (int): The number of parallel jobs to run. Set to -1 to use all available cores. Default is 1.
    force_true_length (bool): Whether to force the true path length calculation. Default is False.
    trees (list): A list to store the trained isolation trees.
    classes_ (numpy.ndarray): An array representing the classes (0 for normal, 1 for anomaly).
 
Methods:
    __init__(n_trees=100, max_samples=None, max_depth=10, n_jobs=1, force_true_length=False):
        Initializes the IsolationForest with the specified parameters.
    fit(X):
        Fits the isolation forest to the data.
            X (array-like): The input features.
    _fit_tree(X):
        Fits a single isolation tree to a subset of the data.
            X (array-like): The input features.
            IsolationTree: A trained isolation tree.
    anomaly_score(X):
        Computes the anomaly scores for given samples.
            X (array-like): The input samples.
            numpy.ndarray: An array of anomaly scores.
    predict(X, threshold=0.5):
        Predicts whether samples are anomalies.
            X (array-like): The input samples.
            threshold (float): The threshold for classifying anomalies (default: 0.5).
            numpy.ndarray: An array of predictions (1 if the sample is an anomaly, 0 otherwise).
    __sklearn_is_fitted__():
        Checks if the model has been fitted.
            bool: True if the model is fitted, False otherwise.
 
  Methods defined here:
__init__(self, n_trees=100, max_samples=None, max_depth=10, n_jobs=1, force_true_length=False)
Initializes the IsolationForest with the specified parameters.
 
Args:
    n_trees: (int), optional - The number of isolation trees to build (default: 100).
    max_samples: (int or None), optional - The maximum number of samples to draw for each tree.
        If None, defaults to the minimum of 256 or the number of samples in the dataset (default: None).
    max_depth: (int), optional - The maximum depth of each isolation tree (default: 10).
    n_jobs: (int), optional - The number of parallel jobs to run.
        Set to -1 to use all available cores (default: 1).
    force_true_length: (bool), optional - Whether to force the true path length calculation (default: False).
 
Attributes:
    n_trees: (int) - The number of isolation trees.
    max_samples: (int or None) - The maximum number of samples for each tree.
    max_depth: (int) - The maximum depth of the trees.
    force_true_length: (bool) - Indicates whether to use the true path length for scoring.
    trees: (list) - A list to store the trained isolation trees.
    n_jobs: (int) - The number of parallel jobs to run.
    classes_: (np.ndarray) - An array representing the classes (0 for normal, 1 for anomaly).
__sklearn_is_fitted__(self)
Checks if the model has been fitted.
anomaly_score(self, X)
Computes the anomaly scores for given samples.
 
Args:
    X: (array-like) - The input samples.
 
Returns:
    array: An array of anomaly scores.
fit(self, X, y=None)
Fits the isolation forest to the data.
 
Args:
    X: (array-like) - The input features.
    y: (array-like) - The target labels (not used in this implementation).
predict(self, X, threshold=0.5)
Predicts whether samples are anomalies.
 
Args:
    X: (array-like) - The input samples.
    threshold: (float) - The threshold for classifying anomalies (default: 0.5).
 
Returns:
    array: An array of predictions (1 if the sample is an anomaly, 0 otherwise).

Data descriptors defined here:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
class IsolationTree(builtins.object)
    IsolationTree(max_depth=10, force_true_length=False)
 
IsolationTree is a class that implements an isolation tree, which is a fundamental building block of the Isolation Forest algorithm.
 
The Isolation Forest is an unsupervised learning method used for anomaly detection.
 
Attributes:
    max_depth (int): The maximum depth of the tree. Default is 10.
    tree (dict): The learned isolation tree structure.
    force_true_length (bool): If True, the true path length is used for scoring
        instead of the average path length.
 
Methods:
    __init__(max_depth=10, force_true_length=False):
        Initializes the IsolationTree with the specified maximum depth and
        scoring method.
    fit(X, depth=0):
        Fits the isolation tree to the input data by recursively partitioning
        the data based on randomly selected features and split values.
    path_length(X, tree=None, depth=0):
        Computes the path length for a given sample by traversing the tree
        structure. The path length is used to determine how isolated a sample is.
 
  Methods defined here:
__init__(self, max_depth=10, force_true_length=False)
Initializes the Isolation Forest with specified parameters.
 
Args:
    max_depth: (int), optional - Maximum depth of the tree (default is 10).
    force_true_length: (bool), optional - If True, use the true path length for scoring (default is False).
 
Attributes:
    max_depth: (int) - Maximum depth of the tree.
    tree: (object or None) - The tree structure used in the Isolation Forest (default is None).
    force_true_length: (bool) - Indicates whether to use the true path length for scoring.
fit(self, X, depth=0)
Fits the isolation tree to the data.
 
Args:
    X: (array-like) - The input features.
    depth: (int) - The current depth of the tree (default: 0).
 
Returns:
    dict: The learned isolation tree.
path_length(self, X, tree=None, depth=0)
Computes the path length for a given sample.
 
Args:
    X: (array-like) - The input sample.
    tree: (dict) - The current node of the tree (default: None).
    depth: (int) - The current depth of the tree (default: 0).
 
Returns:
    int: The path length.

Data descriptors defined here:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object

 
class IsolationUtils(builtins.object)
    Utility functions for the Isolation Forest algorithm.
 
  Static methods defined here:
compute_avg_path_length(size)
Computes the average path length of unsuccessful searches in a binary search tree.
 
Args:
    size: (int) - The size of the tree.
 
Returns:
    average_path_length: (float) - The average path length.

Data descriptors defined here:
__dict__
dictionary for instance variables
__weakref__
list of weak references to the object