Оценки как внутренние компоненты детектора¶
Детекторы - это просто менеджеры над объектами оценок. Они представляют собой способы оценки каждой строки в каждой подвыборке.
Warning
Оценки имеют ограниченную поддержку типов данных.
applybn
реализует две основные группы оценок: на основе модели и на основе близости.
Подробнее читайте в Руководстве пользователя.
Оценка¶
Bases: ABC
An abstract base class for implementing scoring mechanisms.
Source code in applybn/anomaly_detection/scores/score.py
__init__(verbose=1)
¶
Initializes the Score object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
verbose
|
int
|
The verbosity level for logging. Default is 1. |
1
|
score(X)
abstractmethod
¶
Abstract method to compute scores for the given input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input data to be scored. |
required |
Raises:
Type | Description |
---|---|
NotImplementedError
|
This method must be implemented by subclasses. |
Source code in applybn/anomaly_detection/scores/score.py
Оценки на основе близости¶
Оценка локальных выбросов¶
Bases: ProximityBasedScore
A class for computing outlier scores using the Local Outlier Factor (LOF) algorithm.
Source code in applybn/anomaly_detection/scores/proximity_based.py
__init__(proximity_steps=5, verbose=1, **kwargs)
¶
Initializes the LocalOutlierScore object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
proximity_steps
|
int
|
The number of proximity steps to perform. Default is 5. |
5
|
verbose
|
int
|
The verbosity level for logging. Default is 1. |
1
|
**kwargs
|
Additional parameters for the Local Outlier Factor algorithm. |
{}
|
Source code in applybn/anomaly_detection/scores/proximity_based.py
local_score(X)
¶
Computes the local outlier scores for the given data using the LOF algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input data. |
required |
Returns:
Type | Description |
---|---|
np.ndarray: An array of negative outlier factors, where higher values indicate more abnormal data points. |
Source code in applybn/anomaly_detection/scores/proximity_based.py
Оценка на основе Isolation Forest¶
Bases: ProximityBasedScore
A class for computing outlier scores using the Isolation Forest algorithm.
Source code in applybn/anomaly_detection/scores/proximity_based.py
__init__(proximity_steps=5, verbose=1, **kwargs)
¶
Initializes the IsolationForestScore object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Additional parameters for the Isolation Forest algorithm. |
{}
|
Source code in applybn/anomaly_detection/scores/proximity_based.py
local_score(X)
¶
Computes the outlier scores for the given data using the Isolation Forest algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input data. |
required |
Returns:
Type | Description |
---|---|
np.ndarray: An array of negative decision function values, where higher values indicate more abnormal data points. |
Source code in applybn/anomaly_detection/scores/proximity_based.py
Оценки на основе модели¶
Bases: Score
A generic score class that computes scores based on a provided model. Model must implement the predict_proba method.
Source code in applybn/anomaly_detection/scores/model_based.py
__init__(model)
¶
Initializes the ModelBasedScore object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model
|
The model used to compute probabilities for scoring. |
required |
score(X)
¶
Computes the score for the input data using the model's predicted probabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
The input data to be scored. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The predicted probabilities for the input data. |
Source code in applybn/anomaly_detection/scores/model_based.py
Оценка на основе BN¶
Bases: Score
A score class based on a Bayesian network (BN).
Attributes:
Name | Type | Description |
---|---|---|
bn |
The Bayesian network used for scoring. |
|
encoding |
The encoding for discrete variables. |
|
child_nodes |
The child nodes in the Bayesian network. |
|
verbose |
The verbosity level for logging. |
Source code in applybn/anomaly_detection/scores/model_based.py
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 |
|
__init__(bn, encoding, verbose=1)
¶
Initializes the BNBasedScore object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bn
|
bamt_network
|
The Bayesian network used for scoring. |
required |
encoding
|
dict
|
The encoding for discrete variables. |
required |
verbose
|
int
|
The verbosity level for logging. |
1
|
Source code in applybn/anomaly_detection/scores/model_based.py
local_score(X, node_name)
¶
Computes the local score for a specific node in the Bayesian network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input data. |
required |
node_name
|
The name of the node to compute the score for. |
required |
Returns:
Type | Description |
---|---|
np.ndarray: An array of local scores for the specified node. |
Source code in applybn/anomaly_detection/scores/model_based.py
score(X)
¶
Computes the scores for all child nodes in the Bayesian network.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input data. |
required |
Returns:
Type | Description |
---|---|
np.ndarray: A 2D array of scores for all child nodes. |
Source code in applybn/anomaly_detection/scores/model_based.py
Оценка на основе IQR¶
Bases: BNBasedScore
A score class that uses the Interquartile Range (IQR) for anomaly detection.
Source code in applybn/anomaly_detection/scores/model_based.py
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 |
|
__init__(bn, encoding, iqr_sensivity=1.0, verbose=1)
¶
Initializes the IQRBasedScore object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bn
|
bamt_network
|
The Bayesian network used for scoring. |
required |
encoding
|
dict
|
The encoding for discrete variables. |
required |
iqr_sensivity
|
float
|
The sensitivity factor for IQR-based scoring. |
1.0
|
verbose
|
int
|
The verbosity level for logging. |
1
|
Source code in applybn/anomaly_detection/scores/model_based.py
local_score(X, node_name)
¶
Computes the local IQR-based score for a specific node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input data. |
required |
node_name
|
The name of the node to compute the score for. |
required |
Returns:
Type | Description |
---|---|
np.ndarray: An array of local scores for the specified node. |
Source code in applybn/anomaly_detection/scores/model_based.py
score_iqr(upper, lower, y, max_distance, min_distance)
staticmethod
¶
Computes the IQR-based score for a given value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
upper
|
float
|
The upper bound of the IQR. |
required |
lower
|
float
|
The lower bound of the IQR. |
required |
y
|
float
|
The value to score. |
required |
max_distance
|
float
|
The maximum distance for scaling. |
required |
min_distance
|
float
|
The minimum distance for scaling. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the closest value does not match either upper or lower bound. |
Returns:
Name | Type | Description |
---|---|---|
float |
The IQR-based score. |
Source code in applybn/anomaly_detection/scores/model_based.py
Оценка на основе отношения условных вероятностей¶
Bases: BNBasedScore
A score class that uses conditional probability ratios for anomaly detection.
Source code in applybn/anomaly_detection/scores/model_based.py
__init__(bn, encoding, verbose=1)
¶
Initializes the CondRatioScore object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bn
|
bamt_network
|
The Bayesian network used for scoring. |
required |
encoding
|
dict
|
The encoding for discrete variables. |
required |
verbose
|
int
|
The verbosity level for logging. |
1
|
Source code in applybn/anomaly_detection/scores/model_based.py
local_score(X, node_name)
¶
Computes the local conditional ratio score for a specific node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input data. |
required |
node_name
|
str
|
The name of the node to compute the score for. |
required |
Returns:
Type | Description |
---|---|
np.ndarray: An array of local scores for the specified node. |
Source code in applybn/anomaly_detection/scores/model_based.py
score_proba_ratio(sample, X_value, cond_dist)
staticmethod
¶
Computes the conditional probability ratio score.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sample
|
Series
|
The sample data. |
required |
X_value
|
str
|
The value to score. |
required |
cond_dist
|
tuple
|
The conditional distribution. |
required |
Returns:
Name | Type | Description |
---|---|---|
float |
The conditional probability ratio score. |
Source code in applybn/anomaly_detection/scores/model_based.py
Комбинированная оценка на основе IQR и отношения условных вероятностей¶
Bases: BNBasedScore
A score class that combines IQR-based scoring and probability ratio scoring for anomaly detection.
Source code in applybn/anomaly_detection/scores/model_based.py
__init__(bn, encoding, scores, verbose=1)
¶
Initializes the CombinedIQRandProbRatioScore object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bn
|
bamt_network
|
The Bayesian network used for scoring. |
required |
encoding
|
dict
|
The encoding for discrete variables. |
required |
scores
|
dict
|
A dictionary containing scoring objects for continuous and discrete variables. |
required |
verbose
|
int
|
The verbosity level for logging. |
1
|
Source code in applybn/anomaly_detection/scores/model_based.py
local_score(X, node_name)
¶
Computes the local score for a specific node by combining IQR-based and probability ratio scoring.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
DataFrame
|
The input data. |
required |
node_name
|
str
|
The name of the node to compute the score for. |
required |
Returns:
Type | Description |
---|---|
np.ndarray: An array of local scores for the specified node. |