caret-rfe: there should be the same number of samples in x and y

692 Views Asked by At

I'm trying to implement rfe from caret package. I have the same number of observations for X and Y but kept getting the error.

there should be the same number of samples in x and y

My script is as follow

# Importing the dataset
dataset <- read.csv('P1training.csv')
dataset <- dataset[,2:ncol(dataset)]

# Splitting test and train dataset
library(caTools)
set.seed(123)
split <- sample.split(dataset$Appliances, SplitRatio = 0.8)
training_set <- subset(dataset, split == TRUE)
test_set <- subset(dataset, split == FALSE)

# Feature Scaling
training_set <- scale(training_set)
test_set <- scale(test_set)

train_X <- as.data.frame(training_set[,2:ncol(training_set)])
train_y <- as.data.frame(training_set[,1])

test_X <- as.data.frame(test_set[,2:ncol(test_set)])
test_y <- as.data.frame(test_set[,1])

nrow(test_X)
nrow(test_y)
nrow(train_X)
nrow(train_y)

# load the library
library(doParallel)
library(mlbench)
library(caret)
# define the control using a random forest selection function
control <- rfeControl(functions=nbFuncs, method="cv", number=1, allowParallel = TRUE)
# run the RFE algorithm
results <- rfe(x=train_X, y=train_y, sizes=c(1:5),testX=test_X, testY=test_y, rfeControl=control)
# summarize the results
print(results)
# list the chosen features
predictors(results)
# plot the results
plot(results, type=c("g", "o"))

Observation counts

> nrow(test_X)
[1] 2958
> nrow(test_y)
[1] 2958
> nrow(train_X)
[1] 11845
> nrow(train_y)
[1] 11845

Anyone seen this issue before? Any help is greatly appreciated. Thank you.

1

There are 1 best solutions below

0
On

According to ?rfe argument y should be

a vector of training set outcomes (either numeric or factor)

Your y is train_y <- as.data.frame(training_set[,1]) i.e. data.frame. Therefore length (train_y) is 1 and is not equal to nrow(test_X).

Try to use:

results <- rfe(x=train_X, y=train_y[, 1], sizes=c(1:5),testX=test_X, testY=test_y, rfeControl=control)