我正在尝试从插入符号包中实现rfe。我对X和Y的观察次数相同,但是不断出现错误。
x和y中的样本数应该相同
我的脚本如下
# 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"))
观察计数
> nrow(test_X)
[1] 2958
> nrow(test_y)
[1] 2958
> nrow(train_X)
[1] 11845
> nrow(train_y)
[1] 11845
有人看过这个问题吗?任何帮助是极大的赞赏。谢谢。
答案 0 :(得分:0)
根据?rfe
参数y
应该
训练集结果(数字或因子)的向量
您的y
是train_y <- as.data.frame(training_set[,1])
,即data.frame。因此,length (train_y)
为1,不等于nrow(test_X)
。
尝试使用:
results <- rfe(x=train_X, y=train_y[, 1], sizes=c(1:5),testX=test_X, testY=test_y, rfeControl=control)