gbm
대신 XGBoost
을 사용하려고합니다.회귀 기반 모델에 대해 R에서 XGBoost 사용
내가 얻는 점수는 다소 이상합니다. 그래서 나는 내 코드에서 뭔가 잘못하고 있다고 생각합니다.
내 데이터에는 몇 가지 요인 변수가 포함되며 다른 모든 숫자가 포함됩니다.
응답 변수는 주택 가격을 나타내는 연속 변수입니다.
나는 XGBoost
을 사용하기 위해 핫 언코딩을 사용해야한다고 알고 있습니다. 나는 다음과 같은 코드를 사용하여 그렇게하고 있어요 :
Xtrain <- Xall[1:nrow(train.data), ]
Xtest <- Xall[-(1:nrow(train.data)), ]
을 그리고 모델을 구축하고, 인쇄 : 그 후
Xtest <- test.data
Xtrain <- train.data
XSalePrice <- Xtrain$SalePrice
Xtrain$SalePrice <- NULL
# Combine data
Xall <- data.frame(rbind(Xtrain, Xtest))
# Get categorical features names
ohe_vars <- names(Xall)[which(sapply(Xall, is.factor))]
# Convert them
dummies <- dummyVars(~., data = Xall)
Xall_ohe <- as.data.frame(predict(dummies, newdata = Xall))
# Replace factor variables in data with OHE
Xall <- cbind(Xall[, -c(which(colnames(Xall) %in% ohe_vars))], Xall_ohe)
을, 나는 시험 & 기차 세트에 데이터를 다시 분할하고있어 RMSE & Rsquared는 :
# Model
xgb.fit <- xgboost(data = data.matrix(Xtrain), label = XSalePrice,
booster = "gbtree", objective = "reg:linear",
colsample_bytree = 0.2, gamma = 0.0,
learning_rate = 0.05, max_depth = 6,
min_child_weight = 1.5, n_estimators = 7300,
reg_alpha = 0.9, reg_lambda = 0.5,
subsample = 0.2, seed = 42,
silent = 1, nrounds = 25)
xgb.pred <- predict(xgb.fit, data.matrix(Xtrain))
postResample(xgb.pred, XSalePrice)
문제는 매우 RMSE & Rsxquare 떨어져 얻고있다 :
RMSE Rsquared
1.877639e+05 5.308910e-01
GBM을 사용할 때 얻을 수있는 결과에서 매우 벗어납니다.
내가 잘못 생각하고 있다고 생각하는데, 익숙하지 않은 One Hot Encoding 단계에서 추측 할 수 있습니다. 내 데이터를 조정하여 googled 코드를 사용했습니다.
누군가 내가 뭘 잘못하고 '고칠'수있는 방법을 나타낼 수 있습니까?
UPDATE :
@Codutie 대답을 검토 한 후, 내 코드가 몇 가지 오류 :
Xtrain <- sparse.model.matrix(SalePrice ~. , data = train.data)
XDtrain <- xgb.DMatrix(data = Xtrain, label = "SalePrice")
xgb.DMatrix
는 생산 :
Error in setinfo.xgb.DMatrix(dmat, names(p), p[[1]]) :
The length of labels must equal to the number of rows in the input data
train.data
는 데이터 프레임이며, 1453이있다 행. 라벨 SalePrice
는 1453 개 값 (없음 누락 된 값)
감사