Skip to content

Commit 392835e

Browse files
committed
add new diagram k-nearest-neighbors.typ and assets
1 parent 6865c81 commit 392835e

7 files changed

+483
-66
lines changed
Loading
Binary file not shown.
Loading

assets/k-nearest-neighbors/k-nearest-neighbors.svg

+278
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#import "@preview/cetz:0.3.4": canvas, draw
2+
#import draw: line, content, circle, polygon
3+
4+
#set page(width: auto, height: auto, margin: 8pt)
5+
#set text(weight: "bold")
6+
7+
8+
// Draw a star directly with lines
9+
#let draw-star(pos, size: 0.3, fill: red) = {
10+
let outer-r = size
11+
let inner-r = size * 0.4
12+
13+
let points = ()
14+
for idx in range(10) {
15+
let angle = idx * 36deg
16+
let radius = if calc.rem(idx, 2) == 0 { outer-r } else { inner-r }
17+
points.push((
18+
pos.at(0) + radius * calc.cos(angle),
19+
pos.at(1) + radius * calc.sin(angle),
20+
))
21+
}
22+
23+
// Draw connected lines to form a star
24+
for idx in range(10) {
25+
let next = if idx == 9 { 0 } else { idx + 1 }
26+
line(
27+
points.at(idx),
28+
points.at(next),
29+
stroke: 1.5pt,
30+
fill: fill,
31+
)
32+
}
33+
34+
if fill != none {
35+
line(..points, close: true, stroke: none, fill: fill)
36+
}
37+
}
38+
39+
// Draw a triangle using polygon function
40+
#let draw-triangle(pos, size: 0.275, fill: green) = {
41+
polygon(
42+
pos,
43+
3,
44+
radius: size,
45+
angle: 90deg, // Point up
46+
fill: fill,
47+
stroke: .5pt,
48+
)
49+
}
50+
51+
#canvas({
52+
// Set up coordinate system
53+
let arrow-style = (mark: (end: "stealth", fill: black, scale: 0.7))
54+
let axis-length = 6
55+
56+
// Draw axes
57+
line(
58+
(0, 0),
59+
(axis-length, 0),
60+
..arrow-style,
61+
name: "x-axis",
62+
)
63+
content((rel: (-0.1, 0.2), to: "x-axis.end"), [$x$ axis], anchor: "south-east")
64+
65+
line((0, 0), (0, axis-length), ..arrow-style, name: "y-axis")
66+
content((rel: (0.2, -0.1), to: "y-axis.end"), [$y$ axis], anchor: "north-west")
67+
68+
// Draw Class A (red stars)
69+
draw-star((1.6, 4.0))
70+
draw-star((1.3, 3.5))
71+
draw-star((2.3, 3.8))
72+
draw-star((1.8, 3.0))
73+
draw-star((1.6, 2.7))
74+
draw-star((2.5, 2.5))
75+
draw-star((2, 2.2))
76+
77+
// Draw Class B (green triangles)
78+
draw-triangle((4.2, 3.5))
79+
draw-triangle((3.6, 2.8))
80+
draw-triangle((3.4, 2.2))
81+
draw-triangle((4.0, 2.2))
82+
draw-triangle((5.2, 2.5))
83+
draw-triangle((4.7, 3.5))
84+
draw-triangle((4, 1.5))
85+
draw-triangle((4.7, 1.8))
86+
87+
// Draw the new example to classify (yellow square with question mark)
88+
content(
89+
(3, 2.5),
90+
(rel: (0.4, 0.4)),
91+
align(center, text(baseline: 1.5pt)[?]),
92+
anchor: "center",
93+
frame: "rect",
94+
fill: yellow,
95+
padding: 1pt,
96+
name: "new-example",
97+
)
98+
99+
// Draw the k=3 and k=7 circles centered at the new example
100+
circle(
101+
"new-example.center",
102+
radius: 0.8,
103+
stroke: (dash: "dashed"),
104+
name: "k3-circle",
105+
)
106+
content((rel: (0, -0.3), to: "k3-circle.south"), $k = 3$, anchor: "north")
107+
108+
circle(
109+
"new-example.center",
110+
radius: 2.0,
111+
stroke: (dash: "dashed"),
112+
name: "k7-circle",
113+
)
114+
content((rel: (0, -0.3), to: "k7-circle.south"), $k = 7$, anchor: "north")
115+
116+
// Add class labels in the upper right corner
117+
content((5.4, 4.8), text(fill: red, size: 12pt)[Class A])
118+
content((5.4, 4.3), text(fill: green, size: 12pt)[Class B])
119+
120+
// Add arrow pointing to the new example
121+
content((4, 5.5), [New example\ to classify], name: "new-example-label")
122+
line(
123+
"new-example-label",
124+
"new-example.north",
125+
stroke: 0.6pt,
126+
mark: (end: "stealth", fill: black, offset: 0.05),
127+
)
128+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: k-Nearest Neighbors
2+
tags:
3+
- machine learning
4+
- classification
5+
- data science
6+
- algorithms
7+
- KNN
8+
description: This image illustrates the K-Nearest Neighbors classification algorithm, showing how different values of $k$ affect classification results. Red stars represent Class A data points, green triangles represent Class B data points, and the yellow square with a question mark is a new instance to be classified. The dashed circles show the $k=3$ and $k=7$ neighborhoods around the instance.
9+
source: https://kdnuggets.com/2020/11/most-popular-distance-metrics-knn.html

0 commit comments

Comments
 (0)