我正在R中使用“神经网络”包来训练“葡萄酒”数据集的模型。
以下是我到目前为止提出的代码-

library(neuralnet)
library(rattle)
library(rattle.data)

# load 'wine' dataset-
data(wine)

D <- as.data.frame(wine, stringsAsFactors=FALSE)

# replace 'Type' response variable (with values- 1, 2, 3) by 3 dummy variables-
D$wine1 <- 0
D$wine1[D$Type == 1] <- 1

D$wine2 <- 0
D$wine2[D$Type == 2] <- 1

D$wine3 <- 0
D$wine3[D$Type == 3] <- 1

# create formula to be used-
wine_formula <- as.formula(wine1 + wine2 + wine3 ~ Alcohol + Malic + Ash + Alcalinity + Magnesium + Phenols + Flavanoids + Nonflavanoids + Proanthocyanins + Color + Hue + Dilution + Proline)

# split dataset into training and testing datasets-
train_indices <- sample(1:nrow(wine), floor(0.7 * nrow(wine)), replace = F)
training <- D[train_indices, ]
testing <- D[-train_indices, ]

# train neural network model-
wine_nn <- neuralnet(wine_formula, data = training, hidden = c(5, 3), linear.output = FALSE, stepmax = 1e+07)

# make predictions using 'compute()'-
preds <- compute(wine_nn, testing[, 2:14])


# create a final data frame 'results' containing predicted & actual values-
results <- as.data.frame(preds$net.result)
results <- cbind(results, testing$wine1, testing$wine2, testing$wine3)

# rename the data frame-
names(results) <- c("Pred_Wine1", "Pred_Wine2", "Pred_Wine3", "Actual_Wine1", "Actual_Wine2", "Actual_Wine3")


我现在要做的任务是将属性“ Pred_Wine1”,“ Pred_Wine2”和“ Pred_Wine3”中的值转换为1s和0s,以便创建混淆矩阵并测试模型的准确性。

我应该怎么做,因为“ Pred_Wine1”,“ Pred_Wine2”,“ Pred_Wine3”包含的计算值介于0和1之间。

有什么建议么?

谢谢!

最佳答案

就像是:

> head(results)
            Pred_Wine1
1  1.00000000000000000
14 1.00000000000000000
17 1.00000000000000000
21 0.00000001901851182
26 0.21287781596598065
27 1.00000000000000000
                                                         Pred_Wine2
1  0.00000000000000000000000000000000000000000000000000015327712484
14 0.00000000000000000000000000000000000000000000000000009310376079
17 0.00000000000000000000000000000000000000000000000000009435487922
21 0.99999999363562386278658777882810682058334350585937500000000000
26 0.78964805454441211463034733242238871753215789794921875000000000
27 0.00000000000000000000000000000000000000000000000000009310386461
         Pred_Wine3 Actual_Wine1 Actual_Wine2 Actual_Wine3
1   5.291055036e-10            1            0            0
14  1.336129635e-09            1            0            0
17  1.303396935e-09            1            0            0
21 8.968513318e-122            1            0            0
26 1.623066411e-111            1            0            0
27  1.336126866e-09            1            0            0
> class <- apply(results[1:3], 1, which.max)
> results[1:3] <- 0
> head(results)
   Pred_Wine1 Pred_Wine2 Pred_Wine3 Actual_Wine1 Actual_Wine2 Actual_Wine3
1           0          0          0            1            0            0
14          0          0          0            1            0            0
17          0          0          0            1            0            0
21          0          0          0            1            0            0
26          0          0          0            1            0            0
27          0          0          0            1            0            0
> for (r in names(class)) {results[r,class[r]] <- 1}
> head(results)
   Pred_Wine1 Pred_Wine2 Pred_Wine3 Actual_Wine1 Actual_Wine2 Actual_Wine3
1           1          0          0            1            0            0
14          1          0          0            1            0            0
17          1          0          0            1            0            0
21          0          1          0            1            0            0
26          0          1          0            1            0            0
27          1          0          0            1            0            0

08-19 22:25