"iris"データセットのRで利用可能な "neuralnet"パッケージを使用して、単純なマルチレイヤフィードフォワードニューラルネットワークを実装しようとしています。neuralnetパッケージのエラーが発生しました。
私が使用していたコードは、私は、時間
1のこの時点では二つの質問を持ってfollows-
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)
ようです)どのように私は、「隠された」パラメータを使用していますか?マニュアルページによると、そのsaying-
隠さ:各レイヤー
に隠されたニューロンの数(頂点)を指定する整数のベクトルがどのように私は整数のベクトルを供給する必要がありますか?各層に4つのニューロン/パーセプトロンからなる1つの隠れたレイヤーを持っていたいと思ったら、あるいは各レイヤーに5つのニューロンからなる3つの隠れたレイヤーを持っていたいとします。私が削除した場合、オブジェクトの種の
が見つかりません:
2)コードの最後の行は、evalの中にエラー -
エラー(predvars、データ、ENV)私を与えます"hidden"パラメータでは、このエラーは引き続き発生します。
私はここで間違っていますか?
編集:ライン -
m <- model.matrix(f, data = D)
を追加した後の行列「m」は、もはや私は予測しようとしている「種」変数/属性が含まれていません。
出力
str(D)
STR(D) 'data.frame':150台のOBS。 5つの変数のうち、$ 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 ... $種:3レベルの "setosa"、 "versicolor" 1 1 1 1 1 1 1 1 1 1 ...
これを "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)
ありがとう!!
可能な重複:https://stackoverflow.com/questions/17457028/working-with-neuralnet([初めてRにneuralnetでの作業を取得は、「数値/複素数行列/ベクトル引数が必要です」] -in-r-for-the-first-time-requires-numeric-complex-ma) – SamFlynn
@SamFlynn私は行列 'm'を含むように自分の投稿を編集しました。しかし、今私は "種"を予測しようとしている変数はなくなっています!したがって、コードの最後の行は "Species"が見つかりませんでした。何か案は? – Arun
私はあまりにもそれを試して、それが何かエラーを取得し続けて把握できませんでした。その質問に 'str(d)'の出力を加えてください。私がしたのは、すべての階乗列をダミー変数に手動で変更していたことです。 – SamFlynn