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
+12-8Lines changed: 12 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
---
2
-
title: Build a K-Nearest Neighbor Model with p5.js
2
+
title: Build a K-Nearest Neighbors Model with p5.js
3
3
author: Alex Kuntz
4
4
uid: ZjTU0yoJGgNolU9xIJGOR3bxYKm2
5
5
datePublished: 2025-09-08
6
-
description: Learn how to build a K-Nearest Neighbor (KNN) model from scratch using p5.js
6
+
description: Learn how to build a K-Nearest Neighbors (KNN) model from scratch using p5.js
7
7
header: https://i.imgur.com/RmZDc8j.gif
8
8
bannerImage: https://i.imgur.com/RmZDc8j.gif
9
9
published: live
@@ -101,12 +101,12 @@ Let's begin by creating a `class` to represent our points. Each point will have:
101
101
```js
102
102
classPoint {
103
103
constructor() {
104
-
this.x=Math.random() * width; // A random value along the x axis
105
-
this.y=Math.random() * height; // A random value along the y axis
106
-
this.class=floor(Math.random() *2)// A random class: either 0 or 1
104
+
this.x=Math.random() * width; // A random value along the x axis
105
+
this.y=Math.random() * height; // A random value along the y axis
106
+
this.class=floor(Math.random() *2);// A random class: either 0 or 1
107
107
}
108
108
109
-
display() {
109
+
display() {
110
110
stroke(0);
111
111
strokeWeight(2);
112
112
@@ -185,7 +185,7 @@ function classifyMouse() {
185
185
let distances = [];
186
186
187
187
for (var i =0; i <points.length; i++) {
188
-
// TODO: Find the distance between each point and the mouse.
188
+
// TODO... Find the distance between each point and the mouse.
189
189
}
190
190
}
191
191
```
@@ -254,7 +254,7 @@ Now we just need to call it! At the bottom of the `draw()` function, call `class
254
254
255
255
Congrats on reaching the end of this project! You made a working KNN visualizer!
256
256
257
-
You now hopefully have a better understanding of how a K-Nearest Neighbor model works under the hood!
257
+
You now hopefully have a better understanding of how a K-Nearest Neighbor model works under the hood.
258
258
259
259
### Bonus Challenges
260
260
@@ -265,6 +265,10 @@ Want to level it up? Try these bonus challenges:
265
265
- 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?
266
266
- 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.
267
267
268
+
### Video Walkthrough
269
+
270
+
<iframewidth="560"height="315"src="https://www.youtube.com/embed/uq0kvzBX6uU?si=r4GbxX5BBG953-Ad"title="YouTube video player"frameborder="0"allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"referrerpolicy="strict-origin-when-cross-origin"allowfullscreen></iframe>
0 commit comments