Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +88 -0
- innomatics_logo.png +0 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from sklearn.datasets import make_classification, make_circles, make_blobs, make_moons
|
| 5 |
+
from sklearn.model_selection import train_test_split, learning_curve
|
| 6 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 7 |
+
from sklearn.metrics import accuracy_score, f1_score
|
| 8 |
+
from mlxtend.plotting import plot_decision_regions
|
| 9 |
+
|
| 10 |
+
# π Display image (Logo)
|
| 11 |
+
st.image("innomatics_logo.png", width=600)
|
| 12 |
+
|
| 13 |
+
# π Streamlit App Title
|
| 14 |
+
st.title("π KNN Classifier: Decision Boundaries & Learning Curve")
|
| 15 |
+
|
| 16 |
+
# π Select dataset
|
| 17 |
+
st.sidebar.header("π Select Dataset")
|
| 18 |
+
data = st.sidebar.selectbox("Choose a dataset type:", ("Classification", "Circles", "Blobs", "Moons"))
|
| 19 |
+
|
| 20 |
+
if data == "Classification":
|
| 21 |
+
X, y = make_classification(n_samples=100, n_features=2, n_redundant=0, random_state=42)
|
| 22 |
+
elif data == "Circles":
|
| 23 |
+
X, y = make_circles(n_samples=100, factor=0.5, noise=0.05)
|
| 24 |
+
elif data == "Blobs":
|
| 25 |
+
X, y = make_blobs(n_samples=250, centers=2, n_features=2, cluster_std=1.0, random_state=42)
|
| 26 |
+
elif data == "Moons":
|
| 27 |
+
X, y = make_moons(n_samples=250, noise=0.1, random_state=42)
|
| 28 |
+
|
| 29 |
+
# π Split dataset
|
| 30 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
|
| 31 |
+
|
| 32 |
+
# π Function to plot decision boundary
|
| 33 |
+
def plot_decision_surface(X, y, model, title):
|
| 34 |
+
plt.figure(figsize=(6, 4))
|
| 35 |
+
plot_decision_regions(X, y, clf=model, colors="red,blue,green")
|
| 36 |
+
plt.title(title, fontsize=12, color="purple")
|
| 37 |
+
plt.xlabel("Feature 1", fontsize=10, color="blue")
|
| 38 |
+
plt.ylabel("Feature 2", fontsize=10, color="blue")
|
| 39 |
+
st.pyplot(plt.gcf(), clear_figure=True)
|
| 40 |
+
|
| 41 |
+
# π§ KNN Classifier Parameters
|
| 42 |
+
st.sidebar.header("βοΈ KNN Parameters")
|
| 43 |
+
n_neighbors = st.sidebar.slider("π’ Number of Neighbors (k)", 1, 15, 3)
|
| 44 |
+
weights = st.sidebar.radio("βοΈ Weight Function", ("uniform", "distance"))
|
| 45 |
+
algorithm = st.sidebar.selectbox("π Algorithm", ("auto", "ball_tree", "kd_tree", "brute"))
|
| 46 |
+
|
| 47 |
+
# π― Initialize and Train Model
|
| 48 |
+
model = KNeighborsClassifier(n_neighbors=n_neighbors, weights=weights, algorithm=algorithm)
|
| 49 |
+
model.fit(X_train, y_train)
|
| 50 |
+
|
| 51 |
+
# π Make Predictions
|
| 52 |
+
y_pred = model.predict(X_test)
|
| 53 |
+
|
| 54 |
+
# π Compute Accuracy & F1-score
|
| 55 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 56 |
+
f1 = f1_score(y_test, y_pred)
|
| 57 |
+
|
| 58 |
+
# π― Model Performance Metrics
|
| 59 |
+
st.subheader("π Model Performance")
|
| 60 |
+
st.markdown(f"β
**Accuracy:** `{accuracy:.2f}` π―")
|
| 61 |
+
st.markdown(f"π **F1-score:** `{f1:.2f}` π₯")
|
| 62 |
+
|
| 63 |
+
# π¨ Plot Decision Boundary
|
| 64 |
+
st.subheader("πΌοΈ Decision Boundary")
|
| 65 |
+
plot_decision_surface(X, y, model, "π KNN Decision Surface")
|
| 66 |
+
|
| 67 |
+
# π Plot Learning Curve
|
| 68 |
+
def plot_learning_curve(model, X, y):
|
| 69 |
+
train_sizes, train_scores, test_scores = learning_curve(
|
| 70 |
+
model, X, y, cv=5, scoring="accuracy", train_sizes=np.linspace(0.1, 1.0, 10)
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
train_mean = np.mean(train_scores, axis=1)
|
| 74 |
+
test_mean = np.mean(test_scores, axis=1)
|
| 75 |
+
|
| 76 |
+
plt.figure(figsize=(6, 4))
|
| 77 |
+
plt.plot(train_sizes, train_mean, "o-", label="ποΈ Training Accuracy", color="green")
|
| 78 |
+
plt.plot(train_sizes, test_mean, "o-", label="π§ͺ Validation Accuracy", color="red")
|
| 79 |
+
|
| 80 |
+
plt.xlabel("Training Samples", fontsize=10, color="blue")
|
| 81 |
+
plt.ylabel("Accuracy", fontsize=10, color="blue")
|
| 82 |
+
plt.title("π Learning Curve: KNN", fontsize=12, color="purple")
|
| 83 |
+
plt.legend()
|
| 84 |
+
st.pyplot(plt.gcf(), clear_figure=True)
|
| 85 |
+
|
| 86 |
+
# π Display Learning Curve
|
| 87 |
+
st.subheader("π Learning Curve")
|
| 88 |
+
plot_learning_curve(model, X, y)
|
innomatics_logo.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
scikit-learn
|
| 3 |
+
numpy
|
| 4 |
+
matplotlib
|
| 5 |
+
mlxtend
|