Read all the libraryies you need.
In iris dataset, there are three types of flowers, which means we’re now dealing with a multi-class classification.
xor <- model.matrix(~ Species, Data)
xor[,1] <- ifelse(xor[,2] %in% 0 & xor[,3] %in% 0, 1, 0)
colnames(xor)[1] <- 'Speciessetosa'
head(xor)
## Speciessetosa Speciesversicolor Speciesvirginica
## 1 1 0 0
## 2 1 0 0
## 3 1 0 0
## 4 1 0 0
## 5 1 0 0
## 6 1 0 0
Data <- cbind(xor, Data[,-5])
set.seed(30)
DataFinal <- Data[sample(nrow(Data)),]
Size <- floor(0.7*nrow(Data))
TrainSet <- DataFinal[1:Size, ]
TestSet <- DataFinal[c(Size+1):nrow(Data), ]
We can us neuralnet() to train a NN model. Also, the train() function from caret can help us tune parameters. We can plot the result to see which set of parameters is fit our data the best.
FormulaNN <- Speciessetosa + Speciesversicolor + Speciesvirginica ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width
Model <- train(form=FormulaNN,
data=TrainSet,
method="neuralnet",
### Parameters for layers
tuneGrid = expand.grid(.layer1=c(1:4), .layer2=c(0:4), .layer3=c(0)),
### Parameters for optmization
learningrate = 0.01,
threshold = 0.01,
stepmax = 50000
)
## Warning in nominalTrainWorkflow(x = x, y = y, wts = weights, info =
## trainInfo, : There were missing values in resampled performance measures.
plot(Model)
Build model based on the result from the plot above and plot the model.
BestModel <- neuralnet(formula = FormulaNN,
data = TrainSet,
hidden = c(1,2),
learningrate = 0.01,
threshold = 0.01,
stepmax = 50000
)
plot(BestModel, rep = 'best')
Use compute() to calculate the prediction of the model.
pred <- compute(BestModel, TestSet[,4:7])
pred <- round(pred$net.result)
Result <- ifelse(pred[,1] %in% 1, 'setosa',
ifelse(pred[,2] %in% 1, 'versicolor', 'virginica'))
Species <- ifelse(TestSet[,1] %in% 1, 'setosa',
ifelse(TestSet[,2] %in% 1, 'versicolor', 'virginica'))
table(Result, Species)
## Species
## Result setosa versicolor virginica
## setosa 15 0 0
## versicolor 0 16 0
## virginica 0 0 14