Osm
Osm

Reputation: 81

How to use deepnet for classification in R

When i use code from example:

library(deepnet)
Var1 <- c(rnorm(50, 1, 0.5), rnorm(50, -0.6, 0.2))
Var2 <- c(rnorm(50, -0.8, 0.2), rnorm(50, 2, 1))
x <- matrix(c(Var1, Var2), nrow = 100, ncol = 2)
y <- c(rep(1, 50), rep(0, 50))
nn <- dbn.dnn.train(x, y, hidden = c(5))

it works. But when i use this code:

Var1 <- c(rnorm(50, 1, 0.5), rnorm(50, -0.6, 0.2))
Var2 <- c(rnorm(50, -0.8, 0.2), rnorm(50, 2, 1))
x <- matrix(c(Var1, Var2), nrow = 100, ncol = 2)
**y <- c(rep("1", 50), rep("0", 50))**
nn <- dbn.dnn.train(x, y, hidden = c(5))

i receive error:

Error in batch_y - nn$post[[i]] : non-numeric argument to binary operator

How can i use deepnet package for classification problem?

Upvotes: 0

Views: 2467

Answers (1)

user6376316
user6376316

Reputation:

y1 <- c(rep("1", 50), rep("0", 50))

lead you to character vector which is not acceptable by the package. so that you get error

class(y)
#[1] "character"

The right y should be numeric as follows

y <- c(rep(1, 50), rep(0, 50))
class(y)
#[1] "numeric"

if you see inside your y , you can find that you have 1 or 0 which is a binary values for classification

> table(y)
#y
# 0  1 
#50 50 

If you want to train as it is mentioned in the manual, you can do the following to train and predict a test set

Var1 <- c(rnorm(50, 1, 0.5), rnorm(50, -0.6, 0.2))
Var2 <- c(rnorm(50, -0.8, 0.2), rnorm(50, 2, 1))
x <- matrix(c(Var1, Var2), nrow = 100, ncol = 2)
y <- c(rep(1, 50), rep(0, 50))

If you now look at your x and y by str just simply write str(x) or str(y) you can see that they are numeric (to make sure, you can check them by class(x) and class(y).

After having your X and y , then you can build your model

dnn <- dbn.dnn.train(x, y, hidden = c(5, 5))

If you have a test set to predict, then you can predict it using for example as is mentioned in the manual

test_Var1 <- c(rnorm(50, 1, 0.5), rnorm(50, -0.6, 0.2))
test_Var2 <- c(rnorm(50, -0.8, 0.2), rnorm(50, 2, 1))
test_x <- matrix(c(test_Var1, test_Var2), nrow = 100, ncol = 2)
nn.test(dnn, test_x, y)

#[1] 0.25

Again your test_x must be numeric. If your problem is that you have the values as character, then you can convert it to numeric by mydata<- as.numeric()

Upvotes: 1

Related Questions