我正在尝试在神经网络中使用神经网络包进行二进制分类。这是我的代码:
n<-names(Train)
f<-as.formula(paste("y~", paste(n[!n %in% c("y","ID")], collapse = " + ")))
nn.train<-Train[,!colnames(Train) %in% c("ID")]
#create nn model
library(neuralnet)
nn <- neuralnet(f, data=nn.train,
linear.output=FALSE,
err.fct = "ce",
threshold = 0.5,
hidden = 5,
stepmax = 1e6,
rep=1)
这就是我的训练数据集nn.train的样子:
'data.frame': 44115 obs. of 51 variables:
$ V1 : num -0.00255 -0.00651 -0.00298 -0.00368 -0.00734 ...
$ V2 : num 0.001711 0.000582 -0.001216 -0.002618 0.005182 ...
$ V3 : num -0.000154 0.000225 0.000271 -0.000469 0.000228 ...
$ V4 : num 0.000653 -0.000188 -0.002388 0.001877 -0.000513 ...
$ V5 : num 0.000499 0.004389 0.000286 -0.005098 -0.002419 ...
$ V6 : num 0.0025538 0.0013363 0.0004831 -0.0000491 0.0025626 ...
$ V7 : num -0.000457 0.005012 0.004569 0.000953 0.001897 ...
$ V8 : num -0.002214 -0.004218 -0.000477 -0.003886 0.007115 ...
$ V9 : num -0.000179 0.0028142 0.0017912 -0.0013334 -0.0000217 ...
$ V10: num 0.000595 -0.002748 0.000372 0.000181 -0.007722 ...
$ V11: num 0.000031 0.002415 0.001556 -0.000771 0.002484 ...
$ V12: num -0.00173 -0.00349 -0.00384 0.0015 -0.00545 ...
$ V13: num -0.00609 0.0078 -0.00239 0.0054 0.00171 ...
$ V14: num 0.000827 0.007988 0.003059 0.001085 0.014218 ...
$ V15: num 0.00339 -0.00373 0.00238 0.00747 -0.00591 ...
$ V16: num -0.000785 -0.003595 0.006264 0.000486 -0.015204 ...
$ V17: num 0.00314 -0.00412 -0.00605 0.00385 -0.00396 ...
$ V18: num -0.00023 0.00642 -0.0015 -0.00221 0.01579 ...
$ V19: num 0.000627 -0.009451 -0.002939 0.005636 -0.006328 ...
$ V20: num -0.001 0.00268 -0.00164 -0.00149 0.00484 ...
$ V21: num 0.000374 -0.004883 -0.004358 0.003519 0.017265 ...
$ V22: num -0.0000281 0.0072532 0.0002763 -0.0035641 -0.0000763 ...
$ V23: num -0.001404 -0.004764 0.007284 -0.000317 -0.019807 ...
$ V24: num 0.00228 -0.007045 0.001688 -0.000884 0.001551 ...
$ V25: num -0.002273 -0.006216 0.000883 -0.001772 -0.001794 ...
$ V26: num 0.000697 -0.001269 -0.001608 -0.000756 0.004126 ...
$ V27: num 0.00325 0.01148 -0.00562 -0.002 -0.00474 ...
$ V28: num 0.00105 -0.00983 0.00207 -0.00383 0.00317 ...
$ V29: num 0.000501 0.005874 -0.008242 -0.002254 -0.003525 ...
$ V30: num -0.000875 0.001314 -0.002309 0.000255 0.009615 ...
$ V31: num 0.003109 -0.000797 -0.000386 0.003241 -0.002977 ...
$ V32: num 0.00487 0.00458 -0.00676 -0.00189 -0.01085 ...
$ V33: num 0.000265 -0.000254 0.018617 -0.001104 -0.004839 ...
$ V34: num 0.0000765 0.005325 -0.0005011 -0.0045697 0.0020402 ...
$ V35: num -0.00295 0.00838 0.00043 -0.00359 -0.00942 ...
$ V36: num 0.00336 -0.0057 -0.01404 0.00186 -0.00528 ...
$ V37: num -0.00321 -0.00268 -0.0026 -0.00674 -0.00399 ...
$ V38: num 0.000084 -0.005826 0.001771 0.004715 0.008845 ...
$ V39: num 0.002567 -0.001225 0.003515 -0.002223 0.000895 ...
$ V40: num 0.00383 -0.00454 0.00152 -0.00243 -0.00931 ...
$ V41: num 0.001465 0.007912 -0.000598 0.001808 -0.003645 ...
$ V42: num 0.000379 0.002077 -0.008668 -0.000406 0.003503 ...
$ V43: num -0.008015 -0.000934 0.004492 -0.002327 -0.003737 ...
$ V44: num 0.00266 -0.005037 0.005414 0.000566 0.004254 ...
$ V45: num -0.00446 0.00343 0.00574 0.00157 0.00301 ...
$ V46: num 0.00276 0.00135 0.00119 -0.00228 -0.0017 ...
$ V47: num 0.008141 -0.006811 0.003744 -0.000869 0.000467 ...
$ V48: num -0.00477 0.00363 0.00326 -0.00227 0.00905 ...
$ V49: num -0.00738 -0.001 -0.00622 -0.00039 0.00217 ...
$ V50: num -0.00594 -0.006 0.0025 0.00364 0.00313 ...
$ y : Factor w/ 2 levels "0","1": 1 1 1 1 2 1 1 1 1 1 ...
这就是我的公式:
y ~ V1 + V2 + V3 + V4 + V5 + V6 + V7 + V8 + V9 + V10 + V11 +
V12 + V13 + V14 + V15 + V16 + V17 + V18 + V19 + V20 + V21 +
V22 + V23 + V24 + V25 + V26 + V27 + V28 + V29 + V30 + V31 +
V32 + V33 + V34 + V35 + V36 + V37 + V38 + V39 + V40 + V41 +
V42 + V43 + V44 + V45 + V46 + V47 + V48 + V49 + V50
我咨询了有关同一错误的其他问题。我没有NA。我所有的自变量都是数字的。我仍然收到此错误:
Error in neurons[[i]] %*% weights[[i]] :
requires numeric/complex matrix/vector arguments
我甚至将Train数据子集化,以删除公式中不存在的变量。我也尝试将训练数据集转换为矩阵。我仍然收到此错误。请帮忙。
更新 如建议的那样,您可以使用它来生成数据集的代表性子集。
Train<- structure(list(V1 = c(-0.0003, -0.00117, -0.00147, -0.00247),
V2 = c(-0.00218, -0.00116, 0.00017, 0.00135), V3 = c(-0.00135,
-0.0008, -0.00013, -0.00077), V4 = c(0.00028, 0.0012, 0.0012,
0.00437), V5 = c(-0.00263, 0.00085, 0.0006, 0.0006), V6 = c(0.00472,
0.00067, 0.00066, 0.0002), V7 = c(0.00348, -0.00161, -0.00043,
0.00172), V8 = c(0.00012, -0.00051, -0.00285, 0.00073), V9 = c(-0.00109,
0.00004, 0.00062, -0.00017), V10 = c(0.00268, -0.00039, 0.00034,
-0.00124), V11 = c(0.00138, -0.00056, -0.00011, -0.00164),
V12 = c(-0.00086, -0.00033, 0.00068, -0.00062), V13 = c(0.00021,
0.00049, -0.00007, 0.00302), V14 = c(-0.0005, 0.00111, 0.00008,
-0.00258), V15 = c(0.00011, -0.00037, 0.00002, -0.00086),
V16 = c(0.00213, -0.00053, -0.0008, -0.00677), V17 = c(-0.00038,
0.00082, 0.00041, 0.00269), V18 = c(-0.00043, -0.00034, 0.00008,
-0.00629), V19 = c(0.00024, -0.00017, 0.00046, -0.00178),
V20 = c(-0.00097, 0.00167, 0.00081, 0.00126), V21 = c(0.00033,
0.00208, 0.00178, -0.00361), V22 = c(0.00105, -0.00038, -0.00078,
0.00586), V23 = c(-0.00093, 0.00223, 0.00274, -0.00478),
V24 = c(0.00044, 0.00217, 0.00147, 0.00279), V25 = c(0.00108,
0.00121, 0.00208, 0.00102), V26 = c(0.00125, 0.00065, 0.00081,
-0.00647), V27 = c(0.00015, -0.00123, -0.00156, -0.001),
V28 = c(-0.00031, 0.00083, 0.00103, 0.00314), V29 = c(-0.00042,
-0.00021, 0.00003, -0.00247), V30 = c(-0.00064, -0.00055,
-0.00022, -0.00027), V31 = c(0.00021, 0.00084, 0.00132, -0.00121
), V32 = c(-0.00005, 0.00061, -0.00007, 0.00343), V33 = c(0.00042,
-0.00197, -0.00085, -0.00322), V34 = c(-0.00014, -0.00192,
-0.00141, -0.00052), V35 = c(0.00048, 0.00036, -0.00115,
0.00176), y = c(1, 0, 0, 0)), .Names = c("V1", "V2", "V3",
"V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "V13",
"V14", "V15", "V16", "V17", "V18", "V19", "V20", "V21", "V22",
"V23", "V24", "V25", "V26", "V27", "V28", "V29", "V30", "V31",
"V32", "V33", "V34", "V35", "y"), row.names = c(NA, 4L), class = "data.frame")