머신러닝, 딥러닝에 대해 배워보고 싶어서 이것저것 찾아보다가 괜찮은 자료가 있어 도전해봤다.



목표


R의 nnet 패키지를 이용하여


1. 전통적 신경망에 대해 이해

2. 예측 모형을 구축

3. 신경망의 시각화

4. 변수 중요도와 민감도를 분석


하는 것이 목표이다.


사용할 자료는 150개 붓꽃 데이터이고 훈련 데이터 100개, 검증 데이터 50개로 분리하여 모형의 결과와 정확도를 확인할 것이다.



=========================================R=========================================


rm(list=ls())


##================================================================

## 01. 환경설정 및 데이터 불러오기

##================================================================

library("nnet")

data(iris)


# nnet 종속변수 입력을 위한 종속변수 변환

species.ind <- class.ind(iris$Species)

iris <- cbind(iris, species.ind)


##================================================================

## 02. 훈련 및 검증 데이터 분리

##================================================================

train.idx  <-  sample(1:150,100)


iris.train.df <- iris[train.idx, ]

iris.test.df <- iris[-train.idx, ]


##================================================================

## 03. 신경망 모형 적합

##================================================================


iris.nn  <-  nnet(x=iris.train.df[,c(1:4)], y=iris.train.df[,c(6:8)], size=10, softmax=TRUE)


##================================================================

## 04. 검증 데이터 예측 및 모형 평가

##================================================================

# 훈련데이터

iris.pred <- predict(iris.nn, iris.train.df[,c(1:4)], type="class")

table(iris.pred, iris.train.df$Species)

# 검증데이터

iris.pred <- predict(iris.nn, iris.test.df[,c(1:4)], type="class")

table(iris.pred, iris.test.df$Species)


##================================================================

## 05. 신경망 시각화

##================================================================

# 시각화 R 코드 함수 다운로드

library("devtools")

source_url('https://gist.githubusercontent.com/fawda123/7471137/raw/466c1474d0a505ff044412703516c34f1a4684a5/nnet_plot_update.r')


# 신경망 모형 시각화

library("reshape2")

plot.nnet(iris.nn)


##================================================================

## 06. 신경망 이해

##================================================================

library("NeuralNetTools")


garson(iris.nn)

lekprofile(iris.nn)


=========================================R=========================================



=====================================시각화 결과물=====================================

fig 1. 신경망 모형 시각화


fig 2. 민감도 분석



=====================================시각화 결과물=====================================




아쉽게도 Garson function을 이용한 변수 중요도 시각화 자료는 

Error in garson.default(wts_in, x_names, y_names, ...) :   Garson only applies to neural networks with one output node

라는 오류와 함께 안 나온다.



위의 결과를 해석해보자면 일단 fig1에서 Petal.Width, Petal.Length가 가중치가 크고 노드는 H4, H3이 가중치가 크다는 걸 알 수 있다.


민감도 또한 마찬가지로 모든 그룹이 Petal.~에 많이 나타났고 versicolor에서도 많이 나타났다는 것을 알 수 있다.


그만큼 Petal.~, wersicolor가 초기값에 영향을 많이 받는 지표라는 뜻이다.




분석을 하기에는 아직 지식이 많이 부족하다는 것을 느끼고 앞으로 더 열심히 공부해야겠다는 생각을 하며 글을 마친다.




ref.

software carpentry / xwMOOC딥러닝

- https://statkclee.github.io/deep-learning/r-nnet.html


+ Recent posts