03 KNN Analogy 1 Rev2021

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

K-Nearest Neighbor

http://www.free-powerpoint-templates-design.com
Classification Model

K-Nearest Neighbor
Classifier
K-Nearest Neighbor Classifiers
Learning by analogy:
Tell me who your friends are and I’ll tell you
who you are
A new example is assigned to the most common
class among the (K) examples that are most
similar to it.
K-Nearest Neighbor Algorithm
• Untuk menentukan kelas dari data baru F:
• Hitung jarak antara F dengan semua data dalam data pelatihan (training set)
• Pilih sejumlah K data terdekat dengan F dari data pelatihan
• Tetapkan F ke kelas paling banyak di antara tetangga terdekat dalam K

Response
No response No response

Class: ??? No response


Response
K-Nearest Neighbor Classifier
Distance Between Neighbors
 Setiap data diwakili oleh satu set atribut

John: Rachel:
Age=35 Age=41
Income=135K Income=50K
No. of credit cards=3 No. of credit cards=2

 “Kedekatan” (Closeness) diukur dalam hal jarak Euclidean antara


dua data.
 Jarak Euclidean antara X=(x1, x2, x3,…xn) dan Y =(y1,y2, y3,…yn) is
adalah:
n
D( X , Y )   (x  y )
i 1
i i
2

 Distance (John, Rachel)=sqrt [(35-41)2+(135K-50K)2 +(3-2)2]


K-Nearest Neighbor: Instance Based Learning

• Tidak ada model yang dibuat: catat semua data pelatihan (training set)
• Pemrosesan hanya dilakukan jika ada data baru yang harus diklasifikasikan.

Response No response No response

Class: ??? No response


Response
Example : 3-Nearest Neighbors
Customer Age Income No. credit Response
cards
John 35 135K 3 Yes

Rachel 41 50K 2 No

Hannah 63 200K 1 No

Tom 59 170K 1 No

Nellie 25 40K 4 Yes

David 37 50K 2 ???


Example
Customer Age Income No. Response Distance from David
(K) cards
John 35 135 3 Yes sqrt [(35-37)2+(135K-
50K)2 +(3-2)2]=85.000
Rachel 41 50 2 No sqrt [(41-37)2+(50K-
50K)2 +(2-2)2]=4
Hannah 63 200 1 No sqrt [(63-37)2+(200K-
50K)2 +(1-2)2]=150.000
Tom 59 170 1 No sqrt [(59-37)2+(170K-
50K)2 +(1-2)2]=120.000
Nellie 25 40 4 Yes sqrt [(25-37)2+(40K-
50K)2 +(4-2)2]=10.000
David 37 50 2 Yes
Example
Customer Age Income No. Response Distance from David
(K) cards
John 35 135 3 Yes sqrt [(35-37)2+(135-
50)2 +(3-2)2]=85.02
Rachel 41 50 2 No sqrt [(41-37)2+(50-50)2
+(2-2)2]=4
Hannah 63 200 1 No sqrt [(63-37)2+(200-
50)2 +(1-2)2]=152.23
Tom 59 170 1 No sqrt [(59-37)2+(170-
50)2 +(1-2)2]=122
Nellie 25 40 4 Yes sqrt [(25-37)2+(40-50)2
+(4-2)2]=15.74
David 37 50 2 Yes
K-Nearest Neighbor Classifier
Strengths and Weaknesses

Strengths:
 Simple to implement and use
 Comprehensible – easy to explain prediction
 Robust to noisy data by averaging k-nearest neighbors.
 Some appealing applications (will discuss next in personalization )
Weaknesses:
 Need a lot of space to store all examples.
 Takes more time to classify a new example than with a model (need
to calculate and compare distance from new example to all other
examples).
Section Break
Insert the Subtitle of Your Presentation
K-Nearest Neighbor Classifier
Classification Tree Modes K-Nearest Neighbors
Customer Age Income No. credit Response
cards

Age > 50 John 35 135K 3 Yes

Rachel 41 50K 2 No
Yes No Hannah 63 200K 1 No

Tom 59 170K 1 No

Class=No Nellie 25 40K 4 Yes


Income<100K
Response David 37 50K 2 ???

Yes No

No No cards>1 Response
Response No response No
Yes No response

Response No Response Class: Response


No response
Response
Strengths and Weaknesses
K-Nearest Neighbor Classifier

John: Rachel:
Age=35 Age=41
Inco me=135K Income=50K
No. of credit cards=3 No. of credit cards=2

Distance (John, Rachel)=sqrt [(35-41)2+(135,000-50,000)2 +(3-2)2]

• Jarak dengan tetangga didominasi oleh atribut dengan nilai yang


besar (dalam kasus ini: income). Disinilah PENTING untuk dilakukan
teknik NORMALISASI (misal: memetakan nilai ke nilai konversi
antara 0-1)
Example: Income
Highest income = 200K
Rachel income dinormalisasi menjadi 50/200, John income menjadi
135/200, dst
Strengths and Weaknesses
K-Nearest Neighbor Classifier

Normalization of Variables
Customer Age Income No. Response
(K) cards

John 35/63= 135/200= ¾= Yes


0.55 0.675 0.75

Rachel 41/63= 50/200= 2/4= No


0.65 0.25 0.5

Hannah 63/63= 200/200=1 ¼= No


1 0.25

Tom 59/63= 170/200=0. ¼= No


0.93 85 0.25

Nellie 25/63= 40/200= 4/4= Yes


0.39 0.2 1

David 37/63= 50/200= 2/4=


0.58 0.25 0.5 No
Strengths and Weaknesses
K-Nearest Neighbor Classifier

• Distance berjalan secara natural pada data yang


numerik
D(Rachel&John)= sqrt [(35-41)2+(135-50)2 +(3-2)2]=85.21
• Bagaimana jika kita memiliki atribut yang nominal?
Contoh: married
Customer Married Income No. Response
(K) cards

John Yes 135 3 No


Rachel No 50 2 Yes
Hannah No 200 1 No
Tom Yes 170 1 No
Nellie No 40 4 Yes
David Yes 50 2
Strengths and Weaknesses
K-Nearest Neighbor Classifier

Tugas Anda

Customer Married Age Income No. credit Response


cards
John Yes 35 135K 3 Yes

Rachel No 41 50K 2 No

Hannah No 63 200K 1 No

Tom Yes 59 170K 1 No

Nellie No 25 40K 4 Yes

<saya> ... ... 50K .... ???

1. Ganti data ke-6 dengan data Anda


2. Lakukan normalisasi pada atribut: Age, Income, No. Credit Card
3. Prediksi kelas ANDA, dengan metode KNN, gunakan K=3
Lakukan di ms-excel, tampilkan hasil perhitungan jarak, tandai 3 data yang terdekat.
Ringkasan
Algoritma k-nearest neighbor (KNN) adalah
algoritma pembelajaran terbimbing
(supervised machine learning algorithm)
yang dapat digunakan untuk
menyelesaikan masalah klasifikasi dan
regresi. Sangat mudah untuk diterapkan
dan dipahami, tetapi memiliki kelemahan
utama yaitu menjadi sangat lambat seiring
dengan bertambahnya ukuran data yang
digunakan.
17
THANK YOU
Insert the Subtitle of Your Presentation
Catatan Terkait Berdasarkan proses kuantifikasi data, biasanya variabel
dibedakan menjadi 4 jenis, yaitu:

Variabel Nominal; variabel yang ditetapkan atas proses penggolongan,


variabel ini bersifat diskret dan saling pilah antara kategori satu dengan

Data Kategorik
kategori lainnya. Contohnya seperti status perkawinan, jenis pekerjaan, dan
jenis kelamin.

Variabel Ordinal; variabel yang tersusun berdasarkan jenjang dalam


atribut tertentu. Biasanya jenjang tertinggi diberi angka 1, jenjang di
bawahnya diberi angka 2, lalu di bawahnya diberi angka 3 dan seterusnya.

Variabel Interval; biasanya dihasilkan dari pengukuran, yang mana dalam


pengukuran tersebut diasumsikan terdapat satuan pengukuran yang sama.
Data Numerik

Misalnya seperti prestasi belajar, penghasilan, sikap terhadap suatu


program yang dinyatakan dalam skor, dan sebagainya.

Variabel Ratio; Variabel yang dalam kuantifikasinya mempunyai nol


mutlak.

https://dqlab.id/digital-transformation-kenali-macam-macam-data-dalam-ilmu-data-science

You might also like