Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,55 +33,6 @@ 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 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)
|
|
@@ -107,18 +58,20 @@ def detect_anomalies(input_data, n_samples, outliers_fraction, model_name):
|
|
| 107 |
clf = NAME_CLF_MAPPING[model_name]
|
| 108 |
if model_name == "Local Outlier Factor":
|
| 109 |
scores = -clf.fit_predict(X) # Negative for LOF: higher is more anomalous
|
|
|
|
| 110 |
else:
|
| 111 |
clf.fit(X)
|
| 112 |
scores = -clf.decision_function(X) # Higher score indicates greater anomaly
|
|
|
|
| 113 |
|
| 114 |
# Normalize scores to [0, 1]
|
| 115 |
-
|
| 116 |
|
| 117 |
# Create DataFrame
|
| 118 |
df = pd.DataFrame({
|
| 119 |
"Feature1": X[:, 0],
|
| 120 |
"Feature2": X[:, 1],
|
| 121 |
-
"Anomaly_Score":
|
| 122 |
"Anomaly_Label": labels,
|
| 123 |
})
|
| 124 |
|
|
|
|
| 33 |
labels[-len(outliers):] = "Anomaly"
|
| 34 |
return X, labels
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Function to detect anomalies and generate anomaly records
|
| 37 |
def detect_anomalies(input_data, n_samples, outliers_fraction, model_name):
|
| 38 |
X, labels = prepare_data(input_data, n_samples, outliers_fraction)
|
|
|
|
| 58 |
clf = NAME_CLF_MAPPING[model_name]
|
| 59 |
if model_name == "Local Outlier Factor":
|
| 60 |
scores = -clf.fit_predict(X) # Negative for LOF: higher is more anomalous
|
| 61 |
+
anomaly_scores = clf.negative_outlier_factor_ # LOF specific
|
| 62 |
else:
|
| 63 |
clf.fit(X)
|
| 64 |
scores = -clf.decision_function(X) # Higher score indicates greater anomaly
|
| 65 |
+
anomaly_scores = scores
|
| 66 |
|
| 67 |
# Normalize scores to [0, 1]
|
| 68 |
+
normalized_scores = (anomaly_scores - anomaly_scores.min()) / (anomaly_scores.max() - anomaly_scores.min())
|
| 69 |
|
| 70 |
# Create DataFrame
|
| 71 |
df = pd.DataFrame({
|
| 72 |
"Feature1": X[:, 0],
|
| 73 |
"Feature2": X[:, 1],
|
| 74 |
+
"Anomaly_Score": normalized_scores,
|
| 75 |
"Anomaly_Label": labels,
|
| 76 |
})
|
| 77 |
|