在R

时间:2018-03-02 18:58:08

标签: r machine-learning neural-network

我在R中使用'neuralnet'软件包来训练'wine'数据集的模型。 下面是我到目前为止提出的代码 -

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之间的计算值。

有什么建议吗?

谢谢!

2 个答案:

答案 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

答案 1 :(得分:0)

我认为你需要标签编码。

假设您的数据框名为df。这会将要素中的值转换为数字。因此,如果Pred_Wine1包含a,则b将其转换为0,1反之亦然。

试试这个:

features <- c("Pred_Wine1", "Pred_Wine2","Pred_Wine3")
for(f in features)
{
    levels <- unique(df[[f]])
    df[[i]] <- as.integer(factor(df[[i]], levels=levels))
}