Error in running GBM

machine_learning

#1

Hi,

I’m trying to run the GBM.FIT for a data and the error i getting is

"The number of rows in x does not equal the length of y"

I 'm attaching the code below for reference.Can anyone help me out ??

library(‘ElemStatLearn’)
library(“klaR”)
library(“caret”)
library(“gbm”)

data(spam)

x=spam[,1:57]# predictor variable
y=spam[,58]#response variable

y <- ifelse(spam==“spam”, 0, 1)

ntrees=1000

model<-gbm.fit(x,y,offset=NULL,distribution = “bernoulli”,n.trees=ntrees,shrinkage =0.01,interaction.depth = 3,n.minobsinnode = 10,nTrain = NULL,verbose = T)


#2

May be the ifelse line is not correct:

Assuming the 58th columnname is “spam”, then code can be:
y <- ifelse(y$spam=="spam", 0, 1)


#3

well the code is correct…the only error i m getting is while running the gbm.fit as that the rows of x and y are not equal, however they are equal as well


#4

Just check that the class of Y is not data.frame
class(y)

also spam==“spam” will not give the desired results since “spam” is a data.frame


#5

sonu …if you have look at the dataset spam it has variable name spam…wud you mind running the code and then telling any answers


#6

Hi @mudit1712. Sonny is trying to help you, please respect the guidelines.

What you can do simply is, post the first few lines of target “y”. before sending it to gbm.fit(). It will be clear if indeed there is some unknown error.

Does this seem reasonable?


#7

hey sorry sonny…found out there was some mistake in the code in the ifelse line…sorry if i did sounded offensive…

thanks for the help