Naive Bayes adalah algoritma klasifikasi pembelajaran yang diawasi berdasarkan Teorema Bayes. Ini adalah perluasan dari pengklasifikasi Exact Bayes dan menganggap semua variabel independen satu sama lain. Metode ini cocok ketika dimensi input tinggi. Ini menggunakan metode probabilitas bersyarat sederhana dan dapat murah secara komputasi dan mengungguli model yang lebih canggih. Kami akan kembali menggunakan set data debat Presiden untuk klasifikasi dan membandingkannya dengan hasil klasifikasi K Nearest Neighbor.
Mari kita mulai dengan memuat pustaka yang diperlukan dan mengimpor lembar data yang akan kita gunakan untuk model ini.
#- set working directory
setwd("D:/Algoritma Naive Bayes/Algoritma Naive Bayes dan R/Algoritma-Naive-Bayes-dan-R")
options(scipen = 999)
#- load required libraries
library(knitr)
library(ggplot2)
library(tidyr)
library(caret)
## Loading required package: lattice
library(e1071)
library(ROCR)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
#- Import Data
library(readxl)
Breast_Cancer <- read_excel("Breast_Cancer.xlsx",
sheet = "Breast_Cancer")
View(Breast_Cancer)
Sebelum kita melangkah lebih jauh, penting untuk melakukan analisis data eksplorasi dasar. Karena kedua hasil cukup terwakili dalam data, oversampling tidak diperlukan. Juga, tidak ada yang aneh dalam plot distribusi.
#dependent Variabel
#dependent variable
kable(table(Breast_Cancer$Status),
col.names = c("Status", "Frequency"), align = 'l')
| Status | Frequency |
|---|---|
| 0 | 626 |
| 1 | 3398 |
Kami sekarang perlu memastikan data kami dalam format yang benar dan membaginya menjadi kumpulan data pelatihan dan validasi. Kami telah menggunakan pembagian 60-40 di sini.
# independent variables
ggplot(gather(Breast_Cancer [, 2:ncol(Breast_Cancer)]), aes(value))
geom_histogram(bins = 5, fill = "blue", alpha = 0.6)
## geom_bar: na.rm = FALSE, orientation = NA
## stat_bin: binwidth = NULL, bins = 5, na.rm = FALSE, orientation = NA, pad = FALSE
## position_stack
Melatih model Naive Bayes sangat sederhana, cukup gunakan naive-Bayes yang disertakan dalam paket e1701. Setelah model dilatih, kami menggunakannya untuk memprediksi hasil debat dalam kumpulan data uji.
str(Breast_Cancer)
## tibble [4,024 x 16] (S3: tbl_df/tbl/data.frame)
## $ Age : num [1:4024] 68 50 58 58 47 51 51 40 40 69 ...
## $ Race : chr [1:4024] "White" "White" "White" "White" ...
## $ Marital Status : chr [1:4024] "Married" "Married" "Divorced" "Married" ...
## $ T Stage : chr [1:4024] "T1" "T2" "T3" "T1" ...
## $ N Stage : chr [1:4024] "N1" "N2" "N3" "N1" ...
## $ 6th Stage : chr [1:4024] "IIA" "IIIA" "IIIC" "IIA" ...
## $ differentiate : chr [1:4024] "Poorly differentiated" "Moderately differentiated" "Moderately differentiated" "Poorly differentiated" ...
## $ Grade : chr [1:4024] "3" "2" "2" "3" ...
## $ A Stage : chr [1:4024] "Regional" "Regional" "Regional" "Regional" ...
## $ Tumor Size : num [1:4024] 4 35 63 18 41 20 8 30 103 32 ...
## $ Estrogen Status : chr [1:4024] "Positive" "Positive" "Positive" "Positive" ...
## $ Progesterone Status : chr [1:4024] "Positive" "Positive" "Positive" "Positive" ...
## $ Regional Node Examined: num [1:4024] 24 14 14 2 3 18 11 9 20 21 ...
## $ Reginol Node Positive : num [1:4024] 1 5 7 1 1 2 1 1 18 12 ...
## $ Survival Months : num [1:4024] 60 62 75 84 50 89 54 14 70 92 ...
## $ Status : num [1:4024] 1 1 1 1 1 1 1 0 1 1 ...
Breast_Cancer$Status = as.factor(Breast_Cancer$Status)
#- split data in training and test set.
Index = sample(1:nrow(Breast_Cancer), size = round(0.6*nrow(Breast_Cancer)), replace=FALSE)
train = Breast_Cancer[Index ,]
test = Breast_Cancer[-Index ,]
rm(Index)
NBClassifier = naiveBayes(Status ~., train)
NBClassifier
##
## Naive Bayes Classifier for Discrete Predictors
##
## Call:
## naiveBayes.default(x = X, y = Y, laplace = laplace)
##
## A-priori probabilities:
## Y
## 0 1
## 0.1507871 0.8492129
##
## Conditional probabilities:
## Age
## Y [,1] [,2]
## 0 54.69231 9.711193
## 1 54.05707 8.714391
##
## Race
## Y Black Other White
## 0 0.12637363 0.06318681 0.81043956
## 1 0.06000000 0.08634146 0.85365854
##
## Marital Status
## Y Divorced Married Separated Single Widowed
## 0 0.145604396 0.565934066 0.024725275 0.170329670 0.093406593
## 1 0.128780488 0.665365854 0.008780488 0.140000000 0.057073171
##
## T Stage
## Y T1 T2 T3 T4
## 0 0.26373626 0.46978022 0.19780220 0.06868132
## 1 0.42390244 0.43463415 0.12195122 0.01951220
##
## N Stage
## Y N1 N2 N3
## 0 0.45879121 0.23351648 0.30769231
## 1 0.73317073 0.18536585 0.08146341
##
## 6th Stage
## Y IIA IIB IIIA IIIB IIIC
## 0 0.16758242 0.20329670 0.28571429 0.03571429 0.30769231
## 1 0.36341463 0.29463415 0.24487805 0.01560976 0.08146341
##
## differentiate
## Y Moderately differentiated Poorly differentiated Undifferentiated
## 0 0.48626374 0.41483516 0.03021978
## 1 0.59512195 0.24975610 0.00000000
## differentiate
## Y Well differentiated
## 0 0.06868132
## 1 0.15512195
##
## Grade
## Y 1 2 3 anaplastic
## 0 0.06868132 0.48626374 0.41483516 0.03021978
## 1 0.15512195 0.59512195 0.24975610 0.00000000
##
## A Stage
## Y Distant Regional
## 0 0.05665722 0.94334278
## 1 0.01658537 0.98341463
##
## Tumor Size
## Y [,1] [,2]
## 0 37.43909 24.49669
## 1 29.33805 20.36625
##
## Estrogen Status
## Y Negative Positive
## 0 0.1699717 0.8300283
## 1 0.0502439 0.9497561
##
## Progesterone Status
## Y Negative Positive
## 0 0.3541076 0.6458924
## 1 0.1526829 0.8473171
##
## Regional Node Examined
## Y [,1] [,2]
## 0 14.77337 8.527674
## 1 14.46195 8.132010
##
## Reginol Node Positive
## Y [,1] [,2]
## 0 7.422096 7.671173
## 1 3.569756 4.408194
##
## Survival Months
## Y [,1] [,2]
## 0 45.49858 23.21162
## 1 76.32293 19.53213
# Predict using Naive Bayes
test$predicted = predict(NBClassifier,test)
test$actual = test$Status
Matriks Kebingungan memberi kita pemahaman yang baik tentang seberapa baik kinerja model. Sementara akurasi keseluruhan adalah 72% , sensitivitas dan spesifisitas masing-masing adalah 64% dan 77%. Jika kami membandingkannya dengan hasil yang kami dapatkan dengan menggunakan KNN, KNN keluar sebagai pemenang yang jelas dalam kasus ini.
confusionMatrix(factor(test$predicted),
factor(test$actual))
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 132 148
## 1 130 1200
##
## Accuracy : 0.8273
## 95% CI : (0.808, 0.8455)
## No Information Rate : 0.8373
## P-Value [Acc > NIR] : 0.8670
##
## Kappa : 0.3834
##
## Mcnemar's Test P-Value : 0.3079
##
## Sensitivity : 0.50382
## Specificity : 0.89021
## Pos Pred Value : 0.47143
## Neg Pred Value : 0.90226
## Prevalence : 0.16273
## Detection Rate : 0.08199
## Detection Prevalence : 0.17391
## Balanced Accuracy : 0.69701
##
## 'Positive' Class : 0
##