Spaces:
Sleeping
Sleeping
File size: 3,576 Bytes
b016c22 |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification, make_circles, make_blobs, make_moons
from sklearn.model_selection import train_test_split, learning_curve
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, f1_score
from mlxtend.plotting import plot_decision_regions
# π Display image (Logo)
st.image("innomatics_logo.png", width=600)
# π Streamlit App Title
st.title("π KNN Classifier: Decision Boundaries & Learning Curve")
# π Select dataset
st.sidebar.header("π Select Dataset")
data = st.sidebar.selectbox("Choose a dataset type:", ("Classification", "Circles", "Blobs", "Moons"))
if data == "Classification":
X, y = make_classification(n_samples=100, n_features=2, n_redundant=0, random_state=42)
elif data == "Circles":
X, y = make_circles(n_samples=100, factor=0.5, noise=0.05)
elif data == "Blobs":
X, y = make_blobs(n_samples=250, centers=2, n_features=2, cluster_std=1.0, random_state=42)
elif data == "Moons":
X, y = make_moons(n_samples=250, noise=0.1, random_state=42)
# π Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# π Function to plot decision boundary
def plot_decision_surface(X, y, model, title):
plt.figure(figsize=(6, 4))
plot_decision_regions(X, y, clf=model, colors="red,blue,green")
plt.title(title, fontsize=12, color="purple")
plt.xlabel("Feature 1", fontsize=10, color="blue")
plt.ylabel("Feature 2", fontsize=10, color="blue")
st.pyplot(plt.gcf(), clear_figure=True)
# π§ KNN Classifier Parameters
st.sidebar.header("βοΈ KNN Parameters")
n_neighbors = st.sidebar.slider("π’ Number of Neighbors (k)", 1, 15, 3)
weights = st.sidebar.radio("βοΈ Weight Function", ("uniform", "distance"))
algorithm = st.sidebar.selectbox("π Algorithm", ("auto", "ball_tree", "kd_tree", "brute"))
# π― Initialize and Train Model
model = KNeighborsClassifier(n_neighbors=n_neighbors, weights=weights, algorithm=algorithm)
model.fit(X_train, y_train)
# π Make Predictions
y_pred = model.predict(X_test)
# π Compute Accuracy & F1-score
accuracy = accuracy_score(y_test, y_pred)
f1 = f1_score(y_test, y_pred)
# π― Model Performance Metrics
st.subheader("π Model Performance")
st.markdown(f"β
**Accuracy:** `{accuracy:.2f}` π―")
st.markdown(f"π **F1-score:** `{f1:.2f}` π₯")
# π¨ Plot Decision Boundary
st.subheader("πΌοΈ Decision Boundary")
plot_decision_surface(X, y, model, "π KNN Decision Surface")
# π Plot Learning Curve
def plot_learning_curve(model, X, y):
train_sizes, train_scores, test_scores = learning_curve(
model, X, y, cv=5, scoring="accuracy", train_sizes=np.linspace(0.1, 1.0, 10)
)
train_mean = np.mean(train_scores, axis=1)
test_mean = np.mean(test_scores, axis=1)
plt.figure(figsize=(6, 4))
plt.plot(train_sizes, train_mean, "o-", label="ποΈ Training Accuracy", color="green")
plt.plot(train_sizes, test_mean, "o-", label="π§ͺ Validation Accuracy", color="red")
plt.xlabel("Training Samples", fontsize=10, color="blue")
plt.ylabel("Accuracy", fontsize=10, color="blue")
plt.title("π Learning Curve: KNN", fontsize=12, color="purple")
plt.legend()
st.pyplot(plt.gcf(), clear_figure=True)
# π Display Learning Curve
st.subheader("π Learning Curve")
plot_learning_curve(model, X, y)
|