If I analyse a random forest in python with scikit I do:

target = "time"

dataIn = data[features + [target]]

# Splitting data into x % training data and Hyperparameter.TestSize % test data
X, y = dataIn[dataIn.columns.difference([target])], dataIn[target]

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y,
                                                                    test_size=TestSize,
                                                                    random_state=42)

If I do a gradient boosted random forest in R I do:

#split data into training and test data sets
trainIndex = createDataPartition(daten$target, p=0.9, list=FALSE, times=1)
train = daten[trainIndex,]
test = daten[-trainIndex,]
rownames(train) <- NULL
rownames(test) <- NULL

gbmTraining <- gbm(train$target~., data=train, distribution = "gaussian", verbose = TRUE,
                   n.trees = 100, 
                   cv.folds = 4, 
                   n.cores = NULL, 
                   shrinkage = 0.3,       
                   interaction.depth = 7, 
                   n.minobsinnode = 15)

As you see in the scikit implementation I have the target not in the data set. There will be created a new dataframe that has only the target feature as column.

In the gbm R implementation instead the dataframe train contains my target and I communicate to it with train$target.

Therefore I want to ask if you split the data in train and test parts in machine learning algorithms in general (I mean I am comparing two different models), do the target has to be a column in the train data set or do the target has to be a dataframe for it's own?

Whether you need two different data sets depends on the arguments needed in function you are using. Some model fitting functions need a formula and a data argument. Some functions need a xand a y argument. Sometimes you have both options.

For example a linear model (lm) takes a formula and a data argument. Since you can only enter 1 data frame in the data argument, your dependent and independent variables have to be in one and the same dataframe. This is the same as in your example with the gbm function.

However for example a random forest (randomForestpackage) takes the arguments x and y. Therefore, you can have two dataframes (one with y data, one with x data).

Conclusion: it always depends on the function and package that you are using and the only way to find out is to read in the documentation which of those two ways you should be using.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.