Python - Classification - K-Nearest Neighbors (KNN)


Questionnaire data from mall visitors contains sex, age, salary & shopping score (200 rows)


How to predict the probability of shopping score from given age & salary

Library used:

  • Pandas
  • Matplotlib
  • Scikit


import pandas as pd
import matplotlib.pyplot as plt
from sklearn.neighbors import KNeighborsRegressor

url = ''
vlog122 = pd.read_csv(url)

X = vlog122[['Usia','Gaji (juta)']]
y = vlog122['Skor Belanja (1-100)']

knn = KNeighborsRegressor(n_neighbors=3),y)

usia = input("Age (years): ")
usia = int(usia)
gaji = input("Salary (mio): ")
gaji = int(gaji)
data = [usia,gaji]
print("Shopping score prediction (1-100): ", knn.predict([data]))

plt.scatter(vlog122[['Usia']],y, color='green')
plt.scatter(gaji,knn.predict([data]), color='red')

I wrapped the scenario in a Youtube video below.

Click this link (, if you want to check out for other algorithms. Thank you for for visiting this blog & subs my channel. 

Labels: ,

PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address to receive feed update from this blog:

Post a Comment


Post a Comment

Leave comments here...