An app that generated data and does anomaly detection and visualize the results
Drop files here
or click to upload
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.ensemble import IsolationForest
# Page title
st.title("Anomaly Detection App")
# Data generation
st.header("Generate Data")
num_samples = st.sidebar.slider("Number of samples:", 100, 1000, 500)
random_state = st.sidebar.slider("Random State:", 0, 100, 42)
np.random.seed(random_state)
X = np.random.randn(num_samples, 2) * 0.5 + np.array([[0], [0]])
# Adding some anomalies
a = np.random.uniform(low=-3, high=3, size=(50, 2))
X = np.concatenate((X, a), axis=0)
# Visualizing generated data
st.subheader("Generated Data")
plt.scatter(X[:, 0], X[:, 1], color='blue')
plt.title("Generated Data Points")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.grid()
st.pyplot(plt)
# Anomaly detection
st.header("Anomaly Detection")
clf = IsolationForest(contamination=0.1, random_state=random_state)
clf.fit(X)
# Predict anomalies
predictions = clf.predict(X)
# Anomalies are labeled as -1
anomalies = X[predictions == -1]
# Visualize results
st.subheader("Anomaly Detection Results")
plt.scatter(X[:, 0], X[:, 1], color='blue', label='Sample Data')
plt.scatter(anomalies[:, 0], anomalies[:, 1], color='red', label='Anomalies')
plt.title("Detected Anomalies")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend()
plt.grid()
st.pyplot(plt)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?