This commit is contained in:
xuhongzuo 2023-11-09 16:09:08 +08:00
parent 112484fbd2
commit 54dbe70e2a
3 changed files with 174 additions and 0 deletions

95
docs/start.examples.rst Normal file
View File

@ -0,0 +1,95 @@
Examples
=========
Directly Use Detection Models
------------------------------
DeepOD can be used in a few lines of code.
This API style is the same with `Sklean <https://github.com/scikit-learn/scikit-learn>`_ and `PyOD <https://github.com/yzhao062/pyod>`_.
**for tabular anomaly detection:**
.. code-block:: python
# unsupervised methods
from deepod.models.tabular import DeepSVDD
clf = DeepSVDD()
clf.fit(X_train, y=None)
scores = clf.decision_function(X_test)
# weakly-supervised methods
from deepod.models.tabular import DevNet
clf = DevNet()
clf.fit(X_train, y=semi_y) # semi_y uses 1 for known anomalies, and 0 for unlabeled data
scores = clf.decision_function(X_test)
# evaluation of tabular anomaly detection
from deepod.metrics import tabular_metrics
auc, ap, f1 = tabular_metrics(y_test, scores)
**for time series anomaly detection:**
.. code-block:: python
# time series anomaly detection methods
from deepod.models.time_series import TimesNet
clf = TimesNet()
clf.fit(X_train)
scores = clf.decision_function(X_test)
# evaluation of time series anomaly detection
from deepod.metrics import ts_metrics
from deepod.metrics import point_adjustment # execute point adjustment for time series ad
eval_metrics = ts_metrics(labels, scores)
adj_eval_metrics = ts_metrics(labels, point_adjustment(labels, scores))
Testbed
--------
Testbed contains the whole process of testing an anomaly detection model, including data loading, preprocessing, anomaly detection, and evaluation.
Please refer to ``testbed/``
* ``testbed/testbed_unsupervised_ad.py`` is for testing unsupervised tabular anomaly detection models.
* ``testbed/testbed_unsupervised_tsad.py`` is for testing unsupervised time-series anomaly detection models.
Key arguments:
* ``--input_dir``: name of the folder that contains datasets (.csv, .npy)
* ``--dataset``: "FULL" represents testing all the files within the folder, or a list of dataset names using commas to split them (e.g., "10_cover*,20_letter*")
* ``--model``: anomaly detection model name
* ``--runs``: how many times running the detection model, finally report an average performance with standard deviation values
Example:
1. Download `ADBench <https://github.com/Minqi824/ADBench/tree/main/adbench/datasets/>`_ datasets.
2. modify the ``dataset_root`` variable as the directory of the dataset.
3. ``input_dir`` is the sub-folder name of the ``dataset_root``, e.g., ``Classical`` or ``NLP_by_BERT``.
4. use the following command in the bash
.. code-block:: bash
cd DeepOD
pip install .
cd testbed
python testbed_unsupervised_ad.py --model DeepIsolationForest --runs 5 --input_dir ADBench

34
docs/start.install.rst Normal file
View File

@ -0,0 +1,34 @@
Installation
============
It is recommended to use **pip** for installation. Please make sure
**the latest version** is installed, as DeepOD is updated frequently:
.. code-block:: bash
pip install deepod # normal install
pip install --upgrade deepod # or update if needed
Alternatively, you could clone and run setup.py file:
.. code-block:: bash
git clone https://github.com/xuhongzuo/deepod.git
cd pyod
pip install .
**Required Dependencies**\ :
* Python 3.7+
* numpy>=1.19
* scipy>=1.5.1
* scikit_learn>=0.20.0
* pandas>=1.0.0
* torch>1.10.0,<1.13.1
* ray==2.6.1
* pyarrow>=11.0.0
* einops

45
docs/start.model_save.rst Normal file
View File

@ -0,0 +1,45 @@
Model Save & Load
==================
The detection model class has ``save_model`` and ``load_model`` functions.
We take the `DeepSVDD` model for example.
.. code-block:: python
from deepod.models import DeepSVDD
# training an anomaly detection model
model = DeepSVDD() # or any other models in DeepOD
model.fit(X_train) # training
path = 'save_file.pkl'
model.save_model(path) # save trained model at the assigned path
# directly load trained model from path
model = DeepSVDD.load_model(path)
model.decision_function(X_test)
# or
model.predict(X_test)
You can also directly use pickle for saving and loading DeepOD models.
.. code-block:: python
import pickle
from deepod.models import DeepSVDD
model = DeepSVDD()
model.fit(X_train)
with open('save_file.pkl', 'wb'):
pickle.dump(model)
with open('save_file.pkl', 'rb')
model = pickle.load(f)
model.decision_function(X_test)