Skip to content

Commit

Permalink
Create one_class_svm.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent d57dd87 commit e9245cc
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions projects/piguardian/ai_ml/one_class_svm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np
from sklearn.svm import OneClassSVM
from sklearn.preprocessing import StandardScaler

class OneClassSVMAnomalyDetector:
def __init__(self, kernel='rbf', gamma=0.1, nu=0.1):
self.kernel = kernel
self.gamma = gamma
self.nu = nu
self.svm = OneClassSVM(kernel=kernel, gamma=gamma, nu=nu)
self.scaler = StandardScaler()

def fit(self, X):
self.scaler.fit(X)
X_scaled = self.scaler.transform(X)
self.svm.fit(X_scaled)

def predict(self, X):
X_scaled = self.scaler.transform(X)
return self.svm.predict(X_scaled)

def decision_function(self, X):
X_scaled = self.scaler.transform(X)
return self.svm.decision_function(X_scaled)

def score_samples(self, X):
X_scaled = self.scaler.transform(X)
return self.svm.score_samples(X_scaled)

0 comments on commit e9245cc

Please sign in to comment.