19. ANN (Artificial Neural Network)

 

 1) 논리 회로 처리

 - 라이브러리 read

install.packages("nnet")
library(nnet)

 

 - 학습 모델 작성

input <- matrix(c(0,0,1,1,0,1,0,1), ncol=2)
input
# [,1] [,2]
# [1,]    0    0
# [2,]    0    1
# [3,]    1    0
# [4,]    1    1
output <- matrix(c(0,0,0,1)) # and
output
# [,1]
# [1,]    0
# [2,]    0
# [3,]    0
# [4,]    1
output <- matrix(c(0,1,1,1)) # or
output <- matrix(c(0,1,1,0)) # xor - size가 1로는 비선형이라 안됨.

ann <- nnet(input, output, maxit = 10000, size = 3, decay = 0.001)
# maxit : 학습 횟수, size: node수, decay: 학습률
ann

 - 결과 확인

result <- predict(ann, input) # 예측모델 생성
result
#             [,1]
# [1,] 0.002724474
# [2,] 0.070330761
# [3,] 0.070331671
# [4,] 0.897269791
ifelse(result>0.5, 1, 0) # 0.5 이상일경우 1 아닐경우 0으로 판단
#      [,1]
# [1,]    0
# [2,]    0
# [3,]    0
# [4,]    1

df <- data.frame(
  x1 = c(1:6),
  x2 = c(6:1),
  y  = factor(c('n','n','n','y','y','y'))
)
df
# x1 x2 y
# 1  1  6 n
# 2  2  5 n
# 3  3  4 n
# 4  4  3 y
# 5  5  2 y
# 6  6  1 y
str(df)
model_net1 <- nnet(y ~ ., df, size = 1)
model_net1
# a 2-1-1 network with 5 weights
# inputs: x1 x2 
# output(s): y 
# options were - entropy fitting
summary(model_net1)

model_net2 <- nnet(y ~ ., df, size = 2)
model_net2
# a 2-2-1 network with 9 weights
# inputs: x1 x2 
# output(s): y 
# options were - entropy fitting
summary(model_net2)
install.packages("devtools")
library(devtools)
source_url('https://gist.githubusercontent.com/fawda123/7471137/raw/466c1474d0a505ff044412703516c34f1a4684a5/nnet_plot_update.r')
par(mar=c(1,1,1,1))
plot.nnet(summary(model_net1))

plot.nnet(summary(model_net2))

# predict
model_net2$fitted.values
predict(model_net2, df)

pred <- predict(model_net2, df, type='class')
pred
table(pred, df$y)
# pred n y
# n 3 0
# y 0 3

# iris dataset 사용
data(iris)
set.seed(123)
idx <- sample(1:nrow(iris), nrow(iris)*0.7)
train <- iris[idx, ]
test <- iris[-idx, ]

# node 1개 사용한 경우
model_iris1 = nnet::nnet(Species ~ ., train, size = 1)
model_iris1
# a 4-1-3 network with 11 weights
# inputs: Sepal.Length Sepal.Width Petal.Length Petal.Width 
# output(s): Species 
# options were - softmax modelling 
summary(model_iris1)  # weights 11로 weights가 클수록 정교한 분석가능
plot.nnet(summary(model_iris1))

# node 3개 사용한 경우
model_iris3 = nnet::nnet(Species ~ ., train, size = 3)
summary(model_iris3)
plot.nnet(summary(model_iris3))

# 분류평가
pred1 <- predict(model_iris1, test, type="class")
t1 <- table(pred1, test$Species)
sum(diag(t1))/nrow(test) # 0.9777778

pred3 <- predict(model_iris3, test, type="class")
t3 <- table(pred3, test$Species)
sum(diag(t3))/nrow(test)

'BACK END > R' 카테고리의 다른 글

[R] R 정리 21 - 계층적 군집분석  (0) 2021.02.04
[R] R 정리 20 - MLP(deep learning)  (0) 2021.02.04
[R] R 정리 18 - svm, knn  (0) 2021.02.02
[R] R 정리 17 - Naive Bayes  (0) 2021.02.02
[R] R 정리 16 - Random Forest  (0) 2021.02.02

+ Recent posts