Klasifikasi
S1 Teknik Informatika Fakultas Teknologi Informasi Universitas Kristen Maranatha
Agenda
• Pendahuluan
• Klasifikasi
• Induksi Pohon Keputusan
• Klasifikasi Bayesian classification
2
Pendahuluan
• Klasifikasi:
• klasifikasi data berdasarkan training set and nilai-nilai (class labels) dalam klasifikasi atribut, dan menggunakannya dalam klasifikasi data baru
• memprediksi label kelas secara kategorikal (diskrit/nominal)
• Prediksi memodelkan fungsi kontinyu, mis. prediksi nilai hilang atau tidak diketahui
• Contoh aplikasi
• Persetujuan kredit: pinjaman aman atau beresiko
• Target pemasaran: konsumen potensial
• Diagnosis medis
• Deteksi penipuan
• Teknik-teknik :
• Klasifikasi: decision tree induction, Bayesian classification, Bayesian belief network, neural network, k-nearest neighbour classifier, CBR, algoritma genetic, rough set, fuzzy logic
• Prediksi: Linear, nonlinear, generalized linear regression
3
Klasifikasi: Proses Dua Langkah
• Konstruksi Model: menjelaskan sebuah set dari class yang ditentukan sebelumnya
• Setiap sample diasumsikan termasuk class yang ditentukan sebelumnya, ditentukan oleh label atrbut dari class
• Kumpulan sample yang dipakai untuk konstruksi model: set training
• Model direpresentasikan sebagai aturan klasifikasi, pohon keputusan, atau rumus matematka
• Pemakaian Model: mengklasifikasi obyek – obyek di masa datang atau yang tidak diketahui
• Estimasi keakuratan model
• Label yang diketahui dari test sample dibandingkan dengan hasil klasifikasi dari model
• Tingkat keakuratan = persentase dari kumpulan test sample yang diklasifikasikan dengan benar oleh model
• Set tes bersifat independen terhadap set training
• Jika tingkat keakuratan dapat diterima, pakai model untuk klasifikasi sample data yang class labelnya belum diketahui
4
Proses (1): Konstruksi Model
DM-MA/S1IF/FTI/UKM/2010 5
Training Data
N A M E R A N K Y E A R S T E N U R E D M ike A ssistant P rof 3 no M ary A ssistant P rof 7 yes
Bill P rofessor 2 yes
Jim A ssociate Prof 7 yes
Dave A ssistant P rof 6 no Anne A ssociate Prof 3 no
Algoritma Klasifikasi
IF rank = ‘professor’
OR years > 6
THEN tenured = ‘yes’
Klasifier (Model)
Proses (2): Menggunakan Model dalam Prediksi
6
Klasifier
Data Testing
N A M E R A N K Y E A R S T E N U R E D
Tom Assistant Prof 2 no
M erlisa Associate Prof 7 no
George Professor 5 yes
Joseph Assistant Prof 7 yes
Data Tidak Diketahui (Jeff, Professor, 4)
Tenured?
Evaluasi metode Klasifikasi
• Keakuratan Prediksi:
• kemampuan model untuk memprediksi secara benar class label untuk data baru
• Kecepatan:
• biaya komputasi untuk menghasilkan dan menggunakan model
• Kehandalan:
• kemampuan model untuk membuat prediksi yang benar jika terdapat noise atau missing data
• Skalabilitas:
• kemampuan membangun model secara efisien dalam data yang berjumlah sangat besar
• Pemahaman:
• tingkat pemahaman dan pengertian yang disediakan oleh model
7
Induksi Pohon Keputusan
• Pohon keputusan: struktur pohon, tiap internal node menyatakan sebuah test pada suatu atribut, tiap cabang menyatakan hasil test, dan node daun menyatakan class.
Node paling atas adalah node akar (root).
• Algoritma pembentukan pohon: (top down recursive, divide and conquer)
• Tree mulai dengan node akar
• Jika seluruh sample ada di class yang sama, maka node tsb menjadi leaf dan diberi label dengan class tsb.
• Jika tidak, gunakan information gain untuk memilih atribut yang paling baik dalam memisahkan sample ke class.
• Buat cabang untuk tiap nilai dari atribut test
• Ulangi proses pembuatan tree sampai :
• Seluruh sample masuk ke class yang sama, atau
• Tidak terdapat lagi atribut yang dapat memisahkan data sample
• Tidak terdapat sample untuk cabang test atribut
8
Induksi Pohon Keputusan:
Training Dataset
age income student credit_rating buys_computer
<=30 high no fair no
<=30 high no excellent no
31…40 high no fair yes
>40 medium no fair yes
>40 low yes fair yes
>40 low yes excellent no
31…40 low yes excellent yes
<=30 medium no fair no
<=30 low yes fair yes
>40 medium yes fair yes
<=30 medium yes excellent yes
31…40 medium no excellent yes
31…40 high yes fair yes
>40 medium no excellent no
9
Keluaran: Sebuah Pohon Keputusan untuk “buys_computer”
10
age?
overcast
student? credit rating?
<=30 >40
no yes yes
yes
31..40
fair excellent
yes
no
Pengukuran Seleksi Atribut:
Information Gain (ID3/C4.5)
Pilih atribut dengan information gain tertinggi
Bila p
i: probabilitas sembarang tuple dalam D termasuk class C
i, diestimasi sbg |C
i, D|/|D|
Expected information (entropy) untuk klasifikasi suatu tuple dalam D:
Information (setelah memakai A utk membagi D ke dlm v partisi) utk klasifikasi D:
Information gained dengan pencabangan pada atribut A ) ( log )
(
21
i m
i
i
p
p D
Info ∑
=
−
=
)
| (
|
| ) |
(
1
j v
j j
A
I D
D D D
Info = ∑ ×
=
(D) Info Info(D)
Gain(A) = −
A11
Seleksi Atribut: Information Gain
gClass P: buys_computer = “yes”
gClass N: buys_computer = “no”
means “age <=30” has 5 out of 14 samples, with 2 yes’es and 3 no’s. Hence
Similarly,
DM-MA/S1IF/FTI/UKM/2010 12
age pi ni I(pi, ni)
<=30 2 3 0.971
31…40 4 0 0
>40 3 2 0.971
694 . 0 ) 2 , 3 14 (
5
) 0 , 4 14 ( ) 4 3 , 2 14 ( ) 5 (
= +
+
= I
I I
D Infoage
048 . 0 ) _
(
151 . 0 ) (
029 . 0 ) (
=
=
= rating credit
Gain
student Gain
income Gain
246 . 0 ) ( )
( )
(age =Info D −Info D =
Gain age
age income student credit_rating buys_computer
<=30 high no fair no
<=30 high no excellent no
31…40 high no fair yes
>40 medium no fair yes
>40 low yes fair yes
>40 low yes excellent no
31…40 low yes excellent yes
<=30 medium no fair no
<=30 low yes fair yes
>40 medium yes fair yes
<=30 medium yes excellent yes
31…40 medium no excellent yes
31…40 high yes fair yes
>40 medium no excellent no
) 3 , 2 14 (
5 I
940 . 0 14) (5 14log ) 5 14 (9 14log ) 9 5 , 9 ( )
(D = I =− 2 − 2 =
Info
Klasifikasi dalam Basis Data Besar
• Klasifikasi — problem klasik yang diteliti secara ekstensif oleh ahli statistik dan peneliti machine learning
• Skalabilitas: klasifikasi kumpulan data dengan jutaan contoh dan ratusan atribut dengan kecepatan yang masuk akal
• Mengapa memakai induksi pohon keputusan dalam data mining?
• Kecepatan belajar relatif lebih tinggi (dibandingkan cara klasifikasi yang lain)
• Mudah diubah menjadi aturan dan mudah dipahami
• Dapat memakai SQL query utk mengakses basis dat
• Tingkat akurasinya dapat setara dengan metode klasifikasi yang lain
13
Klasifikasi Bayesian
• Bayesian classifier : klasifier statistik.
Dapat memprediksikan kemungkinan keanggotaan class, misalnya probabilitas suatu sample menjadi anggota suatu class tertentu.
• Bayesian classification didasari oleh teorema Bayes
14
P(X|H) P(H) P(H|X) = ---
P(X)
Teorema Bayes
• X : data sample yang label classnya belum diketahui
• H : hipotesis, misalnya data sample X anggota class C.
• Untuk classification, kita ingin menentukan P(H|X), yaitu probabilitas hipotesis H dipenuhi terhadap sample data X.
• P(H|X) : posterior probability / posteriori probability untuk H sesuai kondisi X.
• Misalnya Buah, digambarkan dengan warna dan bentuk.
Jika X : merah,bulat ; H : X adalah apel maka
P(H|X) : keyakinan bahwa X adalah apel karena X adalah merah dan bulat
15
Teorema Bayes
•
P(H) : prior probability / probability awal dari H.
Mis. probabilitas bahwa data sample adalah apel, tanpa peduli bagaimana wujud sample
•
P(X|H) : posterior probability untuk X, probabilitas observasi sample X, bila hipotesis dipenuhi
Mis. probabilitas X adalah merah & bulat jika kita tahu bahwa X adalah apel.
•
P(X) : prior probability dari X, yaitu probability bahwa sample data diobservasi
Mis. probabilitas bahwa data sample adalah merah &
bulat.
16
Klasifikasi Naive Bayesian
Cara kerja naive Bayesian :
•
Tiap data sample dengan n atribut disajikan dalam bentuk n-dimensional feature vector, X = (x
1,x
2,….,x
n)
•
Misalkan terdapat m class, C
1,C
2, … C
m. Dengan data sample X, klasifier akan
memprediksi bahwa X adalah anggota class yang memiliki posterior probability tertinggi dengan kondisi X.
17
Klasifikasi Naïve Bayesian
• Sesuai teorema Bayes:
• Karena P(X) konstan untuk seluruh class, maka hanya P(X|Ci) P(Ci) yang perlu dimaksimalkan.
• P(Ci) = Si/S, dengan Si adalah jumlah training sample dari class Ci, dan S adalah jumlah seluruh training sample.
• Karena menghitung P(X|Ci) memerlukan komputasi mahal, maka dibuat asumsi yaitu bahwa tidak ada hubungan ketergantungan antar atribut.
• Hitung P(Xk|Ci) = Sik/Si. Sik adalah jumlah training sample class Ci yang mempunyai nilai Xk, Si adalah jumlah training sample dari class Ci.
18
P(X|Ci) P(Ci) P(Ci|X) = ---
P(X)
Klasifier Naïve Bayesian: Training Dataset
age income student credit_rating buys_c
<=30 high no fair no
<=30 high no excellent no
31…40 high no fair yes
>40 medium no fair yes
>40 low yes fair yes
>40 low yes excellent no 31…40 low yes excellent yes
<=30 medium no fair no
<=30 low yes fair yes
>40 medium yes fair yes
<=30 medium yes excellent yes 31…40 medium no excellent yes
31…40 high yes fair yes
>40 medium no excellent no
19
Class:
C1:buys_computer = ‘yes’
C2:buys_computer = ‘no’
Data sample X = (age <=30, Income = medium, Student = yes Credit_rating = Fair)
Klasifier Naïve Bayesian: Contoh
• P(Ci): P(buys_computer = “yes”) = 9/14 = 0.643
P(buys_computer = “no”) = 5/14= 0.357
• Compute P(X|Ci) for each class
P(age = “<=30” | buys_computer = “yes”) = 2/9 = 0.222 P(age = “<= 30” | buys_computer = “no”) = 3/5 = 0.6 P(income = “medium” | buys_computer = “yes”) = 4/9 = 0.444 P(income = “medium” | buys_computer = “no”) = 2/5 = 0.4 P(student = “yes” | buys_computer = “yes) = 6/9 = 0.667 P(student = “yes” | buys_computer = “no”) = 1/5 = 0.2 P(credit_rating = “fair” | buys_computer = “yes”) = 6/9 = 0.667 P(credit_rating = “fair” | buys_computer = “no”) = 2/5 = 0.4
20
• X = (age <= 30 , income = medium, student = yes, credit_rating = fair)
P(X|Ci) : P(X|buys_computer = “yes”) = 0.222 x 0.444 x 0.667 x 0.667
= 0.044
P(X|buys_computer = “no”) = 0.6 x 0.4 x 0.2 x 0.4 = 0.019
P(X|Ci)*P(Ci) : P(X|buys_computer = “yes”) * P(buys_computer =
“yes”) = 0.028 (MAX)
P(X|buys_computer = “no”) * P(buys_computer = “no”)
= 0.007
Therefore, X belongs to class (“buys_computer = yes”)
Menghindari Masalah 0-Probability
• Naïve Bayesian prediction memerlukan setiap conditional prob. harus non- zero. Jika tidak, predicted probability akan bernilai 0
• Mis. Terdapat dataset dengan 1000 tuples, income=low (0), income=
medium (990), and income = high (10),
• Memakai Laplacian correction ( Laplacian estimator)
• Tambahkan 1 pada setiap kasus Prob(income = low) = 1/1003 Prob(income = medium) = 991/1003 Prob(income = high) = 11/1003
• Estimasi “corrected” prob. mendekati perhitungan “uncorrected”
∏
=
= n
k P xk Ci X Ci
P
1 )
| ( )
| (
21
Klasifier Naïve Bayesian: Komentar
• Keuntungan
• Mudah diimplementasikan
• Memberikan hasil cukup baik pada banyak kasus
• Kerugian
• Asumsi: class conditional independence => kehilangan akurasi
• Secara Praktis, dependency ada di antara variabel
• Mis., hospitals: patients: Profile: age, family history, dll Symptoms: fever, cough , dll Disease: lung cancer, diabetes, dll
• Dependency antara variabel tidak dapat dimodelkan dengan Naïve Bayesian Classifier
• Bagaimana menangani dependency?
• Bayesian Belief Networks
22
Menggunakan Aturan IF-THEN untuk Klasifikasi
• Representasi pengetahuan dalam bentukIF-THENrules R: IF age = youth AND student = yes THEN buys_computer = yes
• Rule antecedent/precondition vs. rule consequent
• Penilaian rule: coverage and accuracy
• ncovers= # of tuples covered by R
• ncorrect= # of tuples correctly classified by R coverage(R) = ncovers/|D| /* D: training data set */
accuracy(R) = ncorrect/ ncovers
• Jika lebih dari satu rule ditrigger, maka perlu conflict resolution
• Size ordering: berikan prioritas tinggi untuk rules yang bersifat “toughest” (mis., rule dengan most attribute test)
• Class-based ordering: mengurangi order dari prevalence atau misclassification cost per class
• Rule-based ordering (decision list): rules diorganisasi menjadi satu daftar prioritas, mengikuti ukuran kualitas rule atau saran pakar.
23
age?
student? credit rating?
<=30 >40
no yes yes
yes 31..40
fair excellent yes
no
Ekstraksi aturan dari Pohon Keputusan
• Example: Rule extraction from our buys_computer decision-tree IF age = young AND student = no THEN buys_computer = no IF age = young AND student = yes THEN buys_computer = yes IF age = mid-age THEN buys_computer = yes IF age = old AND credit_rating = excellent THEN buys_computer = yes IF age = young AND credit_rating = fair THEN buys_computer = no
24
• Rules lebih mudah dipahami dibandingkan tree
• Satu rule diciptakan untuk setiap jalur dari akar ke daun
• Setiap pasang attribute-value dalam suatu jalur membentuk conjunction: daun adalah class prediction
• Rules bersifat mutually exclusive dan exhaustive
Latihan
id member age student income class:buys_funky_tshirt
1 no <=20 n high n
2 yes <=20 n high n
3 no 21 .. 25 n high y
4 no 26 .. 30 n medium y
5 no 26 .. 30 y low y
6 yes 26 .. 30 y low n
7 yes 21 .. 25 y low y
8 no <=20 n medium n
9 no <=20 y low y
10 no 26 .. 30 y medium y
11 yes <=20 y medium y
12 yes 21 .. 25 n medium y
25
Latihan
• Dengan training data set tersebut, buatlah decision tree-nya. Jangan lupa sertakan langkah-langkah, perhitungan, serta
pertimbangan untuk menghilangkan node tertentu (jika ada).
• Cari prediksi dengan naïve Bayesian classifier untuk sample X = (member=yes, age=26..30, student=no, income=high),
sertakan langkah-langkah dan perhitungannya.
26