[R] What to do when an error occurs in the neuralnet function

2018/11/15

neuralnet function

The neuralnet function is one of the neural network libraries in R.The advantage of this library is that the created learner can be visualized directly with plot (), and the calculation graph and weight / bias values ​​can be seen at a glance.
The following is a memo that also serves as a memorandum because I stumbled in various ways when performing multi-class classification with the iris dataset as a practice.When actually trying out neural networks in R, it is recommended to choose another easy-to-use library.

Error and handling when executing neuralnet

Qualitative variable input error

In many R functions, if you define a qualitative variable as a factor type, it will be automatically treated as a dummy variable (python does not have a factor type, pandas get_dummies function etc. divides qualitative variables, and 0 or 1 is which Create a column to indicate whether it belongs). Since the neuralnet function can handle only quantitative variables, it could not correctly recognize qualitative variables such as "Species" of iris, and an error occurred.To convert a qualitative variable to a dummy variable, run the following code.

library(caret) 
tmp <- dummyVars(~.,data=train) 
dummy <- as.data.frame(predict(tmp, train))

About formula at the time of multi-class classification

In the nnet function etc., it can be written as follows.Here, for Species, other variables are specified as explanatory variables ("".Means other than the objective variable).

library(nnet) 
nn1 <- nnet(formula = Species ~ ., size=5, data=train)

Since the explanatory variable has been converted to a dummy variable, formula describes all the classes to be classified by connecting them with "+".

library("neuralnet") f = Species.setosa + Species.versicolor + Species.virginica       
~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width 
nn2 <- neuralnet(formula = f, data = dummy)

Result

I was able to classify iris safely.