"홍채"데이터 세트의 R에서 사용할 수있는 "neuralnet"패키지를 사용하여 간단한 다층 피드 순방향 신경망을 구현하려고합니다. 나는 시간 -neuralnet 패키지에 대한 오류가 있음
1)의이 시점에서 두 가지 질문이
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)
follows-으로 내가 사용하고
코드는 어떻게이 "숨겨진"매개 변수를 사용합니까? 매뉴얼 페이지에 따르면, 자사의 제 말
숨겨진 : 각 계층
에 숨겨진 뉴런의 수 (정점)를 지정하는 정수의 벡터가 어떻게 정수의 벡터를 제공해야합니까? 각 레이어에 4 개의 뉴런/퍼셉트론으로 된 숨겨진 레이어가 1 개 있고 싶었거나, 각 레이어에 5 개의 뉴런으로 구성된 3 개의 숨겨진 레이어가 있기를 원한다면 말하십시오.
2) 코드의 마지막 줄은 나에게 평가에
를 찾을 수 없음 "숨겨진"매개 변수,이 오류가 계속 유지됩니다.오류를 오류 -를 제공는 (predvars, 데이터, ENV) : 나는를 제거하면 객체 '종'은
내가 뭘 잘못하고 있니?
편집 : 라인 -
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 ... $ 종 (Species) : 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-get-requires-numeric-complex-ma) – SamFlynn
@SamFlynn 매트릭스 'm'을 포함하도록 내 게시물을 편집했습니다. 그러나 이제는 "종"을 예측하려고하는 변수가 사라졌습니다! 그러므로 마지막 코드 줄은 "Species"를 찾을 수 없다는 오류를줍니다! 어떤 아이디어? – Arun
나도 그걸 시도했는데, 어떤 오류가 계속 발생했는지 파악할 수 없었다. 질문에'str (d)'의 출력을 추가하십시오. 내가 한 것은 더미 변수에 대한 모든 계승 열을 수동으로 변경하는 것이 었습니다. – SamFlynn