File size: 1,834 Bytes
303f2f6 783a1d4 96f4763 303f2f6 783a1d4 303f2f6 783a1d4 303f2f6 783a1d4 303f2f6 783a1d4 303f2f6 783a1d4 2bfaffc 96f4763 783a1d4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import os
import pandas as pd
from sklearn.ensemble import IsolationForest
import numpy as np
from sklearn.model_selection import train_test_split
import gradio as gr
import matplotlib.pyplot as plt
import timm
#Data preparation
n_samples, n_outliers = 120, 40
rng = np.random.RandomState(0)
covariance = np.array([[0.5, -0.1], [0.7, 0.4]])
cluster_1 = 0.4 * rng.randn(n_samples, 2) @ covariance + np.array([2, 2]) # general deformed cluster
cluster_2 = 0.3 * rng.randn(n_samples, 2) + np.array([-2, -2]) # spherical cluster
outliers = rng.uniform(low=-4, high=4, size=(n_outliers, 2))
X = np.concatenate([cluster_1, cluster_2, outliers]) #120+120+40 = 280 with 2D
y = np.concatenate(
[np.ones((2 * n_samples), dtype=int), -np.ones((n_outliers), dtype=int)]
)
#Visualize the data as a scatter plot
def visualize_input_data():
fig = plt.figure(1, facecolor="w", figsize=(5, 5))
scatter = plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
handles, labels = scatter.legend_elements()
plt.axis("square")
plt.legend(handles=handles, labels=["outliers", "inliers"], title="true class")
plt.title("Gaussian inliers with \nuniformly distributed outliers")
# plt.show()
return fig
title = " An example using IsolationForest for anomaly detection."
with gr.Blocks(title=title) as demo:
gr.Markdown(f"# {title}")
gr.Markdown(" **https://scikit-learn.org/stable/auto_examples/ensemble/plot_isolation_forest.html#sphx-glr-auto-examples-ensemble-plot-isolation-forest-py**")
btn = gr.Button(value="Visualize input dataset")
btn.click(visualize_input_data, outputs= gr.Plot(label='Visualizing input dataset') )
model_reloaded = timm.create_model('hf_hub:sklearn-docs/anomaly-detection', pretrained=True)
gr.Markdown( f"## In progress")
demo.launch() |