Skip to content

Commit d648e1a

Browse files
committed
add fallback using oneclass svm with sgd, this fallback will be used upon failure of the sub model _build_model methods
1 parent ecd417d commit d648e1a

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

ads/opctl/operator/lowcode/anomaly/model/base_model.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ def generate_report(self):
4848
import matplotlib.pyplot as plt
4949

5050
start_time = time.time()
51-
anomaly_output = self._build_model()
51+
# fallback using sklearn oneclasssvm when the sub model _build_model fails
52+
try:
53+
anomaly_output = self._build_model()
54+
except Exception as e:
55+
anomaly_output = self._fallback_build_model()
56+
5257
elapsed_time = time.time() - start_time
5358

5459
summary_metrics = None
@@ -318,6 +323,44 @@ def _save_report(
318323
f"generated and placed to the: {output_dir}."
319324
)
320325

326+
def _fallback_build_model(self):
327+
"""
328+
Fallback method for the sub model _build_model method.
329+
"""
330+
logger.warn(
331+
"The build_model method has failed for the model: {}. "
332+
"A fallback model will be built.".format(self.spec.model)
333+
)
334+
from sklearn import linear_model
335+
date_column = self.spec.datetime_column.name
336+
dataset = self.datasets
337+
338+
full_data_dict = dataset.full_data_dict
339+
340+
anomaly_output = AnomalyOutput(date_column=date_column)
341+
342+
# map the output as per anomaly dataset class, 1: outlier, 0: inlier
343+
outlier_map = {1: 0, -1: 1}
344+
345+
# Iterate over the full_data_dict items
346+
for target, df in full_data_dict.items():
347+
est = linear_model.SGDOneClassSVM(random_state=42)
348+
est.fit(df[target].values.reshape(-1, 1))
349+
y_pred = np.vectorize(outlier_map.get)(est.predict(df[target].values.reshape(-1, 1)))
350+
scores = est.score_samples(df[target].values.reshape(-1, 1))
351+
352+
anomaly = pd.DataFrame({
353+
date_column: df[date_column],
354+
OutputColumns.ANOMALY_COL: y_pred
355+
}).reset_index(drop=True)
356+
score = pd.DataFrame({
357+
date_column: df[date_column],
358+
OutputColumns.SCORE_COL: [item for item in scores]
359+
}).reset_index(drop=True)
360+
anomaly_output.add_output(target, anomaly, score)
361+
362+
return anomaly_output
363+
321364
@abstractmethod
322365
def _generate_report(self):
323366
"""

0 commit comments

Comments
 (0)