You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: projects/build-a-k-nearest-neighbor-model-with-p5js/build-a-k-nearest-neighbor-model-with-p5js.mdx
+23-22Lines changed: 23 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,7 +77,7 @@ Let's begin by creating a `class` to represent our points. Each point will have:
77
77
- A random class (either red or green).
78
78
- A function to draw itself on the canvas:
79
79
80
-
```js
80
+
```javascript
81
81
classPoint {
82
82
constructor() {
83
83
this.x=Math.random() * width; // A random value along the x axis
@@ -105,7 +105,7 @@ class Point {
105
105
106
106
If you'd like to test creating and displaying a point, you can do so in the `setup()` function:
107
107
108
-
```js
108
+
```javascript
109
109
functionsetup() {
110
110
createCanvas(300, 300);
111
111
test_point =newPoint();
@@ -119,21 +119,21 @@ Feel free to run your code a few times. Each time you run your code, your point
119
119
120
120
Rather than just creating and displaying a single point, let's create a whole set of points. At the very top of your code, outside of any functions, create an empty array named `points`.
121
121
122
-
```js
122
+
```javascript
123
123
var points = [];
124
124
```
125
125
126
126
Then, inside of `setup()` use a `for` loop to create 20 `Points` (feel free to delete your code that created `test_point` from the last step):
127
127
128
-
```js
128
+
```javascript
129
129
for (var i =0; i <20; i++) {
130
130
points.push(newPoint());
131
131
}
132
132
```
133
133
134
134
Finally, in the `draw()` function, loop through your list and call `.display()` on each point.
135
135
136
-
```js
136
+
```javascript
137
137
functiondraw() {
138
138
background(255);
139
139
for (var i =0; i <points.length; i++) {
@@ -144,20 +144,22 @@ function draw() {
144
144
145
145
### Step 3: Classify Your Mouse
146
146
147
-
Now's the fun part: classification! We'll write a function that classifies our mouse pointer as either red or green based on the 3 closest neighbors. Here are the steps to do this:
147
+
Now's the fun part: classification! We'll write a function that classifies our mouse pointer as either red or green based on the 3 closest neighbors.
148
148
149
-
Measure the distance from the mouse to every `Point`. Store these values in an array.
150
-
Sort the distances to find the 3 nearest neighbors
151
-
Count how many neighbors are red and green, and classify the mouse pointer based on those numbers.
152
-
Draw a small red or green circle at the location of the mouse.
149
+
Here are the steps to do this:
150
+
151
+
1. Measure the distance from the mouse to every `Point`. Store these values in an array.
152
+
2. Sort the distances to find the 3 nearest neighbors
153
+
3. Count how many neighbors are red and green, and classify the mouse pointer based on those numbers.
154
+
4. Draw a small red or green circle at the location of the mouse.
153
155
154
156
Let's get started!
155
157
156
158
### Step 3.1: Measure Distances:
157
159
158
160
We need to find the distance between our mouse pointer and each `Point` in our dataset. We'll put each distance in an array. Let's begin by creating an empty array and setting up our `for` loop:
159
161
160
-
```js
162
+
```javascript
161
163
functionclassifyMouse() {
162
164
163
165
let distances = [];
@@ -169,13 +171,13 @@ function classifyMouse() {
169
171
170
172
Next, we can use the `dist()` function to find the distance between our mouse and each point. Use this code inside your `for` loop:
171
173
172
-
```js
174
+
```javascript
173
175
let distance =dist(points[i].x, points[i].y, mouseX, mouseY);
174
176
```
175
177
176
178
Finally, we don't want to simply add the distance to our list. We need to add the distance **AND** the `class` of the `Point` associated with that distance. Inside your food loop, write the following line of code:
177
179
178
-
```js
180
+
```javascript
179
181
distances.push([distance, points[i].class]);
180
182
```
181
183
@@ -187,15 +189,15 @@ This sort function is slightly complicated, because `distances` isn't simply a l
187
189
188
190
The following line of code will sort your list correctly. This should be put outside the `for` loop, but inside your `classifyMouse()` function:
189
191
190
-
```js
192
+
```javascript
191
193
distances.sort((a, b) => a[0] - b[0]);
192
194
```
193
195
194
196
### Step 3.3: Count the Class of the 3 Nearest Neighbors
195
197
196
198
Let's now loop through the first 3 items in the list and count the number of times class `1` and class `0` show up:
197
199
198
-
```js
200
+
```javascript
199
201
let numZero =0;
200
202
let numOne =0;
201
203
@@ -213,7 +215,7 @@ for (var i = 0; i < 3; i++) {
213
215
214
216
We now know whether the mouse should be classified as red or green. Let's draw a small circle at the location of the mouse with the correct color. This should be the end of your `classifyMouse()` function, outside of all loops:
Now we just need to call it! At the bottom of the `draw()` function, call `classifyMouse()`. Feel free to move your mouse around and see if the classification is correct! Is your mouse pointer red when the majority of the 3 nearest neighbors are also red?
227
229
228
-
###Extensions:
230
+
## Extensions:
229
231
230
232
You made a working KNN visualizer! Want to level it up? Try these challenges:
231
233
232
-
We've hard-coded `20` points in our dataset and `3` nearest neighbors. Refactor your code so it is easy to quickly change those values to any numbers.
233
-
It can be a bit tough to eyeball which points are the nearest neighbors to your mouse. Edit your code so the nearest neighbors are now highlighted (maybe with a yellow border rather than black)
234
-
Add your own style! Rather than red and green points, you could draw your points as wizard 🧙♂️ or warrior ⚔️ emojis (calling back to our original example). Or come up with your own theme! Could you do something interesting with the background?
235
-
KNN models can work with multi-class problems. Rather than simply red and green points, what if you had red, green, and blue points? Refactor your code so your visualization works with _any_ number of classes.
234
+
-We've hard-coded `20` points in our dataset and `3` nearest neighbors. Refactor your code so it is easy to quickly change those values to any numbers.
235
+
-It can be a bit tough to eyeball which points are the nearest neighbors to your mouse. Edit your code so the nearest neighbors are now highlighted (maybe with a yellow border rather than black)
236
+
-Add your own style! Rather than red and green points, you could draw your points as wizard 🧙♂️ or warrior ⚔️ emojis (calling back to our original example). Or come up with your own theme! Could you do something interesting with the background?
237
+
-KNN models can work with multi-class problems. Rather than simply red and green points, what if you had red, green, and blue points? Refactor your code so your visualization works with _any_ number of classes.
236
238
237
239
Congrats on reaching the end of this project! You now hopefully have a better understanding of how a K-Nearest Neighbor model works under the hood!
238
240
@@ -241,4 +243,3 @@ Congrats on reaching the end of this project! You now hopefully have a better un
241
243
-[p5.js](https://p5js.org)
242
244
-[More Machine Learning on Codédex](https://www.codedex.io/machine-learning)
0 commit comments