### Install PyThresh from Source using pip Source: https://github.com/kulikdm/pythresh/blob/main/README.rst Installs PyThresh by cloning the repository and running the setup.py file with pip. This is useful for installing the latest development version. ```bash git clone https://github.com/KulikDM/pythresh.git cd pythresh pip install . ``` -------------------------------- ### Install PyThresh Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/00_Introduction.ipynb Install the pythresh library using pip. This is the first step to using the library. ```python !pip install pythresh ``` -------------------------------- ### Install PyThresh using pip Source: https://github.com/kulikdm/pythresh/blob/main/docs/install.md Use this command for a standard installation of the latest stable version of PyThresh. Ensure you have pip installed. ```bash pip install pythresh ``` -------------------------------- ### Install PyThresh from Source (Git Clone) Source: https://github.com/kulikdm/pythresh/blob/main/docs/install.md Clone the repository and install PyThresh locally. This method is useful for development or for installing the absolute latest updates directly from the main branch. ```bash git clone https://github.com/KulikDM/pythresh.git cd pythresh pip install . ``` -------------------------------- ### Install PyThresh from GitHub Archive Source: https://github.com/kulikdm/pythresh/blob/main/docs/install.md Install PyThresh directly from the main branch zip archive on GitHub. This is an alternative to cloning the repository for installing the latest development version. ```bash pip install https://github.com/KulikDM/pythresh/archive/main.zip ``` -------------------------------- ### Install PyThresh and XGBoost Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/03_Ranking.ipynb Install the necessary libraries, pythresh and xgboost, before proceeding. Ensure xgboost is version 2.0.0 or higher. ```python !pip install pythresh xgboost>=2.0.0 ``` -------------------------------- ### Install PyThresh using pip Source: https://github.com/kulikdm/pythresh/blob/main/README.rst Installs the PyThresh library using pip. Use the upgrade flag for the latest version. ```bash pip install pythresh # normal install pip install --upgrade pythresh # or update if needed ``` -------------------------------- ### Time Complexity Benchmark Setup Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/Benchmark_Thresholder.ipynb Sets up and runs a time complexity benchmark for a given function. This involves generating data, fitting a model, and evaluating a thresholding function. ```python from pyod.utils.data import generate_data import big_o # Make sure to install package func = lambda n: n times = [] # Define function to calculate big-O number def threshold(size): contamination = 0.2 # percentage of outliers n_train = size # number of training points n_test = 50 # number of testing points # Generate sample data X_train, X_test, y_train, y_test = \ generate_data(n_train=n_train, n_test=n_test, n_features=2, contamination=contamination, random_state=9718) clf = PCA() clf.fit(X_train) y_train_scores = clf.decision_scores_ thres = IQR() scores = thres.eval(y_train_scores) # Note that some thresholders have high time complexity, typically ser repeats to 0 or 1 for them best, others = big_o.big_o(threshold, func, min_n=100, max_n=10000, n_repeats=5) print(best) ``` -------------------------------- ### Install PyThresh using Conda Source: https://github.com/kulikdm/pythresh/blob/main/docs/install.md Install PyThresh from the conda-forge channel. This is recommended if you are already using Conda for environment management. ```bash conda install -c conda-forge pythresh ``` -------------------------------- ### Import Models for Karcher Mean Example Source: https://github.com/kulikdm/pythresh/blob/main/docs/example.md Imports necessary modules from pyod and pythresh for outlier detection and thresholding. ```python from pyod.models.knn import KNN from pyod.utils.data import generate_data from pyod.utils.data import evaluate_print from pyod.utils.example import visualize from pythresh.thresholds.karch import KARCH ``` -------------------------------- ### Apply Combined Thresholding with DSN Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md This example shows how to use the COMB thresholding method with multiple DSN thresholders to evaluate decision scores. Ensure that pythresh.thresholds.comb and pythresh.thresholds.dsn are imported. ```python from pythresh.thresholds.comb import COMB from pythresh.thresholds.dsn import DSN # get outlier scores decision_scores = clf.decision_scores_ # raw outlier scores # get outlier labels with combined model thres = COMB(thresholders = [DSN(random_state=1234), DSN(random_state=42), DSN(random_state=9685), DSN(random_state=111222)]) labels = thres.eval(decision_scores) ``` -------------------------------- ### Train KNN detector and evaluate outliers with COMB and BOOT Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md This example demonstrates training a KNN detector and then using a COMB model with multiple BOOT thresholders to evaluate outlier labels. It highlights how to combine different random states for more robust outlier detection. ```python from pyod.models.knn import KNN from pythresh.thresholds.comb import COMB from pythresh.thresholds.boot import BOOT clf = KNN() clf.fit(X_train) # get outlier scores decision_scores = clf.decision_scores_ # raw outlier scores # get outlier labels with combined model thres = COMB(thresholders = [BOOT(random_state=1234), BOOT(random_state=42), BOOT(random_state=9685), BOOT(random_state=111222)]) labels = thres.eval(decision_scores) ``` -------------------------------- ### Apply RANK Method for Outlier Detection Source: https://github.com/kulikdm/pythresh/blob/main/docs/ranking.md Import necessary libraries, initialize outlier detection models and a thresholding method, and then use the RANK utility to evaluate and obtain rankings. This snippet demonstrates the basic setup for using the RANK method. ```python # Import libraries from pyod.models.knn import KNN from pyod.models.iforest import IForest from pyod.models.pca import PCA from pyod.models.mcd import MCD from pyod.models.qmcd import QMCD from pythresh.thresholds.filter import FILTER from pythresh.utils.rank import RANK # Initialize models clfs = [KNN(), IForest(), PCA(), MCD(), QMCD()] thres = FILTER() # or list of thresholder methods # Get rankings ranker = RANK(clfs, thres) rankings = ranker.eval(X) ``` -------------------------------- ### VAE Thresholding Example Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Demonstrates using the VAE thresholding method within a combined model to mitigate randomness effects. Multiple VAE instances with different random states are used to evaluate decision scores. ```python from pyod.models.knn import KNN from pythresh.thresholds.comb import COMB from pythresh.thresholds.vae import VAE clf = KNN() clf.fit(X_train) decision_scores = clf.decision_scores_ # raw outlier scores thres = COMB(thresholders = [VAE(random_state=1234), VAE(random_state=42), VAE(random_state=9685), VAE(random_state=111222)]) labels = thres.eval(decision_scores) ``` -------------------------------- ### Outlier Detection Thresholding with PyThresh Source: https://github.com/kulikdm/pythresh/blob/main/README.rst This snippet demonstrates a quickstart usage of PyThresh for outlier detection thresholding. It requires importing the necessary library and providing outlier likelihood scores. ```python from pythresh.thresholders.threshold import Threshold import numpy as np # Example outlier likelihood scores # Higher scores indicate a higher probability of being an outlier scores = np.array([0.1, 0.2, 0.7, 0.8, 0.3, 0.9, 0.4, 0.6]) # Initialize the Threshold thresholder # The 'method' can be changed to other available algorithms thresholder = Threshold(method='zscore') # Apply the thresholding to the scores # Returns a binary array: 0 for inliers, 1 for outliers thresholded_labels = thresholder.eval(scores) print(thresholded_labels) # Expected output: [0 0 1 1 0 1 0 1] ``` -------------------------------- ### Train KNN detector and evaluate with COMB thresholder Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md This example shows how to train a KNN detector and then use the COMB thresholder with multiple DECOMP instances, each with a different random state, to evaluate decision scores and obtain outlier labels. This approach helps to alleviate the effects of randomness on the thresholder's performance. ```python from pyod.models.knn import KNN from pythresh.thresholds.comb import COMB from pythresh.thresholds.decomp import DECOMP clf = KNN() clf.fit(X_train) # get outlier scores decision_scores = clf.decision_scores_ # raw outlier scores # get outlier labels with combined model thres = COMB(thresholders = [DECOMP(random_state=1234), DECOMP(random_state=42), DECOMP(random_state=9685), DECOMP(random_state=111222)]) labels = thres.eval(decision_scores) ``` -------------------------------- ### Update PyThresh using pip Source: https://github.com/kulikdm/pythresh/blob/main/docs/install.md Run this command to upgrade to the latest version if PyThresh is already installed. This ensures you have the most recent features and bug fixes. ```bash pip install --upgrade pythresh ``` -------------------------------- ### Train KNN Detector and Apply PyThresh Thresholding Source: https://github.com/kulikdm/pythresh/blob/main/docs/index.md This snippet demonstrates training a KNN outlier detector from PyOD and then using a PyThresh thresholding algorithm (CLUST) to determine outlier labels based on the detector's decision scores. Ensure PyOD and PyThresh are installed. ```python # train the KNN detector from pyod.models.knn import KNN from pythresh.thresholds.clust import CLUST clf = KNN() clf.fit(X_train) # get outlier likelihood scores decision_scores = clf.decision_scores_ # get outlier labels thres = CLUST() thres.fit(decision_scores) labels = thres.labels_ # or thres.predict(decision_scores) ``` -------------------------------- ### get_metadata_routing() Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Get metadata routing of this object. Please check User Guide on how the routing mechanism works. ```APIDOC ## get_metadata_routing() ### Description Get metadata routing of this object. Please check User Guide on how the routing mechanism works. * **Returns:** **routing** – A `MetadataRequest` encapsulating routing information. * **Return type:** MetadataRequest ``` -------------------------------- ### Initialize Models and Rank Combinations Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/03_Ranking.ipynb Select outlier detection models (KNN, IForest, PCA) and thresholding methods (KARCH, IQR, CLF). Use the RANK utility to evaluate and rank all possible combinations. ```python import numpy as np from pyod.models.knn import KNN from pyod.models.iforest import IForest from pyod.models.pca import PCA from pythresh.thresholds.karch import KARCH from pythresh.thresholds.iqr import IQR from pythresh.thresholds.clf import CLF from pythresh.utils.rank import RANK # Initialize models clfs = [KNN(), IForest(random_state=1234), PCA()] thres = [KARCH(), IQR(), CLF()] # Get rankings ranker = RANK(clfs, thres) rankings = ranker.eval(X) ``` -------------------------------- ### Get Normalized Threshold Value (Python) Source: https://github.com/kulikdm/pythresh/blob/main/docs/example.md Use this to get the normalized threshold value after evaluating outlier detection likelihood scores. The scores are normalized between 0 and 1. ```python # train kNN detector clf_name = "KNN" clf = KNN() clf.fit(X_train) scores = clf.decision_function(X_train) thres = OCSVM() thres.fit(scores) labels = thres.labels_ threshold = thres.thresh_ ``` -------------------------------- ### Initialize Data Generation Parameters Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/Compare All Thresholders.ipynb Sets up parameters for generating synthetic data, including the number of samples, outlier fraction, and cluster separation. This prepares the ground truth for outlier detection. ```python # Define the number of inliers and outliers n_samples = 200 outliers_fraction = 0.25 clusters_separation = [1.5] # Compare given detectors under given settings # Initialize the data xx, yy = np.meshgrid(np.linspace(-7, 7, 100), np.linspace(-7, 7, 100)) n_inliers = int((1. - outliers_fraction) * n_samples) n_outliers = int(outliers_fraction * n_samples) ground_truth = np.zeros(n_samples, dtype=int) ground_truth[-n_outliers:] = 1 ``` -------------------------------- ### get_params Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Get parameters for this estimator. ```APIDOC ## get_params(deep=True) Get parameters for this estimator. ### Parameters * **deep** (*bool* *,* *default=True*) If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** Parameter names mapped to their values. * **Return type:** dict ``` -------------------------------- ### get_params(deep=True) Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Gets the parameters of the estimator. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Parameters * **deep** (*bool*, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** – Parameter names mapped to their values. * **Return type:** dict ``` -------------------------------- ### VAE.get_params Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Gets the parameters of the VAE estimator. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Parameters * **deep** (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** – Parameter names mapped to their values. ### Return type dict ``` -------------------------------- ### REGR.get_params Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Gets the parameters of the REGR estimator. ```APIDOC #### get_params(deep=True) Get parameters for this estimator. ### Parameters * **deep** (*bool* *,* *default=True*) If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** Parameter names mapped to their values. * **Return type:** dict ``` -------------------------------- ### FGD.get_params Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Gets the parameters of the FGD estimator. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Parameters * **deep** (bool, default=True) - If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** - Parameter names mapped to their values. * **Return type:** dict ``` -------------------------------- ### Initialize and Fit OD and Thresholders (LODA + COMB) Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/01_Advanced.ipynb Initializes a LODA outlier detector and a COMB thresholder with multiple simple thresholders. Fits the OD to data, extracts decision scores, then fits the thresholder to these scores to obtain outlier labels. Useful for a quick ensemble of basic thresholding methods. ```python from pyod.models.loda import LODA from pythresh.thresholds.comb import COMB from pythresh.thresholds.iqr import IQR from pythresh.thresholds.dsn import DSN from pythresh.thresholds.karch import KARCH from sklearn.metrics import f1_score, matthews_corrcoef # Initialize and fit OD and thresholders od = LODA() od.fit(X) scores = od.decision_scores_ thresholders = [DSN(), IQR(), KARCH()] thresh = COMB(thresholders) thresh.fit(scores) fit_labels = thresh.labels_ # How did the unsupervised task perform, lets check the stats metric1 = round(f1_score(y, fit_labels), 2) metric2 = round(matthews_corrcoef(y, fit_labels), 2) print(f'\nThe f1 and mcc score of the outlier detection are {metric1} and {metric2} respectively') plotter(X_decomp, fit_labels) ``` -------------------------------- ### CLUST.get_params Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Gets the parameters of the CLUST estimator. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Parameters * **deep** (*bool*, default=True) If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** (*dict*) Parameter names mapped to their values. ``` -------------------------------- ### CLF.get_params Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Gets the parameters of the CLF estimator. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Parameters * **deep** (*bool*, **default=True**) If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** (*dict*) Parameter names mapped to their values. ``` -------------------------------- ### get_params(deep=True) Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Gets the parameters of the GAMGMM estimator. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Parameters * **deep** (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** – Parameter names mapped to their values. * **Return type:** dict ``` -------------------------------- ### VAE Class Initialization Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Initializes the VAE thresholding model with various configurable parameters. ```APIDOC ## VAE(verbose=False, device='cpu', latent_dims='auto', random_state=1234, epochs=100, batch_size=64, loss='kl') ### Description VAE class for Variational AutoEncoder thresholder. Use a VAE to evaluate a non-parametric means to threshold scores generated by the decision_scores where outliers are set to any value beyond the maximum minus the minimum of the reconstructed distribution probabilities after encoding. ### Parameters * **verbose** (bool, optional) - display training progress * **device** (str, optional) - device for pytorch * **latent_dims** (int, optional) - number of latent dimensions the encoder will map the scores to. Default ‘auto’ applies automatic dimensionality selection using a profile likelihood. * **random_state** (int, optional) - random seed for the normal distribution. Can also be set to None * **epochs** (int, optional) - number of epochs to train the VAE * **batch_size** (int, optional) - batch size for the dataloader during training * **loss** (str, optional) - Loss function during training - ’kl’ : use the combined negative log likelihood and Kullback-Leibler divergence - ’mmd’: use the combined negative log likelihood and maximum mean discrepancy ``` -------------------------------- ### get_params(deep=True) Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Gets the parameters of the estimator, optionally including nested objects. ```APIDOC ## get_params(deep=True) Get parameters for this estimator. * **Parameters**: **deep** (*bool* *,* *default=True*) – If True, will return the parameters for this estimator and contained subobjects that are estimators. * **Returns**: **params** – Parameter names mapped to their values. * **Return type**: dict ``` -------------------------------- ### get_params(deep=True) Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Gets the parameters of the estimator, optionally including nested objects. ```APIDOC ## get_params(deep=True) ### Description Get parameters for this estimator. ### Parameters * **deep** (*bool* *,* *default=True*) – If True, will return the parameters for this estimator and contained subobjects that are estimators. ### Returns * **params** – Parameter names mapped to their values. * **Return type:** dict ``` -------------------------------- ### Import Necessary Libraries Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/Compare All Thresholders.ipynb Imports all required libraries for outlier detection, data manipulation, and visualization. Includes specific thresholding modules from pythresh and models from pyod. ```python from __future__ import division from __future__ import print_function import os import sys from time import time # temporary solution for relative imports in case pyod is not installed # if pythresh is installed, no need to use the following line sys.path.append( os.path.abspath(os.path.join(os.path.dirname("__file__"), '..'))) import numpy as np from numpy import percentile import matplotlib.pyplot as plt import matplotlib.font_manager from pyod.models.iforest import IForest from pythresh.thresholds.iqr import IQR from pythresh.thresholds.mad import MAD from pythresh.thresholds.fwfm import FWFM from pythresh.thresholds.yj import YJ from pythresh.thresholds.zscore import ZSCORE from pythresh.thresholds.aucp import AUCP from pythresh.thresholds.qmcd import QMCD from pythresh.thresholds.fgd import FGD from pythresh.thresholds.dsn import DSN from pythresh.thresholds.clf import CLF from pythresh.thresholds.filter import FILTER from pythresh.thresholds.wind import WIND from pythresh.thresholds.eb import EB from pythresh.thresholds.regr import REGR from pythresh.thresholds.boot import BOOT from pythresh.thresholds.mcst import MCST from pythresh.thresholds.hist import HIST from pythresh.thresholds.moll import MOLL from pythresh.thresholds.chau import CHAU from pythresh.thresholds.gesd import GESD from pythresh.thresholds.mtt import MTT from pythresh.thresholds.karch import KARCH from pythresh.thresholds.ocsvm import OCSVM from pythresh.thresholds.clust import CLUST from pythresh.thresholds.decomp import DECOMP from pythresh.thresholds.meta import META from pythresh.thresholds.vae import VAE from pythresh.thresholds.cpd import CPD from pythresh.thresholds.gamgmm import GAMGMM from pythresh.thresholds.mixmod import MIXMOD ``` -------------------------------- ### get_metadata_routing() Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Get metadata routing of this object. This method allows inspection of the routing mechanism for metadata. ```APIDOC ## get_metadata_routing() Get metadata routing of this object. Please check User Guide on how the routing mechanism works. * **Returns:** **routing** – A `MetadataRequest` encapsulating routing information. * **Return type:** MetadataRequest ``` -------------------------------- ### Initialize and Fit OD and Thresholders (LODA + MIXMOD) Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/01_Advanced.ipynb Initializes a LODA outlier detector and a MIXMOD thresholder. Fits the OD to data, extracts decision scores, then fits the MIXMOD thresholder to these scores to obtain outlier labels. MIXMOD is useful when interpretability is necessary and simple thresholders are insufficient. ```python from pyod.models.loda import LODA from pythresh.thresholds.mixmod import MIXMOD from sklearn.metrics import f1_score, matthews_corrcoef # Initialize and fit ODs and thresholder od = LODA() od.fit(X) scores = od.decision_scores_ thresh = MIXMOD() thresh.fit(scores) fit_labels = thresh.labels_ # How did the unsupervised task perform, lets check the stats metric1 = round(f1_score(y, fit_labels), 2) metric2 = round(matthews_corrcoef(y, fit_labels), 2) print(f'\nThe f1 and mcc score of the outlier detection are {metric1} and {metric2} respectively') plotter(X_decomp, fit_labels) ``` -------------------------------- ### get_metadata_routing Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Retrieves the metadata routing of the object. Consult the User Guide for details on the routing mechanism. ```APIDOC ## get_metadata_routing() ### Description Get metadata routing of this object. Please check User Guide on how the routing mechanism works. ### Returns - **routing** (MetadataRequest) – A `MetadataRequest` encapsulating routing information. ``` -------------------------------- ### Load and Standardize Data Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/03_Ranking.ipynb Import a dataset, load it using scipy.io.loadmat, and standardize the features using pyod.utils.utility.standardizer. This prepares the data for outlier detection. ```python import os from scipy.io import loadmat from pyod.utils.utility import standardizer file = os.path.join('data', 'cardio.mat') mat = loadmat(file) X = mat['X'].astype(float) y = mat['y'].ravel().astype(int) X = standardizer(X) ``` -------------------------------- ### Apply Confidence Thresholding (Cardio Dataset) Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/02_Confidence.ipynb Visualizes the original labels of the cardio dataset, then applies Isolation Forest and IQR thresholding with a 99% confidence interval to identify uncertain points. Results are visualized. ```python # To get a sense lets see the real labels first decomp = PCA(n_components=2) X_decomp = decomp.fit_transform(X) plotter(X_decomp, y) # Now lets apply the OD, thresholder and confidence test od = IForest(random_state=1234) od.fit(X) scores = od.decision_scores_ thresh = IQR() # Alpha is set so that we get a 99% confidence level # Split is set to 0.1 for sample sizes # N_test runs 100 bootstrap tests confidence = CONF(thresh, alpha=0.01, split=0.1, n_test=100) unc_idx = confidence.eval(scores) thresh.fit(scores) fit_labels = thresh.labels_ fit_labels[unc_idx] = 2 plotter(X_decomp, fit_labels) ``` -------------------------------- ### get_params(deep=True) Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Get parameters for this estimator. This method retrieves the estimator's parameters, optionally including those of contained sub-estimators. ```APIDOC ## get_params(deep=True) Get parameters for this estimator. * **Parameters:** **deep** (*bool* *,* *default=True*) – If True, will return the parameters for this estimator and contained subobjects that are estimators. * **Returns:** **params** – Parameter names mapped to their values. * **Return type:** dict ``` -------------------------------- ### FGD Class Initialization Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.thresholds.md Initializes the FGD thresholder with optional fallback action and random state. ```APIDOC ## FGD(fallback='warn', random_state=1234) ### Description FGD class for Fixed Gradient Descent thresholder. Use the fixed gradient descent to evaluate a non-parametric means to threshold scores generated by the decision_scores where outliers are set to any value beyond where the first derivative of the kde with respect to the decision scores passes the mean of the first and second inflection points. See [[QJC21](#id42)] for details. ### Parameters * **fallback** (str, optional, default='warn') - The action to take for thresholders when their criterion are not met. In these cases when set to ‘ignore’ on eval and fit all train data is set to inliers and the threshold is set to max of the train scores + eps. Passing ‘warn’ will do the same as ‘ignore’ but also produce a warning. If ‘raise’, the thresholder raises a ValueError. * **random_state** (int, optional, default=1234) - Random seed for the random number generators of the thresholders. Can also be set to None. ``` -------------------------------- ### Single Benchmark Application Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/Benchmark_Thresholder.ipynb Applies a single standard benchmark to evaluate classifiers and thresholders. Requires importing necessary libraries and defining lists of classifiers and thresholders. ```python from pyod.models.knn import KNN from pyod.models.iforest import IForest from pyod.models.pca import PCA from pyod.models.mcd import MCD from pyod.models.hbos import HBOS from pyod.models.kde import KDE from pyod.models.gmm import GMM from pyod.models.copod import COPOD from pythresh.thresholds.iqr import IQR from pythresh.thresholds.mad import MAD from pythresh.thresholds.fwfm import FWFM from pythresh.thresholds.yj import YJ from pythresh.thresholds.ocsvm import OCSVM from pythresh.thresholds.regr import REGR clfs = [KNN, GMM] thres = [IQR(), MAD()] # Apply single standrd benchmark df = benchmark(clfs, thres, method='single') ``` -------------------------------- ### Train KNN Detector and Get Scores Source: https://github.com/kulikdm/pythresh/blob/main/README.rst Trains a KNN outlier detector and retrieves its decision scores. This is useful for initial outlier detection before applying thresholding. ```python from pyod.models.knn import KNN from pythresh.thresholds.karch import KARCH clf = KNN() clf.fit(X_train) # get outlier likelihood scores decision_scores = clf.decision_scores_ # get outlier labels thres = KARCH() hres.fit(decision_scores) labels = thres.labels_ # or thres.predict(decision_scores) ``` -------------------------------- ### Load and Standardize Data Source: https://github.com/kulikdm/pythresh/blob/main/notebooks/00_Introduction.ipynb Import necessary libraries and load the dataset from a .mat file. The data is then standardized using pyod.utils.utility.standardizer. ```python import os from scipy.io import loadmat from pyod.utils.utility import standardizer file = os.path.join('data', 'musk.mat') mat = loadmat(file) X = mat['X'].astype(float) y = mat['y'].ravel().astype(int) X = standardizer(X) ``` -------------------------------- ### Apply CONF Method for Musk Dataset Source: https://github.com/kulikdm/pythresh/blob/main/docs/confidence.md Applies the CONF method using IQR as the initial thresholding strategy and IForest for outlier detection. Visualizes inliers, outliers, and uncertain points after PCA dimensionality reduction. Ensure the 'data' directory contains 'musk.mat'. ```python import os import matplotlib.pyplot as plt import numpy as np from pyod.models.iforest import IForest from pyod.utils.utility import standardizer from pythresh.thresholds.iqr import IQR from pythresh.utils.conf import CONF from scipy.io import loadmat from sklearn.decomposition import PCA mat_file = 'musk.mat' mat = loadmat(os.path.join('data', mat_file)) X = mat['X'] y = mat['y'].ravel() X = standardizer(X) clf = IForest(random_state=1234) clf.fit(X) scores = clf.decision_scores_ thres = IQR() labels = thres.eval(scores) confidence = CONF(thres, alpha=0.05, split=0.2) unc_idx = confidence.eval(scores) decomp = PCA(n_components=2, random_state=1234) X = decomp.fit_transform(X) uncertains = X[unc_idx] outliers = X[labels==1] inliers = X[labels==0] fig = plt.figure(figsize=(18, 12)) plt.plot(inliers[:, 0], inliers[:, 1], 'y.', label='Inliers', markersize=10) plt.plot(outliers[:, 0], outliers[:, 1], 'r.', label='Outliers', markersize=11) plt.plot(uncertains[:, 0], uncertains[:, 1], 'b.', label='Uncertains', markersize=12) plt.legend() plt.show() ``` -------------------------------- ### IQR Methods Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.md Provides access to methods for the IQR thresholding algorithm, including threshold calculation, anomaly score retrieval, evaluation, fitting, metadata routing, parameter retrieval, prediction, and parameter setting. ```APIDOC ## IQR.thresh_ ### Description Calculates the threshold value using the IQR method. ### Method Call ### Parameters None ### Response The calculated threshold value. ## IQR.dscores_ ### Description Retrieves the anomaly scores calculated by the IQR method. ### Method Call ### Parameters None ### Response Anomaly scores. ## IQR.eval() ### Description Evaluates the IQR thresholding algorithm. ### Method Call ### Parameters None ### Response Evaluation results. ## IQR.fit() ### Description Fits the IQR thresholding algorithm to the data. ### Method Call ### Parameters None ### Response None ## IQR.get_metadata_routing() ### Description Retrieves the metadata routing for the IQR algorithm. ### Method Call ### Parameters None ### Response Metadata routing information. ## IQR.get_params() ### Description Gets the parameters of the IQR algorithm. ### Method Call ### Parameters None ### Response Parameters of the IQR algorithm. ## IQR.predict() ### Description Predicts using the IQR algorithm. ### Method Call ### Parameters None ### Response Prediction results. ## IQR.set_params() ### Description Sets the parameters for the IQR algorithm. ### Method Call ### Parameters None ### Response None ``` -------------------------------- ### CONF Methods Source: https://github.com/kulikdm/pythresh/blob/main/docs/pythresh.md Methods available for the CONF utility class. ```APIDOC ## CONF.eval() ### Description Evaluates the CONF utility. ### Method eval ### Parameters None ### Response #### Success Response (200) - **score** (float) - The evaluation score. ### Response Example { "example": "0.90" } ```