“Neuralnet”库在R中 – 混淆矩阵

huangapple go评论88阅读模式
英文:

"Neuralnet" Library in R - Confusion Matrix

问题

我运行此代码时收到错误消息“所有参数必须具有相同的长度”。

是否有人可以向我展示如何克服这个错误,因为'test$y'和'pred_label'都具有相同的输入长度?

我目前正在尝试从这些数据中生成混淆矩阵(具有讽刺意味),并且将不胜感激地接受所有帮助。

  1. library(neuralnet)
  2. library(tidyverse)
  3. #MOCK DATA
  4. x1 = rep(1:3, times = 40)
  5. x2 = rep(1:3, times = 40)
  6. x3 = rep(1:3, times = 40)
  7. x4 = rep(1:3, times = 40)
  8. x5 = rep(1:3, times = 40)
  9. y = rep(0:1, times = 60)
  10. y <- as.factor(y)
  11. dat <- data.frame(y, x1, x2, x3, x4, x5)
  12. #SPLIT
  13. set.seed(123)
  14. indexes = createDataPartition(dat$y, p = .85, list = F)
  15. train = dat[indexes, ]
  16. test = dat[-indexes, ]
  17. xtest = test[, -1]
  18. ytest = test[, 1]
  19. #MODEL
  20. NN1 <- neuralnet(y ~., train,
  21. linear.output = FALSE,
  22. stepmax = 1e7)
  23. #ACCURACY TEST
  24. pred <- predict(NN1, test)
  25. labels <- c("0", "1")
  26. pred_label <- data.frame(max.col(pred)) %>%
  27. mutate(pred = labels[max.col.pred.]) %>%
  28. select(2) %>%
  29. unlist()
  30. table(test$y, pred_label)

我目前正在遵循此教程:https://www.datacamp.com/tutorial/neural-network-models-r

英文:

I get an error message "all arguments must have the same length" when I run this code.

Would it be possible for someone to show me how to overcome this error, given `test$y,' and 'pred_label' both have the same input length?

I am currently struggling to generate a confusion matrix (ironically) from this data, and all help would be appreciated.

  1. library(neuralnet)
  2. library(tidyverse)
  3. #MOCK DATA
  4. x1 = rep(1:3, times = 40)
  5. x2 = rep(1:3, times = 40)
  6. x3 = rep(1:3, times = 40)
  7. x4 = rep(1:3, times = 40)
  8. x5 = rep(1:3, times = 40)
  9. y = rep(0:1, times = 60)
  10. y &lt;- as.factor(y)
  11. dat &lt;- data.frame(y, x1, x2, x3, x4, x5)
  12. #SPLIT
  13. set.seed(123)
  14. indexes=createDataPartition(dat$y, p=.85, list = F)
  15. train = dat[indexes, ]
  16. test = dat[-indexes, ]
  17. xtest = test[, -1]
  18. ytest = test[, 1]
  19. #MODEL
  20. NN1 &lt;- neuralnet(y ~., train,
  21. linear.output = FALSE,
  22. stepmax=1e7)
  23. #ACCURACY TEST
  24. pred &lt;- predict(NN1, test)
  25. labels &lt;- c(&quot;0&quot;, &quot;1&quot;)
  26. pred_label &lt;- data.frame(max.col(pred)) %&gt;%
  27. mutate(pred=labels[max.col.pred.]) %&gt;%
  28. select(2) %&gt;%
  29. unlist()
  30. table(test$y, prediction_label)

I am currently following this tutorial. https://www.datacamp.com/tutorial/neural-network-models-r

答案1

得分: 1

你可以使用以下代码进行ROC计算

  1. library(MLeval)
  2. pred <- predict(NN1, newdata = test)
  3. colnames(pred) <- c("No", "Yes")
  4. roc <- data.frame(pred, y = test$y, Group = "ANN") %>%
  5. mutate(y = if_else(y == 0, "No", "Yes"))
  6. ROC <- evalm(roc)
英文:

You can use the following code

  1. library(neuralnet)
  2. library(tidyverse)
  3. library(caret)
  4. #MOCK DATA
  5. x1 = rep(1:3, times = 40)
  6. x2 = rep(1:3, times = 40)
  7. x3 = rep(1:3, times = 40)
  8. x4 = rep(1:3, times = 40)
  9. x5 = rep(1:3, times = 40)
  10. y = rep(0:1, times = 60)
  11. y &lt;- as.factor(y)
  12. dat &lt;- data.frame(y, x1, x2, x3, x4, x5)
  13. #SPLIT
  14. set.seed(123)
  15. indexes=createDataPartition(dat$y, p=.85, list = F)
  16. train = dat[indexes, ]
  17. test = dat[-indexes, ]
  18. #MODEL
  19. NN1 &lt;- neuralnet(y ~., train,
  20. linear.output = FALSE,
  21. stepmax=1e7)
  22. #ACCURACY TEST
  23. pred &lt;- predict(NN1, newdata = test)
  24. colnames(pred) &lt;- c(&quot;0&quot;, &quot;1&quot;)
  25. tab &lt;- table(test$y, colnames(pred)[max.col(pred)])
  26. confusionMatrix(tab)
  27. #&gt; Confusion Matrix and Statistics
  28. #&gt;
  29. #&gt;
  30. #&gt; 0 1
  31. #&gt; 0 3 6
  32. #&gt; 1 6 3
  33. #&gt;
  34. #&gt; Accuracy : 0.3333
  35. #&gt; 95% CI : (0.1334, 0.5901)
  36. #&gt; No Information Rate : 0.5
  37. #&gt; P-Value [Acc &gt; NIR] : 0.9519
  38. #&gt;
  39. #&gt; Kappa : -0.3333
  40. #&gt;
  41. #&gt; Mcnemar&#39;s Test P-Value : 1.0000
  42. #&gt;
  43. #&gt; Sensitivity : 0.3333
  44. #&gt; Specificity : 0.3333
  45. #&gt; Pos Pred Value : 0.3333
  46. #&gt; Neg Pred Value : 0.3333
  47. #&gt; Prevalence : 0.5000
  48. #&gt; Detection Rate : 0.1667
  49. #&gt; Detection Prevalence : 0.5000
  50. #&gt; Balanced Accuracy : 0.3333
  51. #&gt;
  52. #&gt; &#39;Positive&#39; Class : 0

For ROC calculation, you can use the following code

  1. library(MLeval)
  2. pred &lt;- predict(NN1, newdata = test)
  3. colnames(pred) &lt;- c(&quot;No&quot;, &quot;Yes&quot;)
  4. roc &lt;- data.frame(pred, y = test$y, Group = &quot;ANN&quot;) %&gt;%
  5. mutate(y = if_else(y == 0, &quot;No&quot;, &quot;Yes&quot;))
  6. ROC &lt;- evalm(roc)

huangapple
  • 本文由 发表于 2023年7月10日 20:18:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定