rtik007 commited on
Commit
13c5031
·
verified ·
1 Parent(s): bc4ace7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -2
app.py CHANGED
@@ -33,7 +33,56 @@ def prepare_data(input_data, n_samples, outliers_fraction=0.0):
33
  labels[-len(outliers):] = "Anomaly"
34
  return X, labels
35
 
36
- # Function to train and detect anomalies
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  def detect_anomalies(input_data, n_samples, outliers_fraction, model_name):
38
  X, labels = prepare_data(input_data, n_samples, outliers_fraction)
39
 
@@ -81,7 +130,7 @@ def detect_anomalies(input_data, n_samples, outliers_fraction, model_name):
81
 
82
  return df
83
 
84
- # Function to fetch anomaly records based on the selected model
85
  def get_anomaly_samples(input_data, n_samples, outliers_fraction, model_name):
86
  df = detect_anomalies(input_data, n_samples, outliers_fraction, model_name)
87
 
 
33
  labels[-len(outliers):] = "Anomaly"
34
  return X, labels
35
 
36
+ # Function to train models and generate plots
37
+ def train_models(input_data, outliers_fraction, n_samples, clf_name):
38
+ X, _ = prepare_data(input_data, n_samples, outliers_fraction)
39
+
40
+ # Define classifiers
41
+ NAME_CLF_MAPPING = {
42
+ "Robust covariance": EllipticEnvelope(contamination=outliers_fraction),
43
+ "One-Class SVM": svm.OneClassSVM(nu=outliers_fraction, kernel="rbf", gamma=0.1),
44
+ "One-Class SVM (SGD)": make_pipeline(
45
+ Nystroem(gamma=0.1, random_state=42, n_components=150),
46
+ SGDOneClassSVM(
47
+ nu=outliers_fraction,
48
+ shuffle=True,
49
+ fit_intercept=True,
50
+ random_state=42,
51
+ tol=1e-6,
52
+ ),
53
+ ),
54
+ "Isolation Forest": IsolationForest(contamination=outliers_fraction, random_state=42),
55
+ "Local Outlier Factor": LocalOutlierFactor(n_neighbors=35, contamination=outliers_fraction),
56
+ }
57
+
58
+ clf = NAME_CLF_MAPPING[clf_name]
59
+ xx, yy = np.meshgrid(np.linspace(-7, 7, 150), np.linspace(-7, 7, 150))
60
+
61
+ t0 = time.time()
62
+ if clf_name == "Local Outlier Factor":
63
+ y_pred = clf.fit_predict(X)
64
+ else:
65
+ clf.fit(X)
66
+ y_pred = clf.predict(X)
67
+ t1 = time.time()
68
+
69
+ # Plotting
70
+ plt.figure(figsize=(5, 5))
71
+ if clf_name != "Local Outlier Factor":
72
+ Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
73
+ Z = Z.reshape(xx.shape)
74
+ plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="black")
75
+
76
+ colors = np.array(["#377eb8", "#ff7f00"])
77
+ plt.scatter(X[:, 0], X[:, 1], s=30, color=colors[(y_pred + 1) // 2])
78
+ plt.title(f"{clf_name} ({t1 - t0:.2f}s)")
79
+ plt.xlim(-7, 7)
80
+ plt.ylim(-7, 7)
81
+ plt.xticks(())
82
+ plt.yticks(())
83
+ return plt.gcf()
84
+
85
+ # Function to detect anomalies and generate anomaly records
86
  def detect_anomalies(input_data, n_samples, outliers_fraction, model_name):
87
  X, labels = prepare_data(input_data, n_samples, outliers_fraction)
88
 
 
130
 
131
  return df
132
 
133
+ # Function to get anomaly samples
134
  def get_anomaly_samples(input_data, n_samples, outliers_fraction, model_name):
135
  df = detect_anomalies(input_data, n_samples, outliers_fraction, model_name)
136