setwd("C:/R/R-3.2.3/bin/x64")
library(rattle) #
To access the weather dataset and utility commands.
library(magrittr) # For the
%>% and %<>% operators.
building <- TRUE
scoring <- ! building
# A pre-defined value is used
to reset the random seed so that results are repeatable.
crv$seed <- 42
# Load the data.
rPATH <-
Sys.getenv("RSCRIPT_PATH")
rINPUT <- paste0(rPATH ,"/neural-net-script.r.input.csv")
rOUTPUT <- paste0(rPATH
,"/neural-net-script.r.result.csv")
dataset <-
read.csv(file=rINPUT, header=FALSE, sep=",")
# Note the user
selections.
# Build the
training/validate/test datasets.
set.seed(crv$seed)
crs$nobs <- nrow(dataset) #
366 observations
crs$sample <- crs$train
<- sample(nrow(dataset), 0.7*crs$nobs) # 256 observations
crs$validate <-
sample(setdiff(seq_len(nrow(dataset)), crs$train), 0.15*crs$nobs) # 54
observations
crs$test <-
setdiff(setdiff(seq_len(nrow(dataset)), crs$train), crs$validate) # 56
observations
# The following variable
selections have been noted.
crs$input <-
c("V1", "V2", "V3", "V4","V5")
crs$target
<- "V6"
#============================================================
# Neural Network
#============================================================
# Build a neural network model
using the nnet package.
library(nnet, quietly=TRUE)
# Build the NNet model.
set.seed(199)
crs$nnet <-
nnet(as.factor(V6) ~ .,data=dataset[crs$sample,c(crs$input, crs$target)],size=10,
skip=TRUE, MaxNWts=10000, trace=FALSE, maxit=100)
#============================================================
# Score a dataset.
#============================================================
# Obtain probability scores for
the Neural Net model on weather.csv [validate].
#crs$pr <- predict(crs$nnet,
newdata=dataset[crs$validate, c(crs$input)], type="class")
#crs$pr <- predict(crs$nnet,
newdata=dataset[crs$validate, c(crs$input)], type="class")
crs$pr <- predict(crs$nnet,
newdata=dataset, type="class")
write.table(crs$pr,
file=rOUTPUT, row.names=FALSE, col.names = FALSE) |