Spaces:
Sleeping
Sleeping
Update pages/9_KNN.py
Browse files- pages/9_KNN.py +91 -82
pages/9_KNN.py
CHANGED
|
@@ -1,123 +1,132 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
-
st.set_page_config(page_title="KNN
|
| 4 |
|
| 5 |
st.markdown("""
|
| 6 |
<style>
|
| 7 |
.stApp {
|
| 8 |
-
background
|
| 9 |
-
color:
|
|
|
|
| 10 |
}
|
| 11 |
-
h1, h2, h3
|
| 12 |
-
color: #
|
| 13 |
}
|
| 14 |
.sidebar .sidebar-content {
|
| 15 |
-
background-color: #
|
| 16 |
}
|
| 17 |
.block-container {
|
| 18 |
padding-top: 2rem;
|
| 19 |
padding-bottom: 2rem;
|
| 20 |
}
|
| 21 |
a {
|
| 22 |
-
color: #
|
| 23 |
text-decoration: none;
|
| 24 |
}
|
| 25 |
a:hover {
|
| 26 |
-
color: #
|
| 27 |
}
|
| 28 |
</style>
|
| 29 |
""", unsafe_allow_html=True)
|
| 30 |
|
| 31 |
-
st.sidebar.title("
|
| 32 |
-
st.sidebar.markdown("
|
| 33 |
-
|
| 34 |
-
st.markdown(""
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
""")
|
| 47 |
|
| 48 |
-
|
| 49 |
-
st.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
4. Predict class (majority vote) or value (average)
|
| 58 |
""")
|
| 59 |
-
|
| 60 |
-
with st.expander("๐ฏ Underfitting vs Overfitting"):
|
| 61 |
-
st.write("""
|
| 62 |
-
- **Overfitting**: Very low training error but poor generalization. Happens with low `K` (e.g., `k=1`).
|
| 63 |
-
- **Underfitting**: Model is too simple to learn any patterns (e.g., very high `K`).
|
| 64 |
-
- **Sweet Spot**: Use **cross-validation** to pick the best `K` that balances both.
|
| 65 |
-
""")
|
| 66 |
-
|
| 67 |
-
with st.expander("๐ Training vs Cross-Validation Error"):
|
| 68 |
-
st.write("""
|
| 69 |
-
- **Training Error**: How well the model does on the data it has seen.
|
| 70 |
-
- **CV Error**: Performance on unseen data using validation.
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
""")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
st.
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
๐ Use **Grid Search**, **Randomized Search**, or **Bayesian Optimization** to tune these.
|
| 82 |
""")
|
| 83 |
|
| 84 |
-
|
| 85 |
-
st.
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
""")
|
| 91 |
|
| 92 |
-
|
| 93 |
-
st.
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
""")
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
st.
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
|
|
|
| 105 |
""")
|
| 106 |
|
| 107 |
-
|
| 108 |
-
st.
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
""")
|
| 115 |
|
| 116 |
st.markdown("""
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
</a>
|
| 121 |
""", unsafe_allow_html=True)
|
| 122 |
|
| 123 |
-
st.success("
|
|
|
|
| 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! ๐")
|