我正在使用UCI机器学习库中提供的银行数据集,以便尝试在R中构建ANN。
数据的结构如下:
> head(bank_data)
age job marital education default housing loan contact month day_of_week duration campaign pdays
1 1.53301567694 housemaid married basic.4y no no no telephone may mon 0.01047129616 -0.5659151042 0.1954115279
2 1.62897345569 services married high.school unknown no no telephone may mon -0.42149539806 -0.5659151042 0.1954115279
3 -0.29018211937 services married high.school no yes no telephone may mon -0.12451829578 -0.5659151042 0.1954115279
4 -0.00230878311 admin. married basic.6y no no no telephone may mon -0.41378170709 -0.5659151042 0.1954115279
5 1.53301567694 services married high.school no no yes telephone may mon 0.18788618843 -0.5659151042 0.1954115279
6 0.47748011065 services married basic.9y unknown no no telephone may mon -0.23250996934 -0.5659151042 0.1954115279
previous poutcome emp.var.rate cons.price.idx cons.conf.idx euribor3m nr.employed targetVar
1 -0.3494900415 nonexistent 0.6480843991 0.722713697 0.8864358006 0.7124512301 0.3316758805 no
2 -0.3494900415 nonexistent 0.6480843991 0.722713697 0.8864358006 0.7124512301 0.3316758805 no
3 -0.3494900415 nonexistent 0.6480843991 0.722713697 0.8864358006 0.7124512301 0.3316758805 no
4 -0.3494900415 nonexistent 0.6480843991 0.722713697 0.8864358006 0.7124512301 0.3316758805 no
5 -0.3494900415 nonexistent 0.6480843991 0.722713697 0.8864358006 0.7124512301 0.3316758805 no
6 -0.3494900415 nonexistent 0.6480843991 0.722713697 0.8864358006 0.7124512301 0.3316758805 no
以下是我使用的数据和库:
library(dplyr)
library(class)
library(neuralnet)
library(nnet)
library(lubridate)
bank_data <- read.csv("Data/bank-additional-full.csv", header = TRUE, sep = ";")
# Manually create "target var"
bank_data$targetVar <- bank_data$y
bank_data <- select(bank_data, -y)
# Normalize the data
bank_num_vars <- sapply(bank_data, is.numeric)
bank_data[bank_num_vars] <- lapply(bank_data[bank_num_vars], scale)
#SplitData
trainObs <- sample(nrow(bank_data), .6 * nrow(bank_data), replace = FALSE)
valObs <- sample(nrow(bank_data), .2 * nrow(bank_data), replace = FALSE)
testObs <- sample(nrow(bank_data), .2 * nrow(bank_data), replace = FALSE)
# Create the training/va/test datasets
trainDS <- bank_data[trainObs,]
valDS <- bank_data[valObs,]
testDS <- bank_data[testObs,]
# One-hot encoding the variables
trainDS <- cbind(select(trainDS, -targetVar), class.ind(as.factor(trainDS$targetVar)))
valDS <- cbind(select(valDS, -targetVar), class.ind(as.factor(valDS$targetVar)))
testDS <- cbind(select(testDS, -targetVar), class.ind(as.factor(testDS$targetVar)))
n_train <- names(trainDS)
n_val <- names(valDS)
n_test <- names(testDS)
# Create the formula
f <- as.formula(paste("no + yes ~", paste(n_train[!n_train %in% c("no", "yes")], collapse = " + ")))
f_val <- as.formula(paste("no + yes ~", paste(n_val[!n_val %in% c("no","yes")], collapse = " + ")))
f_test <- as.formula(paste("no + yes ~", paste(n_test[!n_test %in% c("no","yes")], collapse = " + ")))
# Run the ANN
nn <- neuralnet(f, data = trainDS, hidden = c(13, 10, 3), act.fct = "logistic", linear.output = FALSE, lifesign = "minimal")
当我尝试运行此代码时,会抛出错误:
nn&lt; - neuralnet(f,data = trainDS,hidden = c(13,10,3),act.fct =&#34; logistic&#34;,linear.output = FALSE,lifesign =&#34 ; minimal&#34;)隐藏:13, 10,3 thresh:0.01 rep:1/1步:神经元出错[[i]]%*% 权重[[i]]:需要数字/复杂矩阵/向量参数
这是什么意思,我该如何解决?