Artificial Neural Network: ANN
โครงข่ายประสาทเทียม (Artificial Neuron Network : ANN) เป็นศาสตร์แขนงหนึ่งของทางด้านปัญญาประดิษฐ์ (Artifitial Intelligence : AI) มีรูปแบบโครงสร้างและการทำงานของการประมวลผลเหมือนกับสมองของสิ่งมีชีวิตซึ่งมีปรับเปลี่ยนตัวเองต่อการตอบสนองของอินพุตตามกฎของการเรียนรู้ (learning rule) หลังจากที่โครงข่ายได้เรียนรู้สิ่งที่ต้องการแล้ว โครงข่ายนั้นจะสามารถทำงานที่กำหนดไว้ได้โครงข่ายประสาทเทียมได้ถูกพัฒนาคิดค้นจากการทำงานของสมองมนุษย์โดยสมองมนุษย์ประกอบไปด้วยหน่วยประมวลผลเรียกว่า นิวรอน ( เซลล์ประสาท หรือ neuron) จำนวนนิวรอลในสมองมนุษย์มีอยู่ประมาณและมีการเชื่อมต่อกันอย่างมากมาย สมองมนุษย์จึงสามารถกล่าวได้ว่าเป็นคอมพิวเตอร์ที่มีการปรับตัวเอง (adaptive) ไม่เป็นเชิงเส้น (nonlinear) และทำงานแบบขนาน (parallel) ในการดูแลจัดการการทำงานร่วมกันของนิวรอนในสมอง การคำนวณเชิงนิวรอลเป็นการคำนวณที่เลียนแบบมาจากการทำงานของสมองมนุษย์นั่นเอง [1]
library(readxl)
library(tidyverse)
library(neuralnet)
library(factoextra)
library(GGally)
library(psych)
วัตถุประสงค์ของงาน
ข้อมูลที่ศึกศึกษา
ชุดข้อมูลประกอบด้วยการสังเกตจำนวน 5000 ราการ โดยมีตัวแปร 14 ตัว ดังนี้
## tibble [5,000 x 14] (S3: tbl_df/tbl/data.frame)
## $ ID : num [1:5000] 1 2 3 4 5 6 7 8 9 10 ...
## $ Age : num [1:5000] 25 45 39 35 35 37 53 50 35 34 ...
## $ Experience : num [1:5000] 1 19 15 9 8 13 27 24 10 9 ...
## $ Income : num [1:5000] 49 34 11 100 45 29 72 22 81 180 ...
## $ ZIP Code : num [1:5000] 91107 90089 94720 94112 91330 ...
## $ Family : num [1:5000] 4 3 1 1 4 4 2 1 3 1 ...
## $ CCAvg : num [1:5000] 1.6 1.5 1 2.7 1 0.4 1.5 0.3 0.6 8.9 ...
## $ Education : num [1:5000] 1 1 1 2 2 2 2 3 2 3 ...
## $ Mortgage : num [1:5000] 0 0 0 0 0 155 0 0 104 0 ...
## $ Personal Loan : num [1:5000] 0 0 0 0 0 0 0 0 0 1 ...
## $ Securities Account: num [1:5000] 1 1 0 0 0 0 0 0 0 0 ...
## $ CD Account : num [1:5000] 0 0 0 0 0 0 0 0 0 0 ...
## $ Online : num [1:5000] 0 0 0 0 0 1 1 0 1 0 ...
## $ CreditCard : num [1:5000] 0 0 0 0 1 0 0 1 0 0 ...
#Rename Data Frame Columns
dat <- DF %>% select(Age, Experience, Income, CCAvg, Mortgage, Family, Education,
`Securities Account`, `CD Account`, Online, CreditCard,
`Personal Loan`)
names(dat) <- c("Age", "Experience", "Income","CCAvg", "Mortgage", "Family", "Education",
"Securities_Account", "CD_Account", "Online", "CreditCard",
"Personal_Loan")
#Convert data
bool <- function(arg1) {
arg1 [arg1 == 1] <- "Yes"
arg1 [arg1 == 0] <- "No"
arg1 <- as.factor(arg1)
}
Securities_Account <- bool(dat$Securities_Account)
CD_Account <- bool(dat$CD_Account)
Online <- bool(dat$Online)
CreditCard <- bool(dat$CreditCard)
Personal_Loan <- bool(dat$Personal_Loan)
boolEd <- function(arg1) {
arg1 [arg1 == 1] <- "Undergrad"
arg1 [arg1 == 2] <- "Graduate"
arg1 [arg1 == 3] <- "Advanced/Professional"
arg1 <- as.factor(arg1)
}
Education <- boolEd(dat$Education)
dat1 <- dat %>% select(Age, Experience, Income, CCAvg, Mortgage, Family) %>%
add_column(Education, Securities_Account, CD_Account, Online,
CreditCard, Personal_Loan)
str(dat1)
## tibble [5,000 x 12] (S3: tbl_df/tbl/data.frame)
## $ Age : num [1:5000] 25 45 39 35 35 37 53 50 35 34 ...
## $ Experience : num [1:5000] 1 19 15 9 8 13 27 24 10 9 ...
## $ Income : num [1:5000] 49 34 11 100 45 29 72 22 81 180 ...
## $ CCAvg : num [1:5000] 1.6 1.5 1 2.7 1 0.4 1.5 0.3 0.6 8.9 ...
## $ Mortgage : num [1:5000] 0 0 0 0 0 155 0 0 104 0 ...
## $ Family : num [1:5000] 4 3 1 1 4 4 2 1 3 1 ...
## $ Education : Factor w/ 3 levels "Advanced/Professional",..: 3 3 3 2 2 2 2 1 2 1 ...
## $ Securities_Account: Factor w/ 2 levels "No","Yes": 2 2 1 1 1 1 1 1 1 1 ...
## $ CD_Account : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 1 ...
## $ Online : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 2 2 1 2 1 ...
## $ CreditCard : Factor w/ 2 levels "No","Yes": 1 1 1 1 2 1 1 2 1 1 ...
## $ Personal_Loan : Factor w/ 2 levels "No","Yes": 1 1 1 1 1 1 1 1 1 2 ...
dim(dat1)
## [1] 5000 12
ggpairs(dat1[,c(1:12)], aes(color = Personal_Loan, alpha = 0.5),
title = "Scatterplot Matrix of the Bank Personal Loan Data set")
corPlot(dat, min.length = 3)
dat.sc <- dat %>%
mutate(Age = scale(Age, center = T, scale = T),
Experience = scale(Experience, center = T, scale = T),
Income = scale(Income, center = T, scale = T),
CCAvg = scale(CCAvg, center = T, scale = T),
Mortgage = scale(Mortgage, center = T, scale = T),
Family = scale(Family, center = T, scale = T))
dat.sc
## # A tibble: 5,000 x 12
## Age[,1] Experience[,1] Income[,1] CCAvg[,1] Mortgage[,1] Family[,1] Education
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 -1.77 -1.67 -0.538 -0.193 -0.555 1.40 1
## 2 -0.0295 -0.0963 -0.864 -0.251 -0.555 0.526 1
## 3 -0.553 -0.445 -1.36 -0.537 -0.555 -1.22 1
## 4 -0.902 -0.968 0.570 0.436 -0.555 -1.22 2
## 5 -0.902 -1.06 -0.625 -0.537 -0.555 1.40 2
## 6 -0.727 -0.620 -0.973 -0.880 0.968 1.40 2
## 7 0.668 0.601 -0.0385 -0.251 -0.555 -0.345 2
## 8 0.407 0.340 -1.12 -0.937 -0.555 -1.22 3
## 9 -0.902 -0.881 0.157 -0.766 0.467 0.526 2
## 10 -0.989 -0.968 2.31 3.98 -0.555 -1.22 3
## # ... with 4,990 more rows, and 5 more variables: Securities_Account <dbl>,
## # CD_Account <dbl>, Online <dbl>, CreditCard <dbl>, Personal_Loan <dbl>
samplesize = 0.7*nrow(dat.sc)
set.seed(70)
index <- sample(seq_len(nrow(dat.sc)), size = samplesize)
dat.train <- dat.sc[index,]
dat.test <- dat.sc[-index,]
set.seed(123)
# Full Model
NN <- neuralnet(Personal_Loan ~ .,dat.train, hidden = 3, linear.output = F)
plot(NN, rep = 'best')
pc <- dat.sc %>% select(-Personal_Loan) %>% as.matrix() %>% prcomp()
summary(pc)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 1.4265 1.3298 0.9892 0.9537 0.81810 0.58520 0.49281
## Proportion of Variance 0.2786 0.2421 0.1340 0.1245 0.09163 0.04689 0.03325
## Cumulative Proportion 0.2786 0.5207 0.6547 0.7792 0.87086 0.91775 0.95100
## PC8 PC9 PC10 PC11
## Standard deviation 0.46108 0.32017 0.19355 0.07315
## Proportion of Variance 0.02911 0.01403 0.00513 0.00073
## Cumulative Proportion 0.98010 0.99414 0.99927 1.00000
plot(pc, type = "line")
biplot(pc)
dat.pro <- as_tibble(pc$x) %>% add_column(Personal_Loan = dat$Personal_Loan)
ggplot(dat.pro, aes(x = PC1, y = PC2, color = Personal_Loan))+
geom_point()
ggplot(dat.pro, aes(x = PC1, y = 0, color = Personal_Loan))+
geom_point()
fviz_pca(pc)
fviz_pca_var(pc)
pcX <- pc$x[,1:8]
uspc <- cbind(pcX, dat.sc[,12])
samplesize = 0.7*nrow(uspc)
set.seed(70)
indexpc <- sample(seq_len(nrow(uspc)), size = samplesize)
datpc.train <- uspc[index,]
datpc.test <- uspc[-index,]
set.seed(123)
SNN <- neuralnet(Personal_Loan ~ ., datpc.train,
hidden = 3, linear.output = F)
plot(SNN, rep = 'best')
Class_NN_ICs %>%
ggplot(aes(Network, Value, fill = Metric)) +
geom_col(position = 'dodge') +
ggtitle("AIC, BIC, Cross-Entropy Error of the Classification ANNs and Accuracy",
)
Class_NN_ICs
## # A tibble: 8 x 3
## Network Metric Value
## <chr> <chr> <dbl>
## 1 NN AIC 2.59
## 2 NN BIC 8.99
## 3 NN ce Error 17.5
## 4 NN Accuracy 0.981
## 5 SNN AIC 3.55
## 6 SNN BIC 0.984
## 7 SNN ce Error 22.9
## 8 SNN Accuracy 0.984
[1] อำภา สาระศิริ (2559) เทคนิคการเรียนรู้พื้นฐานโครงข่ายประสาทเทียม. แหล่งที่มา: https://www.mut.ac.th/research-detail-92