You are on page 1of 19

TUGAS BESAR

PENGANTAR DATA MINING

PENERAPAN METODE KLASIFIKASI DENGAN ALGORITMA NAÏVE


BAYES PADA DATA STATLOG GERMAN CREDIT DATA

Disusun Oleh:
Adi Gemilang (H12114021)

DEPARTEMEN STATISTIKA
FAKULTAS MATEMATIKA DAN ILMU PENGETAHUAN ALAM
UNIVERSITAS HASANUDDIN
2019
A. Sumber Data
Penelitian ini menggunakan data sekunder yang diperoleh dari laman:
https://archive.ics.uci.edu/ml/datasets/statlog+(german+credit+data) yang terdiri
dari 1000 observasi dan 21 variabel. Variabel respon yang diamati adalah status
kredit nasabah di negara Jerman.
Deskripsi dari data statlog (German Credit Data) yaitu sebagai berikut.
1. Title: German Credit data
2. Source Information
Professor Dr. Hans Hofmann
Institut f"ur Statistik und "Okonometrie
Universit"at Hamburg
FB Wirtschaftswissenschaften
Von-Melle-Park 5
2000 Hamburg 13
3. Number of Instances: 1000
4. Number of Attributes german: 20 (7 numerical, 13 categorical)
Number of Attributes german.numer: 24 (24 numerical)
5. Attribute description for german
Attribute 1: (qualitative)
Status of existing checking account
A11 : ... < 0 DM
A12 : 0 <= ... < 200 DM
A13 : ... >= 200 DM /salary assignments for at least 1 year
A14 : no checking account
Attribute 2: (numerical)
Duration in month
Attribute 3: (qualitative)
Credit history
A30 : no credits taken/all credits paid back duly
A31 : all credits at this bank paid back duly
A32 : existing credits paid back duly till now
A33 : delay in paying off in the past
A34 : critical account/other credits existing (not at this bank)
Attribute 4: (qualitative)
Purpose
A40 : car (new)
A41 : car (used)
A42 : furniture/equipment
A43 : radio/television
A44 : domestic appliances
A45 : repairs
A46 : education
A47 : (vacation - does not exist?)
A48 : retraining
A49 : business
A410 : others
Attribute 5: (numerical)
Credit amount
Attibute 6: (qualitative)
Savings account/bonds
A61 : ... < 100 DM
A62 : 100 <= ... < 500 DM
A63 : 500 <= ... < 1000 DM
A64 : .. >= 1000 DM
A65 : unknown/ no savings account
Attribute 7: (qualitative)
Present employment since
A71 : unemployed
A72 : ... < 1 year
A73 : 1 <= ... < 4 years
A74 : 4 <= ... < 7 years
A75 : .. >= 7 years
Attribute 8: (numerical)
Installment rate in percentage of disposable income
Attribute 9: (qualitative)
Personal status and sex
A91 : male : divorced/separated
A92 : female : divorced/separated/married
A93 : male : single
A94 : male : married/widowed
A95 : female : single
Attribute 10: (qualitative)
Other debtors / guarantors
A101 : none
A102 : co-applicant
A103 : guarantor
Attribute 11: (numerical)
Present residence since
Attribute 12: (qualitative)
Property
A121 : real estate
A122 : if not A121 : building society savings agreement/ life insurance
A123 : if not A121/A122 : car or other, not in attribute 6
A124 : unknown / no property
Attribute 13: (numerical)
Age in years
Attribute 14: (qualitative)
Other installment plans
A141 : bank
A142 : stores
A143 : none
Attribute 15: (qualitative)
Housing
A151 : rent
A152 : own
A153 : for free
Attribute 16: (numerical)
Number of existing credits at this bank
Attribute 17: (qualitative)
Job
A171 : unemployed/ unskilled - non-resident
A172 : unskilled - resident
A173 : skilled employee / official
A174 : management/ self-employed/ highly qualified employee/ officer
Attribute 18: (numerical)
Number of people being liable to provide maintenance for
Attribute 19: (qualitative)
Telephone
A191 : none
A192 : yes, registered under the customers name
Attribute 20: (qualitative)
foreign worker
A201 : yes
A202 : no
6. Cost Matrix
This dataset requires use of a cost matrix (see below)
1 2
-----------------------
1 0 1
-----------------------
2 5 0
(1 = Good, 2 = Bad)
The rows represent the actual classification and the columns the predicted
classification. It is worse to class a customer as good when they are bad (5), than
it is to class a customer as bad when they are good (1).
B. Pembagian Data
Sintaks untuk melakukan pembagian data 80% dan 20% pada R Studio yaitu
sebagai berikut.

data <- data.frame(as.data.set(spss.system.file('F:/Data Tugas.sav')))


s<- sample(2, nrow(data), replace = T, prob = c(0.8,0.2))
data_training <- data [s==1,]
data_testing <- data [s==2,]

Hasil pembagian data 80% dan 20% berdasarkan data di atas yaitu:

C. Penerapan Metode Klasifikasi dengan Algoritma Naïve Bayes


Algoritma Naive Bayes merupakan sebuah metoda klasifikasi menggunakan
metode probabilitas dan statistik yg dikemukakan oleh ilmuwan Inggris Thomas
Bayes. Algoritma Naive Bayes memprediksi peluang di masa depan berdasarkan
pengalaman di masa sebelumnya sehingga dikenal sebagai Teorema Bayes. Ciri
utama dr Naïve Bayes Classifier ini adalah asumsi yg sangat kuat (naïf) akan
independensi dari masing-masing kondisi / kejadian.
Naive Bayes Classifier bekerja sangat baik dibanding dengan model
classifier lainnya. Hal ini dibuktikan pada jurnal Xhemali, Daniela, Chris J.
Hinde, and Roger G. Stone. “Naive Bayes vs. decision trees vs. neural networks in
the classification of training web pages.” (2009), mengatakan bahwa “Naïve
Bayes Classifier memiliki tingkat akurasi yg lebih baik dibanding model classifier
lainnya”.
Keuntungan penggunan adalah bahwa metoda ini hanya membutuhkan
jumlah data pelatihan (training data) yang kecil untuk menentukan estimasi
parameter yg diperlukan dalam proses pengklasifikasian. Karena yg diasumsikan
sebagai variabel independent, maka hanya varians dari suatu variabel dalam
sebuah kelas yang dibutuhkan untuk menentukan klasifikasi, bukan keseluruhan
dari matriks kovarians.
Setelah dilakukan pembagian data, maka dilakukan klasifikasi
menggunakan algoritma Naïve Bayes pada data training (80%). Sintaks untuk
penerapan algoritma Naïve Bayes pada R Studio yaitu sebagai berikut.

library(naivebayes)
data <- data.frame(as.data.set(spss.system.file('F:/Data Tugas.sav')))
s<- sample(2, nrow(data), replace = T, prob = c(0.8,0.2))
data_training <- data [s==1,]
data_testing <- data [s==2,]

nb<- naive_bayes(y~.,data= data_training, userkernel=7)


plot(nb)
train_predict <- predict (nb,data_training[,names(data_training)!="y"],
type="class")

hitung <- data.frame(data_training)


naivobes <- naive_bayes(y ~ ., data = hitung, method = "class")
print(naivobes$tables)
plot(naivobes)

library(caret)
cfm <- confusionMatrix(train_predict, data_training$y)

Maka diperoleh probabilitas yaitu:


$x1
x1 good bad
...<0 DM 0.20262664 0.45416667
0<=...<200 DM 0.21951220 0.32083333
...>=200 DM 0.06566604 0.05833333
No Checking Account 0.51219512 0.16666667
$x2
x2 good bad
mean 19.53659 25.25833
sd 11.31143 13.60101

$x3
x3
good bad
no credits taken/all credits paid back duly 0.01876173 0.08750000
all credits at this bank paid back duly 0.03001876 0.07916667
existing credits paid back duly till now 0.51594747 0.57500000
delay in paying off in the past 0.08067542 0.08750000
critical account/other credits existing 0.35459662 0.17083333

$x4
x4 god bad
car (new) 0.215759850 0.325000000
car (used) 0.131332083 0.066666667
furniture/equipment 0.170731707 0.175000000
radio/television 0.313320826 0.208333333
domestic appliances 0.011257036 0.016666667
repairs 0.015009381 0.020833333
education 0.048780488 0.062500000
(vacation - does not exist?) 0.000000000 0.000000000
retraining 0.007504690 0.004166667
business 0.086303940 0.120833333
others 0.000000000 0.000000000

$x5
x5 good bad
mean 2995.081 3983.192
sd 2431.035 3483.162

$x6
x6 good bad
...<100 DM 0.54221388 0.72916667
100<=...<500 DM 0.09380863 0.10000000
500<=...<1000 DM 0.08067542 0.03750000
...>=1000 DM 0.06191370 0.02083333
unknown/no savings account 0.22138837 0.11250000

$x7
x7 good bad
unemployed 0.04690432 0.07500000
...<1 year 0.14821764 0.20000000
1<=...<4 years 0.32833021 0.35833333
4<=...<7 years 0.19324578 0.13333333
...>=7 years 0.28330206 0.23333333

$x8
x8 good bad
mean 2.951220 3.112500
sd 1.129518 1.078435
$x9
x9 good bad
male : divorced/separated 0.05065666 0.05833333
female : divorced/separated/married 0.28705441 0.35833333
male : single 0.57786116 0.50833333
male : married/widowed 0.08442777 0.07500000
female : single 0.00000000 0.00000000

$x10
x10 good bad
none 0.91181989 0.89583333
co-applicant 0.03377111 0.07083333
guarantor 0.05440901 0.03333333

$x11
x11 good bad
mean 2.863039 2.837500
sd 1.121393 1.098996

$x12
x12
good
real estate 0.3039400
if not A121 0.2288931
if not A121/A122 0.3283302
unknown / no property 0.1388368

x12
bad
real estate 0.2000000
if not A121 0.2125000
if not A121/A122 0.3583333
unknown / no property 0.2291667

$x13
x13 good bad
mean 36.69606 34.49583
sd 11.41434 11.29298

$x14
x14 good bad
bank 0.12570356 0.18333333
stores 0.04315197 0.06250000
none 0.83114447 0.75416667

$x15
x15 good bad
rent 0.14258912 0.22500000
own 0.75797373 0.62500000
for free 0.09943715 0.15000000

$x16
x16 good bad
mean 1.4277674 1.3791667
sd 0.6014848 0.5582992
$x17
x17
good
unemployed/ unskilled - non-resident 0.02439024
unskilled - resident 0.20262664
skilled employee / official 0.63227017
management/ self-employed/… 0.14071295

x17
bad
unemployed/ unskilled - non-resident 0.02500000
unskilled - resident 0.17500000
skilled employee / official 0.61250000
management/ self-employed/… 0.18750000

$x18
x18 good bad
mean 1.1669794 1.1583333
sd 0.3733078 0.3658162

$x19
x19 good bad
none 0.5684803 0.6250000
yes, registered under the customers name 0.4315197 0.3750000

$x20
x20 good bad
yes 0.95497186 0.98750000
no 0.04502814 0.01250000

Output grafik yang dihasilkan yaitu:


- X1

...<0 DM 0<=...<200 DM ...>=200 DM No Checking Account


good
bad

x1
- X2

0.030
good
bad
Density

0.015
0.000

10 20 30 40 50 60

x2

- X3
no creditsalltaken/all
credits credits
at this bank
paid back
paid existing
back
duly duly
credits paid back duly tilldelay
nowcritical
in paying
account/other
off in the past
credits existing (not at this bank)
good
bad

x3

- X4
car (new ) car (used)
furniture/equipment radio/television
domestic (vacation
appliances
repairs - retraining
does not
education exist?)
business
others
good
bad

x4
- X5

good
bad
0.00010
Density

0.00000

0 5000 10000 15000

x5

- X6

...<100 DM 100<=...<500 unknow


500<=...<1000
DM...>=1000 n/no savings account
DM DM
good
bad

x6

- X7
- X8

- X9

- X10
- X11

- X12

- X13
- X14

- X15

- X16
- X17

- X18

- X19
- X20

D. Confussion Matrix
Confusion Matrix berisikan informasi yang aktual dan prediksi pada suatu
sistem klasifikasi dan memberikan penillaian performance klasifiikasi yang
berdasarkan pada objek dengan benar atau salah. Serta memberikan keputusan
yang diperoleh dalam training dan testing.

Keterangan dari tabel Confusion Matrix diatas yaitu:


1. True Positive (tp) adalah proporsi positive (benar) dalam dataset yang
dikategorikan positive (benar).
2. False Negative (fn) adalah proporsi negative (salah) dalam dataset yang
dikategorikan negative (salah).
3. False Positive (fp) adalah proporsi negative (salah) dalam dataset yang
dikategorikan positive (benar).
4. True Negative (tn) adalah proporsi positive (benar) dalam dataset yang
dikategorikan negative (salah).
Dari keterangan tabel Confusion Matrix diatas, berikut ini merupakan persamaan
pada Confusion Matrix:
1. NilaiaAkurasi (Acc) merupakan proporsi dari jumlahaprediksi yang benar
2. Sensitivity atau Recall biasanya untukamembandingkanaproporsi (tp) terhadap
proporsi yang positive (benar)
3. Specificity biasanya untuk membandingkan proporsi (tn) terhadap (tp) yang
negative (salah)
4. Positive Predictive Value (PPV) atau Precision merupakan suatu proporsi kasus
yang hasil prediksi positive (benar)
5. NegativeaPredictive Value (NPV) aadalah suatu proporsi kasus dengan hasil
yang diprediksi negative (salah)

Berikut ini sintaks pada R Studio untuk menentukan confussion matrix dari
klasifikasi menggunakan algoritma Naïve Bayes.

library(caret)
cfm <- confusionMatrix(train_predict, data_training$y)
print(cfm)

Outputnya sebagai berikut:

Confusion Matrix and Statistics

Reference
Prediction good bad
good 464 113
bad 69 127

Accuracy : 0.7646
95% CI : (0.733, 0.7941)
No Information Rate : 0.6895
P-Value [Acc > NIR] : 2.331e-06

Kappa : 0.4209
Mcnemar's Test P-Value : 0.001436

Sensitivity : 0.8705
Specificity : 0.5292
Pos Pred Value : 0.8042
Neg Pred Value : 0.6480
Prevalence : 0.6895
Detection Rate : 0.6003
Detection Prevalence : 0.7464
Balanced Accuracy : 0.6999

'Positive' Class : good

E. Prediksi
Prediksi menurut Restiana Putri adalah melakukan suatu perhitungan
sistematis yang digunakan untuk mengetahui suatu keadaan diamasa yang akan
dating berdasarkanadari pengujian keadaan masa yang sebelumnya. Sedangkan
prediksi menurut Heldi Diana dan Cahyo Dwi Raharjo yaitu suatu teknik yang
digunakan untuk menganalisa suatu faktor atau beberapa faktor untuk mengetahui
peristiwa masa mendatang berdasarkan acuan peristiwa masa lalu.
Dariabeberapaapendapat yang telah dikemukakan dapat disimpulkan bahwa
prediksi adalah suatu proses yang dapat memperkirakan secara sistematis terhadap
sesuatu kejadian yang memungkinkan terjadi dimasa yang akanadatang
berdasarkan pada informasi masa sebelumnya dan masa sekarang yang dimiliki.
Hasil dari dilakukannya prediksi tersebut tidak harus memberikan secara pasti
kejadian yang akan terjadi, tetapi sedemikan mungkin untuk memerikan solusi
atau jawaban terdekat yang mungkin hal tersebut akan terjadi.
Untuk melakukan prediksi pada kasus di atas, dilakukan pada data testing
(20%) dengan menggunakan probabilitas yang telah diperoleh sebelumnya.

You might also like