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)