Skip to content

Commit 8c06d51

Browse files
authored
Update build-a-k-nearest-neighbor-model-with-p5js.mdx
1 parent cc848d6 commit 8c06d51

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

projects/build-a-k-nearest-neighbor-model-with-p5js/build-a-k-nearest-neighbor-model-with-p5js.mdx

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Let's begin by creating a `class` to represent our points. Each point will have:
7777
- A random class (either red or green).
7878
- A function to draw itself on the canvas:
7979

80-
```js
80+
```javascript
8181
class Point {
8282
constructor() {
8383
this.x = Math.random() * width; // A random value along the x axis
@@ -105,7 +105,7 @@ class Point {
105105

106106
If you'd like to test creating and displaying a point, you can do so in the `setup()` function:
107107

108-
```js
108+
```javascript
109109
function setup() {
110110
createCanvas(300, 300);
111111
test_point = new Point();
@@ -119,21 +119,21 @@ Feel free to run your code a few times. Each time you run your code, your point
119119

120120
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`.
121121

122-
```js
122+
```javascript
123123
var points = [];
124124
```
125125

126126
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):
127127

128-
```js
128+
```javascript
129129
for (var i = 0; i < 20; i++) {
130130
points.push(new Point());
131131
}
132132
```
133133

134134
Finally, in the `draw()` function, loop through your list and call `.display()` on each point.
135135

136-
```js
136+
```javascript
137137
function draw() {
138138
background(255);
139139
for (var i = 0; i < points.length; i++) {
@@ -144,20 +144,22 @@ function draw() {
144144

145145
### Step 3: Classify Your Mouse
146146

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.
148148

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.
153155

154156
Let's get started!
155157

156158
### Step 3.1: Measure Distances:
157159

158160
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:
159161

160-
```js
162+
```javascript
161163
function classifyMouse() {
162164

163165
let distances = [];
@@ -169,13 +171,13 @@ function classifyMouse() {
169171

170172
Next, we can use the `dist()` function to find the distance between our mouse and each point. Use this code inside your `for` loop:
171173

172-
```js
174+
```javascript
173175
let distance = dist(points[i].x, points[i].y, mouseX, mouseY);
174176
```
175177

176178
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:
177179

178-
```js
180+
```javascript
179181
distances.push([distance, points[i].class]);
180182
```
181183

@@ -187,15 +189,15 @@ This sort function is slightly complicated, because `distances` isn't simply a l
187189

188190
The following line of code will sort your list correctly. This should be put outside the `for` loop, but inside your `classifyMouse()` function:
189191

190-
```js
192+
```javascript
191193
distances.sort((a, b) => a[0] - b[0]);
192194
```
193195

194196
### Step 3.3: Count the Class of the 3 Nearest Neighbors
195197

196198
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:
197199

198-
```js
200+
```javascript
199201
let numZero = 0;
200202
let numOne = 0;
201203

@@ -213,7 +215,7 @@ for (var i = 0; i < 3; i++) {
213215

214216
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:
215217

216-
```js
218+
```javascript
217219
noStroke();
218220
if (numOne > numZero) {
219221
fill(255, 0, 0);
@@ -225,14 +227,14 @@ ellipse(mouseX, mouseY, 10, 10);
225227

226228
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?
227229

228-
### Extensions:
230+
## Extensions:
229231

230232
You made a working KNN visualizer! Want to level it up? Try these challenges:
231233

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.
236238

237239
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!
238240

@@ -241,4 +243,3 @@ Congrats on reaching the end of this project! You now hopefully have a better un
241243
- [p5.js](https://p5js.org)
242244
- [More Machine Learning on Codédex](https://www.codedex.io/machine-learning)
243245
- [The KNN Algorithm](https://en.wikipedia.org/wiki/K-nearest_neighbors_algorithm)
244-

0 commit comments

Comments
 (0)