KNN with SQL


Here's a quick implementation of supervised learning, the k-nearest neighbor (KNN) classification algorithm.

To get you up to speed, the premise of KNN is that neighboring data points are similar. Thus, we infer the characteristics of an unlabeled point based on its neighbors.

πŸ‘‡ In the code below, I classify a flower based on its sepal length and width.

Here's how I imagine it working.
1️⃣ First, we plot all the flowers with known sepal length, width, and classification (setosa/virginica) onto a graph.
2️⃣ Then, we plot flowers with unknown classifications onto the same graph.
3️⃣ For a flower with an unknown classification, we classify it as setosa if most of its nearest 50 neighbors are setosa and not virginica.

Here's some food for thought: You can do machine learning with SQL, but should you? Comment your thoughts below.

Code credit goes to FreeCodeCamp.

More from Travis Tang
Use ARRAY_AGG to flatten columns to lists.

Use ARRAY_AGG to flatten columns to lists.

You need to convert a long table of values into a list? This is the most convenient function. πŸ“Œ Syntax: πš‚π™΄π™»π™΄π™²πšƒ π™°πšπšπ™°πšˆ_𝙰𝙢𝙢(πšŒπš˜πš•πšžπš–πš—) π™΅πšπ™Ύπ™Ό πšπšŠπš‹πš•πšŽ There
Pivot Table (From Long to Wide)

Pivot Table (From Long to Wide)

Every data scientist, scientist, and engineer should know how to create a pivot table. π™²π™°πš‚π™΄ πš†π™·π™΄π™½ is the best way to do
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Travis Tang.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.