1

我正在尝试从 caret 包中实现 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

以前有人见过这个问题吗?任何帮助是极大的赞赏。谢谢你。

4

1 回答 1

0

根据?rfe说法y应该是

训练集结果的向量(数字或因子)

ytrain_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)
于 2018-09-24T08:38:32.180 回答