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

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

"Neuralnet" Library in R - Confusion Matrix

问题

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

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

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

library(neuralnet)
library(tidyverse)

#MOCK DATA

x1 = rep(1:3, times = 40)
x2 = rep(1:3, times = 40)
x3 = rep(1:3, times = 40)
x4 = rep(1:3, times = 40)
x5 = rep(1:3, times = 40)

y = rep(0:1, times = 60)
y <- as.factor(y)

dat <- data.frame(y, x1, x2, x3, x4, x5)

#SPLIT

set.seed(123)

indexes = createDataPartition(dat$y, p = .85, list = F)
train = dat[indexes, ]
test = dat[-indexes, ]

xtest = test[, -1]
ytest = test[, 1]

#MODEL

NN1 <- neuralnet(y ~., train,
                 linear.output = FALSE,
                 stepmax = 1e7)

#ACCURACY TEST

pred <- predict(NN1, test)
labels <- c("0", "1")
pred_label <- data.frame(max.col(pred)) %>%
  mutate(pred = labels[max.col.pred.]) %>%
  select(2) %>%
  unlist()

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.

library(neuralnet)
library(tidyverse)


#MOCK DATA

x1 = rep(1:3, times = 40)
x2 = rep(1:3, times = 40)
x3 = rep(1:3, times = 40)
x4 = rep(1:3, times = 40)
x5 = rep(1:3, times = 40)

y = rep(0:1, times = 60)
y &lt;- as.factor(y)

dat &lt;- data.frame(y, x1, x2, x3, x4, x5)

#SPLIT

set.seed(123)

indexes=createDataPartition(dat$y, p=.85, list = F)
train = dat[indexes, ]
test = dat[-indexes, ] 

xtest = test[, -1]
ytest = test[, 1]

#MODEL

NN1 &lt;- neuralnet(y ~., train,
                 linear.output = FALSE,
                 stepmax=1e7)


#ACCURACY TEST

pred &lt;- predict(NN1, test)
labels &lt;- c(&quot;0&quot;, &quot;1&quot;)
pred_label &lt;- data.frame(max.col(pred)) %&gt;%     
  mutate(pred=labels[max.col.pred.]) %&gt;%
  select(2) %&gt;%
  unlist()

table(test$y, prediction_label)

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

答案1

得分: 1

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

library(MLeval)

pred <- predict(NN1, newdata = test)
colnames(pred) <- c("No", "Yes")

roc <- data.frame(pred, y = test$y, Group = "ANN") %>%
  mutate(y = if_else(y == 0, "No", "Yes"))
  
ROC <- evalm(roc)
英文:

You can use the following code

library(neuralnet)
library(tidyverse)
library(caret)

#MOCK DATA
x1 = rep(1:3, times = 40)
x2 = rep(1:3, times = 40)
x3 = rep(1:3, times = 40)
x4 = rep(1:3, times = 40)
x5 = rep(1:3, times = 40)

y = rep(0:1, times = 60)
y &lt;- as.factor(y)

dat &lt;- data.frame(y, x1, x2, x3, x4, x5)

#SPLIT
set.seed(123)
indexes=createDataPartition(dat$y, p=.85, list = F)
train = dat[indexes, ]
test = dat[-indexes, ] 

#MODEL
NN1 &lt;- neuralnet(y ~., train,
                 linear.output = FALSE,
                 stepmax=1e7)


#ACCURACY TEST
pred &lt;- predict(NN1, newdata = test)
colnames(pred) &lt;- c(&quot;0&quot;, &quot;1&quot;)
tab &lt;- table(test$y, colnames(pred)[max.col(pred)])
confusionMatrix(tab)

#&gt; Confusion Matrix and Statistics
#&gt; 
#&gt;    
#&gt;     0 1
#&gt;   0 3 6
#&gt;   1 6 3
#&gt;                                           
#&gt;                Accuracy : 0.3333          
#&gt;                  95% CI : (0.1334, 0.5901)
#&gt;     No Information Rate : 0.5             
#&gt;     P-Value [Acc &gt; NIR] : 0.9519          
#&gt;                                           
#&gt;                   Kappa : -0.3333         
#&gt;                                           
#&gt;  Mcnemar&#39;s Test P-Value : 1.0000          
#&gt;                                           
#&gt;             Sensitivity : 0.3333          
#&gt;             Specificity : 0.3333          
#&gt;          Pos Pred Value : 0.3333          
#&gt;          Neg Pred Value : 0.3333          
#&gt;              Prevalence : 0.5000          
#&gt;          Detection Rate : 0.1667          
#&gt;    Detection Prevalence : 0.5000          
#&gt;       Balanced Accuracy : 0.3333          
#&gt;                                           
#&gt;        &#39;Positive&#39; Class : 0 

For ROC calculation, you can use the following code

library(MLeval)

pred &lt;- predict(NN1, newdata = test)
colnames(pred) &lt;- c(&quot;No&quot;, &quot;Yes&quot;)

roc &lt;- data.frame(pred, y = test$y, Group = &quot;ANN&quot;) %&gt;% 
  mutate(y = if_else(y == 0, &quot;No&quot;, &quot;Yes&quot;))
  
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:

确定