-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: static threshold estimator (#136)
Signed-off-by: Avik Basu <[email protected]>
- Loading branch information
Showing
4 changed files
with
92 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from numalogic.models.threshold._std import StdDevThreshold | ||
from numalogic.models.threshold._static import StaticThreshold | ||
|
||
__all__ = ["StdDevThreshold"] | ||
__all__ = ["StdDevThreshold", "StaticThreshold"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2022 The Numaproj Authors. | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import numpy.typing as npt | ||
from sklearn.base import BaseEstimator | ||
from typing_extensions import Self | ||
|
||
|
||
class StaticThreshold(BaseEstimator): | ||
r""" | ||
Simple and stateless static thresholding as an estimator. | ||
Values more than upper_limit is considered an outlier, | ||
and are given an outlier_score. | ||
Values less than the upper_limit is considered an inlier, | ||
and are given an inlier_score. | ||
Args: | ||
upper_limit: upper threshold | ||
outlier_score: static score given to values above upper threshold; | ||
this has to be greater than inlier_score | ||
inlier_score: static score given to values below upper threshold | ||
""" | ||
__slots__ = ("upper_limit", "outlier_score", "inlier_score") | ||
|
||
def __init__(self, upper_limit: float, outlier_score: float = 10.0, inlier_score: float = 0.5): | ||
self.upper_limit = upper_limit | ||
self.outlier_score = outlier_score | ||
self.inlier_score = inlier_score | ||
|
||
assert ( | ||
self.outlier_score > self.inlier_score | ||
), "Outlier score needs to be greater than inlier score" | ||
|
||
def fit(self, _: npt.NDArray[float]) -> Self: | ||
"""Does not do anything. Only for API compatibility""" | ||
return self | ||
|
||
def predict(self, x_test: npt.NDArray[float]) -> npt.NDArray[float]: | ||
""" | ||
Returns an array of same shape as input. | ||
1 denotes anomaly. | ||
""" | ||
x_test = x_test.copy() | ||
x_test[x_test < self.upper_limit] = 0.0 | ||
x_test[x_test >= self.upper_limit] = 1.0 | ||
return x_test | ||
|
||
def score_samples(self, x_test: npt.NDArray[float]) -> npt.NDArray[float]: | ||
""" | ||
Returns an array of same shape as input | ||
with values being anomaly scores. | ||
""" | ||
x_test = x_test.copy() | ||
x_test[x_test < self.upper_limit] = self.inlier_score | ||
x_test[x_test >= self.upper_limit] = self.outlier_score | ||
return x_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters