Harika22 commited on
Commit
fcf7f83
ยท
verified ยท
1 Parent(s): 80e18d9

Update pages/9_KNN.py

Browse files
Files changed (1) hide show
  1. pages/9_KNN.py +79 -102
pages/9_KNN.py CHANGED
@@ -1,132 +1,109 @@
1
  import streamlit as st
2
 
3
- st.set_page_config(page_title="KNN Visual Guide", page_icon="๐Ÿ“Š", layout="wide")
4
 
5
- st.markdown("""
6
- <style>
7
- .stApp {
8
- background: linear-gradient(to right, #141E30, #243B55);
9
- color: white;
10
- font-family: 'Segoe UI', sans-serif;
11
- }
12
- h1, h2, h3 {
13
- color: #00CED1;
14
- }
15
- .sidebar .sidebar-content {
16
- background-color: #1e1e1e;
17
- }
18
- .block-container {
19
- padding-top: 2rem;
20
- padding-bottom: 2rem;
21
- }
22
- a {
23
- color: #00BFFF;
24
- text-decoration: none;
25
- }
26
- a:hover {
27
- color: #1E90FF;
28
- }
29
- </style>
30
- """, unsafe_allow_html=True)
31
 
32
- st.sidebar.title("๐Ÿ“Š KNN Visual Guide")
33
- st.sidebar.markdown("Dive into KNN concepts interactively!")
34
 
35
- st.markdown("<h1 style='text-align: center;'>๐Ÿงญ K-Nearest Neighbors (KNN) Explorer</h1>", unsafe_allow_html=True)
36
-
37
- section = st.radio(
38
- "Choose a KNN Concept to Explore:",
39
- [
40
- "๐Ÿ“˜ Introduction to KNN",
41
- "โš™๏ธ How KNN Works",
42
  "๐ŸŽฏ Underfitting vs Overfitting",
43
- "๐Ÿ“‰ Cross-Validation",
44
  "๐Ÿ› ๏ธ Hyperparameter Tuning",
45
  "โš–๏ธ Feature Scaling",
46
  "๐Ÿงฎ Weighted KNN",
47
- "๐Ÿ—บ๏ธ Decision Boundaries"
48
- ]
 
49
  )
50
 
51
- if section == "๐Ÿ“˜ Introduction to KNN":
52
- st.subheader("๐Ÿ“˜ What is KNN?")
53
- st.markdown("""
54
- KNN stands for **K-Nearest Neighbors**, a simple and powerful algorithm used for:
55
- - ๐Ÿ“ **Classification**: Predicting a category
56
- - ๐Ÿ”ข **Regression**: Predicting a continuous value
57
- โœ… Lazy Learning โ†’ No training phase, just memorization
58
- โœ… Based on **distance** to nearest neighbors
59
- โœ… Works well with clean and scaled data
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  """)
61
 
62
- elif section == "โš™๏ธ How KNN Works":
63
- st.subheader("โš™๏ธ How Does KNN Work?")
64
- st.markdown("""
65
- ๐Ÿš€ **Step-by-step** process:
66
- 1. Pick a value for `K`
67
- 2. Measure distance (Euclidean, Manhattan, etc.) to all training points
68
- 3. Pick `K` nearest ones
69
- 4. ๐Ÿ“Š Classification โ†’ Majority vote
70
- ๐Ÿ“ˆ Regression โ†’ Average/weighted average
71
  """)
72
 
73
- elif section == "๐ŸŽฏ Underfitting vs Overfitting":
74
- st.subheader("๐ŸŽฏ Underfitting vs Overfitting")
75
- st.markdown("""
76
- ๐Ÿ” **Overfitting**: Model memorizes data โ€” poor on new data
77
- ๐Ÿ” **Underfitting**: Model too simple โ€” misses patterns
78
- โœ… **Best Fit**: Balance both using cross-validation
 
79
  """)
80
 
81
- elif section == "๐Ÿ“‰ Cross-Validation":
82
- st.subheader("๐Ÿ“‰ Training vs Cross-Validation")
83
- st.markdown("""
84
- ๐Ÿงช **Training Error**: Error on known data
85
- ๐Ÿ”„ **Cross-Validation Error**: Error on unseen data
86
- ๐ŸŽฏ Choose K where both errors are low โ†’ best generalization
87
  """)
88
 
89
- elif section == "๐Ÿ› ๏ธ Hyperparameter Tuning":
90
- st.subheader("๐Ÿ› ๏ธ Tuning KNN")
91
- st.markdown("""
92
- ๐Ÿ”ง Main Parameters:
93
- - `k`: Number of neighbors
94
- - `weights`: All equal or weighted by distance
95
- - `metric`: Distance type (e.g., Euclidean)
96
- ๐Ÿง  Use Grid Search, Random Search, or Optuna for optimization
97
  """)
98
 
99
- elif section == "โš–๏ธ Feature Scaling":
100
- st.subheader("โš–๏ธ Why Scale Your Features?")
101
- st.markdown("""
102
- KNN relies on distance โ€” so features must be on the same scale:
103
- - ๐Ÿ”ข **Standardization**: Mean = 0, SD = 1
104
- - ๐Ÿ”ป **Normalization**: Rescales between 0 and 1
105
- โ— Always scale after train-test split to avoid data leakage
106
  """)
107
 
108
- elif section == "๐Ÿงฎ Weighted KNN":
109
- st.subheader("๐Ÿงฎ Weighted KNN")
110
- st.markdown("""
111
- Instead of equal votes, **Weighted KNN** gives:
112
- - Higher weight to nearer neighbors
113
- - Lower influence from distant points
114
- ๐Ÿ“Œ Improves performance when neighbor relevance varies
115
  """)
116
 
117
- elif section == "๐Ÿ—บ๏ธ Decision Boundaries":
118
- st.subheader("๐Ÿ—บ๏ธ Decision Regions")
119
- st.markdown("""
120
- Visuals of how KNN separates classes:
121
- - `k=1` โ†’ Very sensitive, sharp boundaries โ†’ Overfitting
122
- - `k > 1` โ†’ Smoother, more general
123
- ๐Ÿ“Š Helps interpret model behavior in 2D/3D space
124
  """)
125
 
 
126
  st.markdown("""
127
- <hr style='border: 1px solid #555;'>
128
- <h4 style='color: #00CED1;'>๐Ÿ”— Try it Yourself in Colab:</h4>
129
- <a href='https://colab.research.google.com/drive/11wk6wt7sZImXhTqzYrre3ic4oj3KFC4M?usp=sharing' target='_blank'>Open Interactive Notebook</a>
130
  """, unsafe_allow_html=True)
131
 
132
- st.success("Explore, visualize, and understand how KNN works like never before! ๐Ÿš€")
 
1
  import streamlit as st
2
 
3
+ st.set_page_config(page_title="KNN", page_icon="๐Ÿค–", layout="wide")
4
 
5
+ st.markdown("<h1 style='text-align: center; color: #FF4C60;'>๐Ÿ” K-Nearest Neighbors (KNN) Algorithm</h1>", unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ st.sidebar.title("๐Ÿค– KNN App")
8
+ st.sidebar.markdown("Explore KNN concepts step-by-step using the sections below.")
9
 
10
+ option = st.radio(
11
+ "Select a concept to learn:",
12
+ (
13
+ "๐Ÿ“˜ What is KNN?",
14
+ "โš™๏ธ How Does KNN Work?",
 
 
15
  "๐ŸŽฏ Underfitting vs Overfitting",
16
+ "๐Ÿ“‰ Training vs Cross-Validation Error",
17
  "๐Ÿ› ๏ธ Hyperparameter Tuning",
18
  "โš–๏ธ Feature Scaling",
19
  "๐Ÿงฎ Weighted KNN",
20
+ "๐Ÿ—บ๏ธ Decision Regions",
21
+ "๐Ÿ” Cross-Validation Explained"
22
+ )
23
  )
24
 
25
+ if option == "๐Ÿ“˜ What is KNN?":
26
+ st.write("""
27
+ K-Nearest Neighbors (KNN) is a **non-parametric**, **lazy learning** algorithm used for both classification and regression.
28
+
29
+ โœ… It stores all training data instead of learning a function.
30
+ โœ… It uses distance metrics (e.g., Euclidean, Manhattan) to make predictions.
31
+ โœ… Suitable for small to moderately sized datasets.
32
+ """)
33
+
34
+ elif option == "โš™๏ธ How Does KNN Work?":
35
+ st.write("""
36
+ **Training Phase:**
37
+ - No actual training occurs. KNN memorizes the training dataset.
38
+
39
+ **Prediction Phase (Classification):**
40
+ 1. Choose a value of **K**
41
+ 2. Calculate distances from the new point to all others
42
+ 3. Pick **K closest** points
43
+ 4. Use majority vote to classify
44
+
45
+ **Prediction Phase (Regression):**
46
+ - Average the values of the K nearest neighbors.
47
  """)
48
 
49
+ elif option == "๐ŸŽฏ Underfitting vs Overfitting":
50
+ st.write("""
51
+ - **Overfitting**: The model is too specific to the training data. Poor on unseen data.
52
+ - **Underfitting**: The model is too simple. Poor even on training data.
53
+ - **Ideal Model**: A balance that performs well on both seen and unseen data.
 
 
 
 
54
  """)
55
 
56
+ elif option == "๐Ÿ“‰ Training vs Cross-Validation Error":
57
+ st.write("""
58
+ - **Training Error** is the error on the known training data.
59
+ - **Cross-Validation Error** is from unseen validation data.
60
+
61
+ โœ… Use cross-validation to pick the best value of `K`.
62
+ ๐Ÿ” Big gap = Overfitting; Both high = Underfitting.
63
  """)
64
 
65
+ elif option == "๐Ÿ› ๏ธ Hyperparameter Tuning":
66
+ st.write("""
67
+ - **K**: Number of neighbors โ€” test multiple values.
68
+ - **Weights**: Equal (`uniform`) or based on distance (`distance`).
69
+ - **Metric**: How distance is measured (Euclidean, Manhattan).
70
+ - Use **Grid Search**, **Random Search**, or **Optuna** for best tuning.
71
  """)
72
 
73
+ elif option == "โš–๏ธ Feature Scaling":
74
+ st.write("""
75
+ KNN uses distances โ€” so features must be on the same scale.
76
+ - **Normalization** scales data between 0 and 1.
77
+ - **Standardization** centers data around mean 0.
78
+ โš ๏ธ Always scale data after splitting to avoid leakage.
 
 
79
  """)
80
 
81
+ elif option == "๐Ÿงฎ Weighted KNN":
82
+ st.write("""
83
+ Weighted KNN assigns higher importance to closer neighbors.
84
+ - Use `weights='distance'` to apply this logic in libraries like scikit-learn.
85
+ - Helps in noisy datasets or when closer points are more meaningful.
 
 
86
  """)
87
 
88
+ elif option == "๐Ÿ—บ๏ธ Decision Regions":
89
+ st.write("""
90
+ - Small `k` values create complex, wiggly decision boundaries (overfitting).
91
+ - Larger `k` smooths the boundary (better generalization).
92
+ - Visualizing decision regions helps understand the algorithmโ€™s behavior.
 
 
93
  """)
94
 
95
+ elif option == "๐Ÿ” Cross-Validation Explained":
96
+ st.write("""
97
+ - **K-Fold Cross-Validation** splits data into `K` parts.
98
+ - The model trains on K-1 parts and tests on the remaining part.
99
+ - Helps evaluate model stability and avoid overfitting.
 
 
100
  """)
101
 
102
+ st.markdown("<h2 style='color: #58a6ff;'>๐Ÿ““ Try KNN in Colab:</h2>", unsafe_allow_html=True)
103
  st.markdown("""
104
+ <a href='https://colab.research.google.com/drive/11wk6wt7sZImXhTqzYrre3ic4oj3KFC4M?usp=sharing' target='_blank'>
105
+ ๐Ÿ”— Open Jupyter Notebook on Colab
106
+ </a>
107
  """, unsafe_allow_html=True)
108
 
109
+ st.success("KNN is easy to understand and surprisingly powerful! Tune it well, scale your data, and validate your model to get the best results.")