Not able to apply xgboost function in Linear regression problem
Hi,
I want to apply xgboost on a regression model, However I am confused, what should I provide in "label" argument. Please help here.
Dependent variable has numeric values. Below are the First 6 observations of the regression target:
head(final_data[1:n.train,'Dependent']) [1] 4996 3784 1504 4994 3687 3084
Now if I am putting this "dependent" variable into labels & executing below code:
param <- list("objective" = "reg:linear", "num_class" = 9, "nthread" = 8, "eta" = 0.08, "subsample"=0.8, "gamma" = 1, "min_child_weight" = 2, "max_depth"= 12, "colsample_bytree" = 1 ) model_xg <- xgboost(param=param,data = final_data[1:n.train,],label=final_data[1:n.train,'Dependent'],nrounds=250)
Then I am getting following error:
Error in xgb.get.DMatrix(data, label) : xgboost: Invalid input of data In addition: Warning message: In xgb.get.DMatrix(data, label) : xgboost: label will be ignored.
However as suggested in other post; when I converted the data into matrix as follows:
x = as.matrix(final_data) trind = c(1:n.train) teind = c((n.train+1):nrow(x)) model_xg <- xgboost(param=param,data = x[trind,],label=as.numeric(x[trind,1]),nrounds=250)
Now I am getting different error:
Error in xgb.DMatrix(data, label = label) : can not open file "4996"
Please find below path of data file: [https://drive.google.com/file/d/0B_OB4fVwsgJKTHIySFlpS1FDM2c/view?usp=sharing]
Please let me know what am I doing wrong?
regards, Vivek
Thanks for providing the data. However please also provide us the code to reproduce the error you met.
I have already share the code in my above posting. Could you please let me know what other code do you want?
Usually, it is the best if you can provide us the whole process from read in the data to where the error is observed. Based on the segments you provided so far, I cannot see the error. It must be in somewhere else.
Please find path of the R code: https://github.com/vivekag/CompModel/blob/master/Comp_Model.R
Mentioned "data1.csv" file link is already shared.
@vivekag In your data, there're some fields containing characters, so simply applying x = as.matrix(final_data) is changing all the numbers in other fields into characters as well.
> x[1:4,1:4]
Dependent Average.Weekly.Wage Body.Part.Code Cause.Code
[1,] "4996" "7.3447191" "46" "1700"
[2,] "3784" "3.4657359" "42" "1500"
[3,] "1504" "7.5564280" "42" "1500"
[4,] "4994" "5.2470241" "90" "1500"
xgboost only support numerical input, therefore I suggest you to pick those colums with characters out, and do a finer data transformation. Besides, it is always benefitial to look at your data before feeding it to a model :)
Thanks @hetong007 for your reply. Now I have changed value of x from
x = as.matrix(final_data) to x <- sapply(final_data,as.numeric) Now matrix has all data numeric.
Now I am getting different error:
Error in xgb.iter.eval(bst$handle, watchlist, i - 1, feval) : label and prediction size not matchhint: use merror or mlogloss for multi-class classification
What is meant by prediction size here? I am providing parameter objective as reg:liner then why is it asking for merror or mlogloss for multi-class classification? I want to do solve regression problem. Please help me to understand all these confusion.
Please see updated code on below github link: https://github.com/vivekag/CompModel/blob/master/Comp_Model.R
It is because you set the unnecessary parameter "num_class" = 9 here. It indicates the label has nine classes.
Thank you for the help :)