"R 신경망 분석"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 21개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;R 신경망 분석
;R 신경망 분석
;R 인공신경망분석
;R 인공신경망 분석


==nnet==
==2 분류==
===예시: iris 자료===
===예시: 공부시간-합격확률 자료 (neuralnet)===  
<source lang='r' run>
<syntaxhighlight lang='r' run>
df = iris
set.seed(42) # 랜덤값 고정
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)
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)
options(echo=T)
# 모델 정보
model$result.matrix


# 시각화
# 시각화
library(NeuralNetTools)
plot(model)
png(width=800, height=350, pointsize=10)
 
plotnet(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)
</syntaxhighlight>
 
===예시: infert 자료 (neuralnet)===
<syntaxhighlight lang='r' run>
set.seed(42) # 랜덤값 고정
 
df = infert
# 데이터 분할
library(caret, quietly=T)
idx = createDataPartition(df$case, list=F, p=0.8)
Train = df[ idx,]
Test  = df[-idx,]
 
library(neuralnet)
model = neuralnet(case ~ age + parity + induced + spontaneous, Train, hidden=2)
 
options(echo=T)
# 모델 정보
model$result.matrix
 
# 모델 시각화
plot(model)


# 자가 테스트
# 테스트
table(df$Species, predict(model, df, type="class"))
pred = predict(model, Test)
</source>
pred_class = ifelse(pred>0.5, 1, 0)
# 분류표
table(pred_class, Test$case)
# 정분류율(accuracy)
sum(pred_class==Test$case)/nrow(Test)
# ROC 곡선
library(Epi)
ROC(form=case~pred, data=Test, plot="ROC")
</syntaxhighlight>


===예시: ifert 자료===
===예시: infert 자료 (nnet)===
<source lang='r' run>
<syntaxhighlight lang='r' run>
df = infert
df = infert
library(nnet)
library(nnet)
38번째 줄: 88번째 줄:
# 자가 테스트
# 자가 테스트
table(df$case, predict(model, df))
table(df$case, predict(model, df))
</source>
</syntaxhighlight>
 
==3 분류==
==neuralnet==
===예시: iris 자료 (neuralnet)===
===예시: iris 자료===
<syntaxhighlight lang='r' run>
<source lang='r' run>
df = iris
df = iris
library(neuralnet)
library(neuralnet)
model = neuralnet(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, df, hidden=2)
model = neuralnet(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, df, hidden=2)
print(model$result)
# 시각화
plot(model)
</syntaxhighlight>
===예시: iris 자료 (nnet)===
<syntaxhighlight lang='r' run>
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)
summary(model)
options(echo=T)


# 시각화
# 시각화
plot(model)
library(NeuralNetTools)
</source>
png(width=800, height=350, pointsize=10)
plotnet(model)
 
# 자가 테스트
table(df$Species, predict(model, df, type="class"))
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[R 다항로지스틱회귀분석]]
* [[R 다항로지스틱회귀분석]]
* [[R 이진분류 모델 비교 구현]]


[[분류: R nnet]]
[[분류: R nnet]]
[[분류:R 데이터 분석]]
[[분류:R 데이터 분석]]
[[분류: 인공신경망]]

2021년 5월 27일 (목) 20:00 기준 최신판

1 개요[ | ]

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

2 2 분류[ | ]

2.1 예시: 공부시간-합격확률 자료 (neuralnet)[ | ]

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)

2.2 예시: infert 자료 (neuralnet)[ | ]

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

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

library(neuralnet)
model = neuralnet(case ~ age + parity + induced + spontaneous, Train, hidden=2)

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

# 모델 시각화
plot(model)

# 테스트
pred = predict(model, Test)
pred_class = ifelse(pred>0.5, 1, 0)
# 분류표
table(pred_class, Test$case)
# 정분류율(accuracy)
sum(pred_class==Test$case)/nrow(Test)
# ROC 곡선
library(Epi)
ROC(form=case~pred, data=Test, plot="ROC")

2.3 예시: infert 자료 (nnet)[ | ]

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))

3 3 분류[ | ]

3.1 예시: iris 자료 (neuralnet)[ | ]

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

# 시각화
plot(model)

3.2 예시: iris 자료 (nnet)[ | ]

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"))

4 같이 보기[ | ]

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