R 신경망 분석

Jmnote (토론 | 기여)님의 2020년 5월 9일 (토) 18:46 판

1 개요

R 신경망 분석
R 인공신경망분석

2 2 분류

2.1 neuralnet

2.1.1 예시: 공부시간-합격확률 자료

set.seed(42) # 랜덤값 고정

# 데이터 준비
df = data.frame(
  Hours = c(0.50,0.75,1.00,1.25,1.50,1.75,1.75,2.00,2.25,2.50,2.75,3.00,3.25,3.50,4.00,4.25,4.50,4.75,5.00,5.50),
  Pass = c(0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1)
)

# 데이터 분할
library(caret, quietly=T)
idx = createDataPartition(df$Pass, list=F, p=0.8)
Train = df[ idx,]
Test  = df[-idx,]

library(neuralnet)
model = neuralnet(Pass ~ Hours, df, hidden=2)

options(echo=T)
# 모델 정보
model$result.matrix

# 시각화
plot(model)

# 테스트
pred = predict(model, Test, type="class")
pred = ifelse(pred>0.5, 1, 0)
# 분류표
table(pred, Test$Pass)
# 정분류율(accuracy)
sum(pred==Test$Pass)/nrow(Test)

3 nnet

3.1 예시: iris 자료

df = iris
library(nnet)
model = nnet(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, df, size=2, rang=0.1, decay=0.0005, maxit=200)
summary(model)

options(echo=T)

# 시각화
library(NeuralNetTools)
png(width=800, height=350, pointsize=10)
plotnet(model)

# 자가 테스트
table(df$Species, predict(model, df, type="class"))

3.2 예시: infert 자료

df = infert
library(nnet)
model = nnet(case ~ age + parity + induced + spontaneous, df, size=2, rang=0.1, decay=0.0005, maxit=200)
summary(model)

options(echo=T)

# 시각화
library(NeuralNetTools)
png(width=800, height=350, pointsize=10)
plotnet(model)

# 자가 테스트
table(df$case, predict(model, df))

4 neuralnet

4.1 예시: iris 자료

df = iris
library(neuralnet)
model = neuralnet(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, df, hidden=2)
print(model$result)

# 시각화
plot(model)

4.2 예시: infert 자료

df = infert
library(neuralnet)
model = neuralnet(case ~ age + parity + induced + spontaneous, df, hidden=2)
print(model$result)

# 시각화
plot(model)

5 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}