Machine Learning & AI · Chapter 4.4 · Intermediate level
K-Nearest Neighbors / KNN ("Jaise Dost, Waisa Aap!")
KNN एल्गोरिदम ("जैसे 3 सबसे करीबी पड़ोसी, वैसे आप!")
There's a famous saying: 'Tell me who your 3 closest friends are, and I'll tell you who you are!' KNN (K-Nearest Neighbors) works the exact same way! When a mystery cricket player (⭐) arrives with [60 Sixes, 1 Wicket], KNN measures straight-line distances on a 2D map to find the k=3 closest players — if those 3 neighbours are 🏏 Batsmen, the mystery player wins by majority vote as a Batsman!
एक मशहूर कहावत है: 'आपके 3 सबसे करीबी दोस्त जैसे होंगे, आप भी वैसे ही होंगे!' KNN (K-Nearest Neighbors) बिल्कुल यही करता है! जब कोई नया खिलाड़ी (⭐) आता है जिसने 60 छक्के और 1 विकेट लिया है, तो AI ग्राफ़ पर देखता है कि उसके सबसे पास वाले k=3 पड़ोसी कौन हैं — अगर तीनों पास वाले 🏏 Batsman हैं, तो वोटिंग से नया खिलाड़ी भी Batsman घोषित हो जाता है!
Words and symbols in this chapter
from sklearn.neighbors import KNeighborsClassifier- Imports the KNN classifier from
sklearn.neighbors. sklearn.neighborsसे KNN क्लासिफ़ायर मँगाता है।X_players = [[50, 1], ...]- Each player's
[Sixes Hit, Wickets Taken]coordinates. - हर खिलाड़ी के
[छक्के, विकेट]के आँकड़े। KNeighborsClassifier(k=3)k=3tells AI to check the 3 closest neighbours for voting.k=3का मतलब सबसे पास के 3 पड़ोसियों से वोटिंग करवाना।knn.predict([60, 1])- Finds the 3 nearest dots to
[60, 1]and returns the majority role! [60, 1]के सबसे नज़दीक 3 डॉट्स ढूँढकर खिलाड़ी का रोल बताता है!
Ready example
from sklearn.neighbors import KNeighborsClassifier
# [Sixes Hit, Wickets Taken]
X_players = [[50, 1], [65, 2], [70, 0], [2, 45], [5, 60], [1, 55]]
y_role = ["Batsman", "Batsman", "Batsman", "Bowler", "Bowler", "Bowler"]
knn = KNeighborsClassifier(k=3)
knn.fit(X_players, y_role)
print("Player Role:", knn.predict([60, 1]))
What you will see: 2D Cluster Map with 3 Dashed Laser Lines from ⭐ to its 3 closest neighbours + live Sixes/Wickets sliders to move ⭐ around!
2D क्लस्टर मैप पर नए प्लेयर ⭐ से 3 सबसे पास वाले डॉट्स तक लेज़र लाइन्स खिंचेंगी + आप स्लाइडर से ⭐ को घुमाकर लाइव वोटिंग देख सकेंगे!
Line by line
X_players = [[50, 1], [65, 2], [70, 0], [2, 45], [5, 60], [1, 55]]- 3 high-sixes Batsmen and 3 high-wickets Bowlers.
- 3 छक्कों वाले Batsman और 3 विकेट वाले Bowler का डेटा।
knn = KNeighborsClassifier(k=3) / knn.fit(...)- Stores all 6 players on the 2D Cluster Map.
- सभी 6 खिलाड़ियों को 2D मैप पर सेट करता है।
knn.predict([60, 1])- Draws 3 laser lines to
[50,1],[65,2],[70,0]→'Batsman'! - 3 सबसे करीबी पड़ोसियों से लेज़र लाइन जोड़कर
'Batsman'बताता है!
Common mistake
✗ Wrong
knn = KNeighborsClassifier(k=0)✓ Right
knn = KNeighborsClassifier(k=3)k must be at least 1 (usually an odd number like 3 or 5 so voting has no tie)!
k कम से कम 1 होना चाहिए (अक्सर 3 या 5 विषम संख्या रखते हैं ताकि वोटिंग टाई न हो)!
Your task
Train KNeighborsClassifier(k=3) and predict the role of player [60, 1]!
KNeighborsClassifier(k=3) को ट्रेन करके [60, 1] वाले खिलाड़ी का रोल प्रेडिक्ट करो!
- Create
knn = KNeighborsClassifier(k=3).knn = KNeighborsClassifier(k=3)बनाओ। - Train with
knn.fit(X_players, y_role).knn.fit(X_players, y_role)चलाओ। - Print
knn.predict([60, 1])and click ▶ Run & Preview!knn.predict([60, 1])प्रिंट करके ▶ Run & Preview दबाओ!
Starter code for the task
from sklearn.neighbors import KNeighborsClassifier
# [Sixes Hit, Wickets Taken]
X_players = [[50, 1], [65, 2], [70, 0], [2, 45], [5, 60], [1, 55]]
y_role = ["Batsman", "Batsman", "Batsman", "Bowler", "Bowler", "Bowler"]
# TODO 1: knn = KNeighborsClassifier(k=3) banao (3 sabse paas wale padosi)
# TODO 2: knn.fit(X_players, y_role)
# TODO 3: print("Player Role:", knn.predict([60, 1]))Do this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
- KNN looks at the
knearest known points and takes a majority vote. - "Nearest" is measured by the straight-line distance on the feature map.
kmust be at least 1; an oddklike 3 avoids ties.
- KNN सबसे पास के
kजाने-पहचाने points देखकर बहुमत से फ़ैसला करता है। - "सबसे पास" feature मैप पर सीधी दूरी से नापा जाता है।
kकम से कम 1 हो; 3 जैसा विषमkबराबरी (tie) से बचाता है।
Quick quiz
How does KNN (KNeighborsClassifier(k=3)) classify a mystery player?
KNN (k=3) किसी नए खिलाड़ी की पहचान कैसे करता है?
Show the answer
Answer: B (Majority vote of the 3 closest data points on the map)
KNN measures distance and takes a majority vote of the K nearest neighbours!
KNN सबसे नज़दीकी 3 पड़ोसियों से वोटिंग (बहुमत) करवाकर फ़ैसला लेता है!
Practice problems for this chapter
- Chai or Lassi? KNN Neighbours (fill the blanks)
चाय या लस्सी? KNN पड़ोसी (खाली जगह भरो)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.