R中的neuralnet包出错

R中的neuralnet包出错,r,neural-network,R,Neural Network,我正在尝试使用R中针对“iris”数据集提供的“neuralnet”包实现一个简单的多层前馈神经网络 我使用的代码如下- library(neuralnet) data(iris) D <- data.frame(iris, stringsAsFactors=TRUE) # create formula- f <- as.formula(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width) # co

我正在尝试使用R中针对“iris”数据集提供的“neuralnet”包实现一个简单的多层前馈神经网络

我使用的代码如下-

library(neuralnet)
data(iris)

D <- data.frame(iris, stringsAsFactors=TRUE)

# create formula-
f <- as.formula(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width)

# convert qualitative variables to dummy (binary) variables-
m <- model.matrix(f, data = D)

# create neural network-
iris_nn <- neuralnet(f, data = m, hidden = 4, learningrate = 0.3)
str(D)'data.frame':150 obs。共5个变量:$萼片。长度:num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ 萼片宽度:num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1…$花瓣长度:num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.4 1.5…$花瓣宽度:数值0.20.20.20.20.20.20.40.40.30.20.20.1…$物种:系数w/3级“刚毛”、“花色”

我已经成功地用“nnet”编码了这个。张贴我的代码以供参考-

data(iris)
library(nnet)

# create formula-
f <- as.formula(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width)

# create a NN with hidden layer having 4 neurons/node and
# maximum number of iterations = 3
iris_nn <- nnet(f, data = iris, size = 4, maxit = 3)

# create a test data-
new_obs <- data.frame(Sepal.Length = 5.5, Sepal.Width = 3.1, Petal.Length = 1.4, Petal.Width = 0.4)

# make prediction-
predict(iris_nn, new_obs)   # gives percentage of which class it may belong
predict(iris_nn, new_obs, type = "class")   # gives the class instead of percentages of which 'class' this data type may belong to


# create a 'confusion matrix' to measure accuracy of model-
# rows are actual values and columns are predicted values-
# table(iris$Species, predict(iris_nn, iris[, 1:4], type = "class"))
cat("\n\nConfusion Matrix for # of iters = 3\n")
print(table(iris$Species, predict(iris_nn, iris[, 1:4], type = "class")))
cat("\n\n")

rm(iris_nn)

# setting 'maxit' to 1000, makes the model coverge-
iris_nn <- nnet(f, data = iris, size = 4, maxit = 1000)

# create a new confusion matrix to check model accuracy again-
cat("\n\nConfusion Matrix for # of iters = 1000\n")
print(table(iris$Species, predict(iris_nn, iris[, 1:4], type = "class")))
# table(iris$Species, predict(iris_nn, iris[, 1:4], type = "class"))


# to plot 'iris_nn' trained NN-
# library("NeuralNetTools")
# plotnet(iris_nn)
数据(iris)
图书馆(nnet)
#创建公式-

f不知道NN是如何运行的,最好的运行方式是什么。我也不太了解iris数据集

只是指出为什么它没有运行-专栏
Species

str(d)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
物种
是一个因子NN不考虑因子

转换为虚拟变量-

d$set <-0
d$set[d$Species == "setosa"] <- 1

d$versi <-0 
d$versi[d$Species == "versicolor"] <- 1



f <- as.formula(set+versi ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width)

iris_nn <- neuralnet(f, data = d, hidden = 4, learningrate = 0.3)

可能重复@SamFlynn我已经编辑了我的帖子,加入了矩阵“m”。但现在,我试图预测的“物种”变量消失了!因此,最后一行代码给出的错误是找不到“物种”!有什么想法吗?我也试过了,没办法弄明白,但总是出错。在问题中添加
str(d)
的输出。我所做的是手动将所有阶乘列更改为伪变量,这很有效。属性的规范化会有帮助吗?谢谢代码!它起作用了!但是我仍然有两个问题-1.)如何使用“隐藏”参数(2.)对于“neuralnet”,我应该如何使用“compute()”函数。我知道你对NNs了解不多。“只是问问其他人。”阿伦编辑了一下。也可以通过接受问题来结束问题。我的两个问题仍然存在unanswered@Arun隐藏并使用“neuralnet”包的“compute()”函数,因为“predict()”在“neuralnet”的对象上不起作用
str(d)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
d$set <-0
d$set[d$Species == "setosa"] <- 1

d$versi <-0 
d$versi[d$Species == "versicolor"] <- 1



f <- as.formula(set+versi ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width)

iris_nn <- neuralnet(f, data = d, hidden = 4, learningrate = 0.3)
library(neuralnet)
library(caret) #has the confusionmatrix function in it
#for some reason compute needs to be called like that, calling normally was producing some error
nnans <- neuralnet::compute(NN, test)
confusionMatrix(nnans, test_labels))