saramneena commited on
Commit
b016c22
Β·
verified Β·
1 Parent(s): 6c8cef7

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +88 -0
  2. innomatics_logo.png +0 -0
  3. 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