/var/opt/nydus/ops/mysql/ai/ml/__pycache__
Edit: /var/opt/nydus/ops/mysql/ai/ml/__pycache__/outlier.cpython-312.pyc (8048B)
Nj d Z ddlmZmZ ddlZddlZddlm Z ddl
mZ ddlm
Z
ddlmZ ddlmZ d Zd
eeej( f deeej( f fdZ G d
dee Zy)zOutlier/anomaly detection utilities for MySQL Connector/Python.
Provides a scikit-learn compatible wrapper using HeatWave to score anomalies.
)OptionalUnionN)OutlierMixin)
MyBaseMLModel)ML_TASK) copy_dict)MySQLConnectionAbstractgh㈵>probreturnc | t j | t dt z
}t j |d|z
z S )z
Compute logit (logodds) for a probability, clipping to avoid numerical overflow.
Args:
prob: Scalar or array of probability values in (0,1).
Returns:
logit-transformed probabilities.
)npclipEPSlog)r
results D/opt/nydus/tmp/pip-target-a90h98xg/lib/python/mysql/ai/ml/outlier.py_get_logitsr 0 s2 WWT3C
(F
66&AJ'(( c
D e Zd ZdZ d
dedee dee dee fdZde e
j ej f d ej fd
Zde e
j ej f d ej fdZde e
j ej f d ej fdZy)MyAnomalyDetectora
MySQL HeatWave scikit-learn compatible anomaly/outlier detector.
Flags samples as outliers when the probability of being an anomaly
exceeds a user-tunable threshold.
Includes helpers to obtain decision scores and anomaly probabilities
for ranking.
Args:
db_connection (MySQLConnectionAbstract): Active MySQL DB connection.
model_name (str, optional): Custom model name in the database.
fit_extra_options (dict, optional): Extra options for fitting.
score_extra_options (dict, optional): Extra options for scoring/prediction.
Attributes:
boundary: Decision threshold boundary in logit space. Derived from
trained model's catalog info
Methods:
predict(X): Predict outlier/inlier labels.
score_samples(X): Compute anomaly (normal class) logit scores.
decision_function(X): Compute signed score above/below threshold for ranking.
N
db_connection
model_namefit_extra_optionsscore_extra_optionsc t j | |t j || t | | _ d| _ y)a2
Initialize an anomaly detector instance with threshold and extra options.
Args:
db_connection: Active MySQL DB connection.
model_name: Optional model name in DB.
fit_extra_options: Optional extra fit options.
score_extra_options: Optional extra scoring options.
Raises:
ValueError: If outlier_threshold is not in (0,1).
DatabaseError:
If a database connection issue occurs.
If an operational error occurs during execution.
)r r N)r __init__r ANOMALY_DETECTIONr r boundary)selfr r r r s r r zMyAnomalyDetector.__init__W s> , %%!/
$--@#A )-
r Xr c T t j | j | dk dd S )a
Predict outlier/inlier binary labels for input samples.
Args:
X: Samples to predict on.
Returns:
ndarray: Values are -1 for outliers, +1 for inliers, as per scikit-learn convention.
Raises:
DatabaseError:
If provided options are invalid or unsupported,
or if the model is not initialized, i.e., fit or import has not
been called
If a database connection issue occurs.
If an operational error occurs during execution.
DatabaseError:
If provided options are invalid or unsupported,
or if the model is not initialized, i.e., fit or import has not
been called
If a database connection issue occurs.
If an operational error occurs during execution.
g r
)r wheredecision_function)r r! s r predictzMyAnomalyDetector.predictw s' 6 xx..q1C7Q??r c | j | }| j U| j }|t d |d d j dd }|t d t d|z
| _ || j z
S )a
Compute signed distance to the outlier threshold.
Args:
X: Samples to predict on.
Returns:
ndarray: Score > 0 means inlier, < 0 means outlier; |value| gives margin.
Raises:
DatabaseError:
If provided options are invalid or unsupported,
or if the model is not initialized, i.e., fit or import has not
been called
If a database connection issue occurs.
If an operational error occurs during execution.
ValueError:
If the provided model info does not provide threshold
Nz Model does not exist in catalog.model_metadatatraining_paramsanomaly_detection_thresholdzzTrained model is outdated and does not support threshold. Try retraining or using an existing, trained model with MyModel.g ?)
score_samplesr get_model_info
ValueErrorgetr )r r!
sample_scores
model_info thresholds r r% z#MyAnomalyDetector.decision_function s . **1-
== ,,.J! !CDD"#345FGKK-tI W (i8DMt}},,r c | j j || j }t |d j d j S )aJ
Compute normal probability logit score for each sample.
Used for ranking, thresholding.
Args:
X: Samples to score.
Returns:
ndarray: Logit scores based on "normal" class probability.
Raises:
DatabaseError:
If provided options are invalid or unsupported,
or if the model is not initialized, i.e., fit or import has not
been called
If a database connection issue occurs.
If an operational error occurs during execution.
)options
ml_resultsc | d d S )N
probabilitiesnormal )xs r
z1MyAnomalyDetector.score_samples..