Jayabalambika's picture
Update app.py
64eb9a6
raw
history blame
3.39 kB
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
from skops import hub_utils
import pickle
#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
def download_model_skops():
repo_id="sklearn-docs/anomaly-detection"
download_repo = "downloaded-model"
hub_utils.download(repo_id=repo_id, dst=download_repo)
# repo_copy = mkdtemp(prefix="skops")
# hub_utils.download(repo_id=repo_id, dst=repo_copy, token=token)
# print(os.listdir(download_repo))
from sklearn.inspection import DecisionBoundaryDisplay
def plot_decision_boundary(classifier):
disp = DecisionBoundaryDisplay.from_estimator(
classifier,
X,
response_method="predict",
alpha=0.5,
)
fig = plt.figure(1, facecolor="w", figsize=(5, 5))
scatter = plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
disp.ax_.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolor="k")
handles, labels = scatter.legend_elements()
disp.ax_.set_title("Binary decision boundary \nof IsolationForest")
plt.axis("square")
plt.legend(handles=handles, labels=["outliers", "inliers"], title="true class")
# plt.savefig('decision_boundary.png',dpi=300, bbox_inches = "tight")
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') )
# download
repo_id="sklearn-docs/anomaly-detection"
download_repo = "downloaded-model"
hub_utils.download(repo_id=repo_id, dst=download_repo)
if os.listdir(download_repo):
# hub_utils.download(repo_id=repo_id, dst=download_repo)
# print("Empty directory")
# print(os.listdir(download_repo))
loaded_model = pickle.load(open('isolation_forest.pkl', 'rb'))
btn = gr.Button(value="Plot decision boundary")
btn.click(plot_decision_boundary, inputs=[loaded_model], outputs= gr.Plot(label='Visualizing input dataset') )
gr.Markdown( f"## Success")
demo.launch()