Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from sklearn import svm | |
| from sklearn.covariance import EllipticEnvelope | |
| from sklearn.ensemble import IsolationForest | |
| from sklearn.neighbors import LocalOutlierFactor | |
| from sklearn.linear_model import SGDOneClassSVM | |
| from sklearn.kernel_approximation import Nystroem | |
| from sklearn.pipeline import make_pipeline | |
| from sklearn.datasets import make_blobs, make_moons | |
| import gradio as gr | |
| import pandas as pd | |
| import time | |
| # Helper function to prepare data | |
| def prepare_data(input_data, n_samples, outliers_fraction=0.0): | |
| n_outliers = int(outliers_fraction * n_samples) | |
| n_inliers = n_samples - n_outliers | |
| blobs_params = dict(random_state=0, n_samples=n_inliers, n_features=2) | |
| DATA_MAPPING = { | |
| "Central Blob": make_blobs(centers=[[0, 0], [0, 0]], cluster_std=0.5, **blobs_params)[0], | |
| "Two Blobs": make_blobs(centers=[[2, 2], [-2, -2]], cluster_std=[0.5, 0.5], **blobs_params)[0], | |
| "Blob with Noise": make_blobs(centers=[[2, 2], [-2, -2]], cluster_std=[1.5, 0.3], **blobs_params)[0], | |
| "Moons": 4.0 * (make_moons(n_samples=n_samples, noise=0.05, random_state=0)[0] - np.array([0.5, 0.25])), | |
| "Noise": 14.0 * (np.random.RandomState(42).rand(n_samples, 2) - 0.5), | |
| } | |
| X = DATA_MAPPING[input_data] | |
| rng = np.random.RandomState(42) | |
| outliers = rng.uniform(low=-6, high=6, size=(n_outliers, 2)) | |
| X = np.concatenate([X, outliers], axis=0) | |
| labels = np.array(["Normal"] * len(X)) | |
| labels[-len(outliers):] = "Anomaly" | |
| return X, labels | |
| # Function to train models and generate plots | |
| def train_models(input_data, outliers_fraction, n_samples, clf_name): | |
| X, _ = prepare_data(input_data, n_samples, outliers_fraction) | |
| # Define classifiers | |
| NAME_CLF_MAPPING = { | |
| "Robust covariance": EllipticEnvelope(contamination=outliers_fraction), | |
| "One-Class SVM": svm.OneClassSVM(nu=outliers_fraction, kernel="rbf", gamma=0.1), | |
| "One-Class SVM (SGD)": make_pipeline( | |
| Nystroem(gamma=0.1, random_state=42, n_components=150), | |
| SGDOneClassSVM( | |
| nu=outliers_fraction, | |
| shuffle=True, | |
| fit_intercept=True, | |
| random_state=42, | |
| tol=1e-6, | |
| ), | |
| ), | |
| "Isolation Forest": IsolationForest(contamination=outliers_fraction, random_state=42), | |
| "Local Outlier Factor": LocalOutlierFactor(n_neighbors=35, contamination=outliers_fraction), | |
| } | |
| clf = NAME_CLF_MAPPING[clf_name] | |
| xx, yy = np.meshgrid(np.linspace(-7, 7, 150), np.linspace(-7, 7, 150)) | |
| t0 = time.time() | |
| if clf_name == "Local Outlier Factor": | |
| y_pred = clf.fit_predict(X) | |
| else: | |
| clf.fit(X) | |
| y_pred = clf.predict(X) | |
| t1 = time.time() | |
| # Plotting | |
| plt.figure(figsize=(5, 5)) | |
| if clf_name != "Local Outlier Factor": | |
| Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]) | |
| Z = Z.reshape(xx.shape) | |
| plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="black") | |
| colors = np.array(["#377eb8", "#ff7f00"]) | |
| plt.scatter(X[:, 0], X[:, 1], s=30, color=colors[(y_pred + 1) // 2]) | |
| plt.title(f"{clf_name} ({t1 - t0:.2f}s)") | |
| plt.xlim(-7, 7) | |
| plt.ylim(-7, 7) | |
| plt.xticks(()) | |
| plt.yticks(()) | |
| return plt.gcf() | |
| # Function to detect anomalies and generate anomaly records | |
| def detect_anomalies(input_data, n_samples, outliers_fraction, model_name): | |
| X, labels = prepare_data(input_data, n_samples, outliers_fraction) | |
| # Define classifiers | |
| NAME_CLF_MAPPING = { | |
| "Robust covariance": EllipticEnvelope(contamination=outliers_fraction), | |
| "One-Class SVM": svm.OneClassSVM(nu=outliers_fraction, kernel="rbf", gamma=0.1), | |
| "One-Class SVM (SGD)": make_pipeline( | |
| Nystroem(gamma=0.1, random_state=42, n_components=150), | |
| SGDOneClassSVM( | |
| nu=outliers_fraction, | |
| shuffle=True, | |
| fit_intercept=True, | |
| random_state=42, | |
| tol=1e-6, | |
| ), | |
| ), | |
| "Isolation Forest": IsolationForest(contamination=outliers_fraction, random_state=42), | |
| "Local Outlier Factor": LocalOutlierFactor(n_neighbors=35, contamination=outliers_fraction), | |
| } | |
| clf = NAME_CLF_MAPPING[model_name] | |
| if model_name == "Local Outlier Factor": | |
| clf.fit(X) | |
| scores = -clf.negative_outlier_factor_ | |
| else: | |
| clf.fit(X) | |
| scores = -clf.decision_function(X) | |
| # Normalize scores to a consistent range | |
| scores = (scores - scores.min()) / (scores.max() - scores.min()) | |
| # Create DataFrame | |
| df = pd.DataFrame({ | |
| "Feature1": X[:, 0], | |
| "Feature2": X[:, 1], | |
| "Anomaly_Score": scores, | |
| "Anomaly_Label": labels, | |
| }) | |
| # Sort by anomaly score in descending order | |
| df = df.sort_values("Anomaly_Score", ascending=False).reset_index(drop=True) | |
| return df | |
| # Function to get anomaly samples | |
| def get_anomaly_samples(input_data, n_samples, outliers_fraction, model_name): | |
| df = detect_anomalies(input_data, n_samples, outliers_fraction, model_name) | |
| # Debugging: Check the distribution of anomaly labels | |
| print("Anomaly Label Counts:") | |
| print(df["Anomaly_Label"].value_counts()) | |
| # Top 10 anomalies | |
| top_10 = df[df["Anomaly_Label"] == "Anomaly"].head(10) | |
| # If no anomalies are found, show a message | |
| if top_10.empty: | |
| top_10 = pd.DataFrame({"Message": ["No anomalies found"]}) | |
| # Middle 10 (mixed) | |
| mid_start = len(df) // 2 - 5 | |
| middle_10 = df.iloc[mid_start: mid_start + 10] | |
| # Bottom 10 normals | |
| bottom_10 = df[df["Anomaly_Label"] == "Normal"].tail(10) | |
| return top_10, middle_10, bottom_10 | |
| # Gradio Interface | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## Anomaly Detection App") | |
| input_data = gr.Radio( | |
| choices=["Central Blob", "Two Blobs", "Blob with Noise", "Moons", "Noise"], | |
| value="Moons", | |
| label="Dataset" | |
| ) | |
| n_samples = gr.Slider(minimum=10, maximum=10000, step=25, value=500, label="Number of Samples") | |
| outliers_fraction = gr.Slider(minimum=0.001, maximum=0.999, step=0.1, value=0.2, label="Fraction of Outliers") | |
| model_dropdown = gr.Dropdown(choices=["Robust covariance", "One-Class SVM", "One-Class SVM (SGD)", "Isolation Forest", "Local Outlier Factor"], label="Select Model") | |
| # Anomaly Samples Output | |
| top_table = gr.Dataframe(label="Top 10 Anomalies") | |
| middle_table = gr.Dataframe(label="Middle 10 Records") | |
| bottom_table = gr.Dataframe(label="Bottom 10 Normals") | |
| anomaly_samples_button = gr.Button("Show Anomaly Samples") | |
| anomaly_samples_button.click( | |
| fn=get_anomaly_samples, | |
| inputs=[input_data, n_samples, outliers_fraction, model_dropdown], | |
| outputs=[top_table, middle_table, bottom_table], | |
| ) | |
| demo.launch(debug=True) | |