-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored examples. Fixed evaluation comments. int requiremnet for y…
…_pred changed to float.
- Loading branch information
1 parent
0d047b7
commit 9827eb4
Showing
8 changed files
with
77 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,41 @@ | ||
from sklearn.utils import shuffle | ||
from tqdm import tqdm | ||
# Import modules. | ||
from pysad.evaluation import AUROCMetric | ||
from pysad.models import LODA | ||
from pysad.models import xStream | ||
from pysad.utils import ArrayStreamer | ||
from pysad.transform.ensemble import AverageScoreEnsembler | ||
from pysad.utils import Data | ||
from sklearn.utils import shuffle | ||
from tqdm import tqdm | ||
import numpy as np | ||
|
||
# This example demonstrates the usage of an ensembling method. | ||
if __name__ == '__main__': | ||
np.random.seed(61) | ||
np.random.seed(61) # Fix random seed. | ||
|
||
data = Data("data") | ||
X_all, y_all = data.get_data("arrhythmia.mat") | ||
X_all, y_all = shuffle(X_all, y_all) | ||
iterator = ArrayStreamer(shuffle=False) | ||
auroc = AUROCMetric() | ||
X_all, y_all = data.get_data("arrhythmia.mat") # Load Aryhytmia data. | ||
X_all, y_all = shuffle(X_all, y_all) # Shuffle data. | ||
iterator = ArrayStreamer(shuffle=False) # Create streamer to simulate streaming data. | ||
auroc = AUROCMetric() # Tracker of area under receiver-operating- characteristics curve metric. | ||
|
||
models = [ | ||
models = [ # Models to be ensembled. | ||
xStream(), | ||
LODA() | ||
] | ||
ensembler = AverageScoreEnsembler() | ||
ensembler = AverageScoreEnsembler() # Ensembler module. | ||
|
||
for X, y in tqdm(iterator.iter(X_all[100:], y_all[100:])): | ||
for X, y in tqdm(iterator.iter(X_all, y_all)): # Iterate over examples. | ||
model_scores = np.empty(len(models), dtype=np.float) | ||
|
||
# Fit & Score via for each model. | ||
for i, model in enumerate(models): | ||
model.fit_partial(X) | ||
model_scores[i] = model.score_partial(X) | ||
|
||
score = ensembler.fit_transform_partial(model_scores) | ||
auroc.update(y, score) | ||
score = ensembler.fit_transform_partial(model_scores) # fit to ensembler model and get ensembled score. | ||
|
||
auroc.update(y, score) # update AUROC metric. | ||
|
||
# Output score. | ||
print("AUROC: ", auroc.get()) |
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,17 +1,22 @@ | ||
# Import modules. | ||
from pysad.models import xStream | ||
from pysad.transform.probability_calibration import ConformalProbabilityCalibrator | ||
from pysad.utils import Data | ||
import numpy as np | ||
|
||
# This example demonstrates the usage of the probability calibrators. | ||
if __name__ == "__main__": | ||
model = xStream() | ||
calibrator = ConformalProbabilityCalibrator(windowed=True, window_size=300) | ||
streaming_data = Data().get_iterator("arrhythmia.mat") | ||
np.random.seed(61) # Fix seed. | ||
|
||
for i, (x, y_true) in enumerate(streaming_data): | ||
anomaly_score = model.fit_score_partial(x) | ||
model = xStream() # Init model. | ||
calibrator = ConformalProbabilityCalibrator(windowed=True, window_size=300) # Init probability calibrator. | ||
streaming_data = Data().get_iterator("arrhythmia.mat") # Get streamer. | ||
|
||
calibrated_score = calibrator.fit_transform(anomaly_score) | ||
print(calibrated_score) | ||
if calibrated_score < 0.05: # ıf probabability is less than 5%. | ||
for i, (x, y_true) in enumerate(streaming_data): # Stream data. | ||
anomaly_score = model.fit_score_partial(x) # Fit to an instance x and score it. | ||
|
||
calibrated_score = calibrator.fit_transform(anomaly_score) # Fit & calibrate score. | ||
|
||
# Output if the instance is anomalous. | ||
if calibrated_score < 0.05: # If probability is less than 5%. | ||
print(f"Alert: {i}th data point is anomalous.") |
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,18 +1,23 @@ | ||
import numpy as np | ||
|
||
# Import modules. | ||
from pysad.statistics import AverageMeter | ||
from pysad.statistics import VarianceMeter | ||
import numpy as np | ||
|
||
# This example shows the usage of statistics module for streaming data. | ||
if __name__ == '__main__': | ||
|
||
# Init data with mean 0 and standard deviation 1. | ||
X = np.random.randn(1000) | ||
|
||
# Init statistics trackers for mean and variance. | ||
avg_meter = AverageMeter() | ||
var_meter = VarianceMeter() | ||
|
||
for i in range(1000): | ||
# Update statistics trackers. | ||
avg_meter.update(X[i]) | ||
var_meter.update(X[i]) | ||
|
||
# Output resulting statistics. | ||
print(f"Average: {avg_meter.get()}, Standard deviation: {np.sqrt(var_meter.get())}") | ||
# It is close to random normal distribution with mean 0 and std 1 as we init the array via np.random.rand. |
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,14 +1,16 @@ | ||
# Import modules. | ||
from pysad.evaluation import AUROCMetric | ||
from pysad.models import LODA | ||
from pysad.utils import Data | ||
|
||
model = LODA() | ||
metric = AUROCMetric() | ||
streaming_data = Data().get_iterator("arrhythmia.mat") | ||
model = LODA() # Init model | ||
metric = AUROCMetric() # Init area under receiver-operating- characteristics curve metric | ||
streaming_data = Data().get_iterator("arrhythmia.mat") # Get data streamer. | ||
|
||
for x, y_true in streaming_data: | ||
anomaly_score = model.fit_score_partial(x) | ||
for x, y_true in streaming_data: # Stream data. | ||
anomaly_score = model.fit_score_partial(x) # Fit the instance to model and score the instance. | ||
|
||
metric.update(y_true, anomaly_score) | ||
metric.update(y_true, anomaly_score) # Update the AUROC metric. | ||
|
||
# Output the resulting AUROCMetric. | ||
print(f"Area under ROC metric is {metric.get()}.") |
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