# neuralnet : iris dataset : 입력값을 정규화하지 않고 결과 출력
library(neuralnet)
iris
set.seed(1)
train<-sample(1:nrow(iris),nrow(iris)*0.7)
test <- seq(nrow(iris))[-train]
iristrain <-iris[train, ]
iristest <-iris[test, ]
dim(iristrain); dim(iristest)
#꽃의 종류를 범주형으로 하기 위해 양면화(T,F)
iristrain <- cbind(iristrain, iristrain$Species=='setosa') #칼럼 3개 추가
iristrain <- cbind(iristrain, iristrain$Species=='versicolor')
iristrain <- cbind(iristrain, iristrain$Species=='virginica')
names(iristrain)[6:8] <-c('setosa','virginica','versicolor')
head(iristrain, 3)
#nn model
nn <- neuralnet(setosa + virginica + versicolor ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, data = iristrain,hidden = 3)
plot(nn)
#예측
iristest #칼럼을 추가하지 않음
comp <- compute(nn, iristest[-5])
names(comp)
pred.weight <-comp$net.result
pred.weight
idx <- apply(pred.weight, 1, which.max) #which.max 가장 큰 값
idx
#c('setosa','virginica','versicolor')[3] #1. seto 2. virg 3. vers
pred <-c('setosa','virginica','versicolor')[idx] # 숫자값으로 안보이고 꽃의 종류로 보이게 하기
pred
table(pred,iristest$Species)
(15+0+3)/nrow(iristest)
댓글 영역