freeEDU/Log-Decoder
0
1"""2The implementation of the SVM model for anomaly detection.3 4Authors: 5 LogPAI Team6 7Reference: 8 [1] Yinglung Liang, Yanyong Zhang, Hui Xiong, Ramendra Sahoo. Failure Prediction 9 in IBM BlueGene/L Event Logs. IEEE International Conference on Data Mining10 (ICDM), 2007.11 12"""13 14import numpy as np15from sklearn import svm16from ..utils import metrics17 18class SVM(object):19 20 def __init__(self, penalty='l1', tol=0.1, C=1, dual=False, class_weight=None, 21 max_iter=100):22 """ The Invariants Mining model for anomaly detection23 Arguments24 ---------25 See SVM API: https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html26 27 Attributes28 ----------29 classifier: object, the classifier for anomaly detection30 31 """32 self.classifier = svm.LinearSVC(penalty=penalty, tol=tol, C=C, dual=dual, 33 class_weight=class_weight, max_iter=max_iter)34 35 def fit(self, X, y):36 """37 Arguments38 ---------39 X: ndarray, the event count matrix of shape num_instances-by-num_events40 """41 print('====== Model summary ======')42 self.classifier.fit(X, y)43 44 def predict(self, X):45 """ Predict anomalies with mined invariants46 47 Arguments48 ---------49 X: the input event count matrix50 51 Returns52 -------53 y_pred: ndarray, the predicted label vector of shape (num_instances,)54 """55 56 y_pred = self.classifier.predict(X)57 return y_pred58 59 def evaluate(self, X, y_true):60 print('====== Evaluation summary ======')61 y_pred = self.predict(X)62 precision, recall, f1 = metrics(y_pred, y_true)63 print('Precision: {:.3f}, recall: {:.3f}, F1-measure: {:.3f}\n'.format(precision, recall, f1))64 return precision, recall, f165 