NeuralNet By r

,

Assignment 4.knwf (138.0 KB)
It is for the Neural Network, and I ned to figure out the code for the R part.

I need help with the RNN part. Thank you so much

Neural Networks

#install.packages(neuralnet)
require(neuralnet)

Create two classification variables

df$yes ← c(df$Y == “1”)
df$no ← c(df$Y == “0”)

Partition the data into training and validation sets

70% of the sample size

smp_size ← floor(0.70 * nrow(df))

set the seed to make the partition reproducible

set.seed(123)
train_ind ← sample(c(1:nrow(df)), size = smp_size)

train ← df[train_ind, ]
validation ← df[-train_ind, ]

Train the ANN

nn ← neuralnet(yes + no ~ X1 + X2 + X3 + X4 + X5,
train, hidden=c(5), linear.output = FALSE)
plot(nn, rep=“best”)
summary(nn)

Predict Y for the validation dataset

predict ← compute(nn, validation[,1:5])$net.result

Extract predicted values on validation set and construct a confusion matrix

net.prediction ← c(“1”,“0”)[apply(predict,1,which.max)]

predict.table ← table(validation$Y, net.prediction)

predict.table

Alternatively see more diagnostics with a confusion matrix function

#require(caret)
#require(e1071)
#confusionMatrix(predict.table)

#dim(validation)

predict.df ← as.data.frame(predict)
names(predict.df) ← c(“p_1”, “p_0”)
p_validation ← cbind(validation, predict.df, net.prediction)

knime.out ← p_validation

Hi @JustinZMY and welcome to the forum.

Since your question is about an assignment, I doubt anyone will be providing a direct answer for you. :sweat_smile: But we can give hints…

Could you be more specific about the part you need help with? What errors do you see, and where is the process failing?