[英]Predictions using neuralnet in R
I am using 'neuralnet' package in R to train a model for 'wine' dataset. 我在R中使用“神经网络”包来训练“葡萄酒”数据集的模型。 Below is the code that I have come up with so far-
以下是我到目前为止提出的代码-
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")
The task that I have now is to convert the values in attributes "Pred_Wine1", "Pred_Wine2" and "Pred_Wine3" to 1s and 0s so that I can create a confusion matrix and test for model accuracy. 我现在要做的任务是将属性“ Pred_Wine1”,“ Pred_Wine2”和“ Pred_Wine3”中的值转换为1s和0s,以便创建混淆矩阵并测试模型的准确性。
How should I go about it because "Pred_Wine1", "Pred_Wine2", "Pred_Wine3" contain calculated values which are in between 0 and 1. 我应该怎么做,因为“ Pred_Wine1”,“ Pred_Wine2”,“ Pred_Wine3”包含的计算值介于0到1之间。
Any suggestions? 有什么建议么?
Thanks! 谢谢!
Something like: 就像是:
> 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
I think you need label encoding here. 我认为您需要在此处进行标签编码。
Let's say your data frame is called df
. 假设您的数据帧称为
df
。 This will convert the values in your features into numeric. 这会将要素中的值转换为数字。 So, if
Pred_Wine1
contains a,b it will convert it to 0,1 or vice-versa. 因此,如果
Pred_Wine1
包含a,b,它将转换为0,1,反之亦然。
Try this: 尝试这个:
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))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.