-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Start implementing OC-SVM * More implementation on OC-SVM (only testing still needed) * Added tests + update changelog.rst
- Loading branch information
1 parent
aa42991
commit fe4a447
Showing
9 changed files
with
100 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
One Class Support Vector Machine | ||
================================ | ||
|
||
.. autoclass:: dtaianomaly.anomaly_detection.OneClassSupportVectorMachine | ||
:inherited-members: | ||
:members: |
59 changes: 59 additions & 0 deletions
59
dtaianomaly/anomaly_detection/OneClassSupportVectorMachine.py
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,59 @@ | ||
|
||
from pyod.models.ocsvm import OCSVM | ||
from dtaianomaly.anomaly_detection.BaseDetector import Supervision | ||
from dtaianomaly.anomaly_detection.PyODAnomalyDetector import PyODAnomalyDetector | ||
|
||
|
||
class OneClassSupportVectorMachine(PyODAnomalyDetector): | ||
""" | ||
Anomaly detector based on One-Class Support Vector Machines (OC-SVM). | ||
The OC-SVM [Scholkopf1999support]_ uses a Support Vector Machine to learn | ||
a boundary around the normal behavior with minimal margin. New data can | ||
then be identified as anomaly or not, depending on if the data falls within | ||
this boundary (and thus is normal) or outside the boundary (and thus is | ||
anomalous). | ||
Notes | ||
----- | ||
The OC-SVM inherets from :py:class:`~dtaianomaly.anomaly_detection.PyodAnomalyDetector`. | ||
Parameters | ||
---------- | ||
window_size: int or str | ||
The window size to use for extracting sliding windows from the time series. This | ||
value will be passed to :py:meth:`~dtaianomaly.anomaly_detection.compute_window_size`. | ||
stride: int, default=1 | ||
The stride, i.e., the step size for extracting sliding windows from the time series. | ||
**kwargs: | ||
Arguments to be passed to the PyOD OC-SVM | ||
Attributes | ||
---------- | ||
window_size_: int | ||
The effectively used window size for this anomaly detector | ||
pyod_detector_ : OCSVM | ||
A OCSVM-detector of PyOD | ||
Examples | ||
-------- | ||
>>> from dtaianomaly.anomaly_detection import OneClassSupportVectorMachine | ||
>>> from dtaianomaly.data import demonstration_time_series | ||
>>> x, y = demonstration_time_series() | ||
>>> ocsvm = OneClassSupportVectorMachine(10).fit(x) | ||
>>> ocsvm.decision_function(x) | ||
array([-0.7442125 , -1.57019847, -1.86868112, ..., 13.33883568, | ||
12.6492399 , 11.8761641 ]) | ||
References | ||
---------- | ||
.. [Scholkopf1999support] Schölkopf, Bernhard, et al. "Support vector method | ||
for novelty detection." Advances in neural information processing systems 12 | ||
(1999). | ||
""" | ||
|
||
def _initialize_detector(self, **kwargs) -> OCSVM: | ||
return OCSVM(**kwargs) | ||
|
||
def _supervision(self): | ||
return Supervision.SEMI_SUPERVISED |
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
15 changes: 15 additions & 0 deletions
15
tests/anomaly_detection/test_OneClassSupportVectorMachine.py
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,15 @@ | ||
|
||
from dtaianomaly.anomaly_detection import OneClassSupportVectorMachine, Supervision | ||
|
||
|
||
class TestOneClassSupportVectorMachine: | ||
|
||
def test_supervision(self): | ||
detector = OneClassSupportVectorMachine(1) | ||
assert detector.supervision == Supervision.SEMI_SUPERVISED | ||
|
||
def test_str(self): | ||
assert str(OneClassSupportVectorMachine(5)) == "OneClassSupportVectorMachine(window_size=5)" | ||
assert str(OneClassSupportVectorMachine('fft')) == "OneClassSupportVectorMachine(window_size='fft')" | ||
assert str(OneClassSupportVectorMachine(15, 3)) == "OneClassSupportVectorMachine(window_size=15,stride=3)" | ||
assert str(OneClassSupportVectorMachine(25, kernel='poly')) == "OneClassSupportVectorMachine(window_size=25,kernel='poly')" |
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
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