I'm building a classification model to predict some target variable.
I have only one continuous feature (age) that I am interested in scaling. I split my data into train and test sets, I scale this feature separately in both the train and test (using the scale()
function in R), and only then do I train the model, using the caret
package.
I've been told this is not entirely correct, I should scale this feature in the test set according to the train set, and I'm not sure what this means exactly. How do I scale a feature in the test set according to the train set?
I'm dealing with the test and train as entirely separate datasets so that I won't have information leakage. If this is not the way to do it, how should I apply it in R?
NOTE - I have many continuous variables in the data, however, I want to scale only the age feature.
Here is my code, I'm using gradient boosting as my classification algorithm:
for (i in (1:10)) {
print(i)
set.seed(i)
IND = createDataPartition(y = MYData$Target_feature, p=0.8, list = FALSE)
TRAIN_set = MYData[IND, ]
TEST_set = MYData[-IND,]
TRAIN_set$age = scale(TRAIN_set$age)
TEST_set$age = scale(TEST_set$age)
GBMModel <- train(Target_feature~., data = TRAIN_set,
method = "gbm",
metric="ROC",
trControl = ctrlCV,
tuneGrid = gbmGRID,
verbose = FALSE
)
AUCs_Trn[i] = auc(roc(TRAIN_set$Target_feature,predict(GBMModel,TRAIN_set, type='prob')[,1]))
AUCs_Tst[i] = auc(roc(TEST_set$Target_feature,predict(GBMModel,TEST_set, type='prob')[,1]))
}