cristal.helper_classes.base module#
Base classes for outlier detection methods
, plotters
,
inverters
, incrementers
, and polynomial basis functions
.
Also contains the NotFittedError
exception.
- class cristal.helper_classes.base.BaseDetector#
Bases:
ABC
Base class for outlier detection methods
- assert_shape_fitted(x)#
Asserts that the input array x has the correct shape to use the model after it has been fitted.
- Parameters:
x (np.ndarray) – The input data to check. Should be a 2D array with shape (N, d) where N is the number of samples and d is the number of features (the same as during fitting).
- Raises:
NotFittedError – If the model has not been fitted yet.
ValueError – If the input array does not have the expected shape.
- static assert_shape_unfitted(x)#
Asserts that the input array x has the correct shape to fit the model.
- Parameters:
x (np.ndarray) – The input data to check. Should be a 2D array with shape (N, d) where N is the number of samples and d is the number of features.
- Raises:
ValueError – If the input array does not have the expected shape.
- copy()#
Returns a copy of the model.
- Return type:
Self
- Returns:
A copy of the current model.
- Return type:
Self
- abstractmethod decision_function(x)#
Computes the decision function for the samples in x.
- Return type:
ndarray
- Parameters:
x (np.ndarray (L, d)) – The input data to compute the decision function.
- Returns:
The decision function values for each sample.
- Return type:
np.ndarray (L,)
- abstractmethod eval_update(x)#
Evaluates the model on the samples in x and updates it with inliers. This method iterates over each sample in x.
- Return type:
ndarray
- Parameters:
x (np.ndarray (L, d)) – The input data to evaluate and update the model.
- Returns:
The decision function values for each sample.
- Return type:
np.ndarray (L,)
- abstractmethod fit(x)#
Generates a model that fit dataset x.
- Return type:
Self
- Parameters:
x (np.ndarray (N, d)) – The input data to fit the model.
- Returns:
The fitted model.
- Return type:
Self
- fit_predict(x)#
Fits the model with x and predicts the outlier labels for the samples in x.
- Return type:
ndarray
- Parameters:
x (np.ndarray (N, d)) – The input data to fit and predict.
- Returns:
The predicted labels for each sample.
- Return type:
np.ndarray (N,)
- abstractmethod is_fitted()#
Checks if the model is fitted.
- Return type:
bool
- Returns:
True if the model is fitted, False otherwise.
- Return type:
bool
- abstractmethod load_model(model_dict)#
Loads the model from a dictionary.
- Return type:
Self
- Parameters:
model_dict (dict) – The dictionary containing the model parameters returned by save_model().
- Returns:
The loaded model.
- Return type:
Self
- abstractmethod method_name()#
Returns the name of the method.
- Return type:
str
- Returns:
The name of the method.
- Return type:
str
- abstractmethod predict(x)#
Predicts the outlier labels for the samples in x.
- Return type:
ndarray
- Parameters:
x (np.ndarray (L, d)) – The input data to predict the labels.
- Returns:
The predicted labels for each sample.
- Return type:
np.ndarray (L,)
- abstractmethod predict_update(x)#
Predict the model on the samples in x and updates it with inliers.
- Return type:
ndarray
- Parameters:
x (np.ndarray (L, d)) – The input data to evaluate and update the model.
- Returns:
The predicted labels for each sample.
- Return type:
np.ndarray (L,)
- abstractmethod save_model()#
Saves the model as a dictionary.
- Return type:
dict
- Returns:
The model represented as a dictionary.
- Return type:
dict
- abstractmethod score_samples(x)#
Computes the outlier scores for the samples in x.
- Return type:
ndarray
- Parameters:
x (np.ndarray (L, d)) – The input data to compute the scores.
- Returns:
The outlier scores for each sample.
- Return type:
np.ndarray (L,)
- abstractmethod update(x)#
Updates the current model with instances in x.
- Return type:
Self
- Parameters:
x (np.ndarray (N', d)) – The input data to update the model.
- Returns:
The updated model.
- Return type:
Self
- class cristal.helper_classes.base.BaseIncrementer#
Bases:
ABC
Base class for moments matrix incrementers.
- abstractmethod static increment(mm, x, n, inv_class, sym=True)#
Increment the inverse moments matrix (and possibly the moments matrix).
- Parameters:
mm (MomentsMatrix) – The moments matrix to increment.
x (np.ndarray (N', d)) – The input data.
n (int) – The number of points integrated in the moments matrix.
inv_class (type[BaseInverter]) – The inversion class to use.
sym (bool, optional) – Whether to consider the matrix as symmetric, by default True
-
update_moments_matrix:
bool
= False# Whether the method updates the moments matrix during incrementing
- class cristal.helper_classes.base.BaseInverter#
Bases:
ABC
Base class for matrix inversion methods.
- abstractmethod static invert(matrix)#
Invert the given matrix.
- Return type:
ndarray
- Parameters:
matrix (np.ndarray (n, n)) – The input matrix to invert.
- Returns:
The inverse of the input matrix.
- Return type:
np.ndarray (n, n)
- class cristal.helper_classes.base.BasePlotter#
Bases:
ABC
Base class for outlier detection results plotters. This requires that data is 2-dimensional.
- abstractmethod boundary(x, *, n_x1, n_x2, show, save, save_title, close)#
Generates the boundary plot and optionally show, save and close.
- Return type:
None
|tuple
[Figure
,Axes
]- Parameters:
x (np.ndarray) – The input data to plot, shape (n_samples, n_features=2).
n_x1 (int) – Number of points along the first dimension for the grid.
n_x2 (int) – Number of points along the second dimension for the grid.
show (bool) – Whether to show the plot.
save (bool) – Whether to save the plot to a file.
save_title (str) – Title for the saved plot file.
close (bool) – Whether to close the plot or return it after saving or showing.
- Returns:
The figure and axes objects if not closing the plot, otherwise None.
- Return type:
None | tuple[Figure, Axes]
- abstractmethod levelset(x, *, n_x1, n_x2, levels, percentiles, show, save, save_title, close)#
Generates the levelset plot and optionally show, save and close.
- Return type:
None
|tuple
[Figure
,Axes
]- Parameters:
x (np.ndarray) – The input data to plot, shape (n_samples, n_features=2).
n_x1 (int) – Number of points along the first dimension for the grid.
n_x2 (int) – Number of points along the second dimension for the grid.
levels (list[int | float] | None) – The levels to plot.
percentiles (list[int | float] | None) – The percentiles to compute and plot as levels.
show (bool) – Whether to show the plot.
save (bool) – Whether to save the plot to a file.
save_title (str) – Title for the saved plot file.
close (bool) – Whether to close the plot or return it after saving or showing.
- Returns:
The figure and axes objects if not closing the plot, otherwise None.
- Return type:
None | tuple[Figure, Axes]
- class cristal.helper_classes.base.BasePolynomialBasis#
Bases:
ABC
Base class for polynomial basis functions.
- abstractmethod static func(x, monomials_matrix)#
Apply the polynomial basis function to the input data.
- Return type:
ndarray
- Parameters:
x (np.ndarray (1, d) or (d,)) – The input data.
monomials_matrix (np.ndarray (s(n), d)) – The monomials matrix. Should be of shape (s(n), d) where s(n) is the number of monomials and d is the dimension of the input data.
- Returns:
The transformed input data.
- Return type:
np.ndarray (s(n), d)
- class cristal.helper_classes.base.BaseRegularizer#
Bases:
ABC
Base class for regularization methods.
- abstractmethod static regularizer(n, d, C)#
Compute the regularization value.
- Return type:
float
- Parameters:
n (int) – The polynomial basis maximum degree.
d (int) – The dimension of the input data.
C (float | int) – A constant used in the regularization computation.
- Returns:
The computed regularization value.
- Return type:
float