Pasham123 commited on
Commit
52039bd
·
verified ·
1 Parent(s): 39d0b8d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -1
app.py CHANGED
@@ -4,4 +4,125 @@ st.set_page_config(page_title="KNN", page_icon="⭐", layout="wide")
4
 
5
  # Title
6
  st.markdown("<h1 style='color: #003366;'>Understanding K-Nearest Neighbors (KNN)</h1>", unsafe_allow_html=True)
7
- st.image("https://cdn-uploads.huggingface.co/production/uploads/673f6e448c5214b832b3724c/pcgFJYCffuXy7ilH-Kr53.png")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  # Title
6
  st.markdown("<h1 style='color: #003366;'>Understanding K-Nearest Neighbors (KNN)</h1>", unsafe_allow_html=True)
7
+ st.image("https://cdn-uploads.huggingface.co/production/uploads/673f6e448c5214b832b3724c/pcgFJYCffuXy7ilH-Kr53.png")
8
+
9
+ st.write("""
10
+ K-Nearest Neighbors (KNN) is a basic yet powerful machine learning algorithm used for both **classification** and **regression** tasks. It makes predictions by looking at the 'K' closest data points in the training set.
11
+ ### Key Characteristics:
12
+ - KNN is a **non-parametric** and **instance-based** algorithm.
13
+ - It **stores** the training data instead of learning a function from it.
14
+ - Predictions are made based on **similarity** (distance metrics like Euclidean).
15
+ """)
16
+
17
+ # Working of KNN
18
+ st.markdown("<h2 style='color: #003366;'>How KNN Works</h2>", unsafe_allow_html=True)
19
+
20
+ st.subheader("Training Phase")
21
+ st.write("""
22
+ - There is **no actual training** involved.
23
+ - The algorithm simply memorizes the training data.
24
+ """)
25
+
26
+ st.subheader("Prediction Phase (Classification)")
27
+ st.write("""
28
+ 1. Select a value for **K** (number of neighbors).
29
+ 2. Measure distances between the new point and training samples.
30
+ 3. Identify the **K nearest points**.
31
+ 4. Assign the class based on **majority vote**.
32
+ """)
33
+
34
+ st.subheader("Prediction Phase (Regression)")
35
+ st.write("""
36
+ 1. Choose a value for **K**.
37
+ 2. Compute distances to training data.
38
+ 3. Select the **K closest neighbors**.
39
+ 4. Predict the output as the **average** (or weighted average) of these neighbors' values.
40
+ """)
41
+
42
+ # Overfitting vs Underfitting
43
+ st.markdown("<h2 style='color: #003366;'>Model Fit: Overfitting vs Underfitting</h2>", unsafe_allow_html=True)
44
+ st.write("""
45
+ - **Overfitting**: Happens when K is too low (e.g., K=1); the model becomes too sensitive to noise.
46
+ - **Underfitting**: Happens when K is too high; model may miss important patterns.
47
+ - **Optimal Fit**: Requires selecting a K value that provides a good balance between bias and variance.
48
+ """)
49
+
50
+ # Training vs CV Error
51
+ st.markdown("<h2 style='color: #003366;'>Training vs Cross-Validation Error</h2>", unsafe_allow_html=True)
52
+ st.write("""
53
+ To choose the best `K`, monitor both:
54
+ - **Training Error**: Error on the training set.
55
+ - **Cross-Validation (CV) Error**: Error on a validation set, helps assess generalization.
56
+ High training accuracy but poor CV accuracy = overfitting.
57
+ Low training and CV accuracy = underfitting.
58
+ """)
59
+
60
+ # Hyperparameter tuning
61
+ st.markdown("<h2 style='color: #003366;'>KNN Hyperparameters</h2>", unsafe_allow_html=True)
62
+ st.write("""
63
+ Main parameters to tune:
64
+ - `n_neighbors` (K value)
65
+ - `weights`: 'uniform' or 'distance'
66
+ - `metric`: Distance metric, e.g., 'euclidean', 'manhattan'
67
+ - `n_jobs`: Use multiple processors for speed
68
+ These can be optimized using Grid Search, Random Search, or Bayesian methods.
69
+ """)
70
+
71
+ # Feature Scaling
72
+ st.markdown("<h2 style='color: #003366;'>Why Feature Scaling is Crucial</h2>", unsafe_allow_html=True)
73
+ st.write("""
74
+ KNN relies on distance between points, so features must be on the same scale.
75
+ Options:
76
+ - **Normalization** (MinMax Scaling): Range [0, 1]
77
+ - **Standardization** (Z-score): Mean 0, Std 1
78
+ **Important**: Apply scaling *after* splitting your data to avoid data leakage.
79
+ """)
80
+
81
+ # Weighted KNN
82
+ st.markdown("<h2 style='color: #003366;'>Weighted KNN</h2>", unsafe_allow_html=True)
83
+ st.write("""
84
+ In Weighted KNN, closer neighbors contribute more to the prediction.
85
+ This is especially useful when nearby data points are more reliable than distant ones.
86
+ """)
87
+
88
+ # Decision regions
89
+ st.markdown("<h2 style='color: #003366;'>Decision Boundaries</h2>", unsafe_allow_html=True)
90
+ st.write("""
91
+ - K=1 produces sharp, complex boundaries → risk of overfitting.
92
+ - Larger K smoothens the boundary → reduces variance but increases bias.
93
+ """)
94
+
95
+ # Cross-validation
96
+ st.markdown("<h2 style='color: #003366;'>Understanding Cross-Validation</h2>", unsafe_allow_html=True)
97
+ st.write("""
98
+ Cross-validation helps evaluate how well the model generalizes.
99
+ **K-Fold Cross Validation**:
100
+ - Split data into K parts.
101
+ - Train on K-1 parts, test on the remaining.
102
+ - Repeat K times and average the performance.
103
+ """)
104
+
105
+ # Hyperparameter search methods
106
+ st.markdown("<h2 style='color: #003366;'>Hyperparameter Tuning Methods</h2>", unsafe_allow_html=True)
107
+ st.write("""
108
+ - **Grid Search**: Tests all combinations — reliable but slow.
109
+ - **Random Search**: Randomly samples combinations — faster, may miss optimal.
110
+ - **Bayesian Optimization**: Uses past performance to choose next candidates — efficient and smart.
111
+ """)
112
+
113
+ # Link to implementation
114
+ st.markdown("<h2 style='color: #003366;'>KNN Code Implementation</h2>", unsafe_allow_html=True)
115
+ st.markdown(
116
+ "<a href='https://colab.research.google.com/drive/12lD7ceLj5BPiB6tgxaWXciB1IOMYGyZg#scrollTo=96210031-7967-41c4-9de2-56135c423404' target='_blank' style='font-size: 16px; color: #003366;'>Click here to view the notebook</a>",
117
+ unsafe_allow_html=True
118
+ )
119
+
120
+ # Summary
121
+ st.write("""
122
+ KNN is a straightforward but effective algorithm.
123
+ To get the best results:
124
+ - Scale your data properly.
125
+ - Use cross-validation.
126
+ - Carefully choose hyperparameters using tuning methods.
127
+ """)
128
+