【问题标题】:caret-rfe: there should be the same number of samples in x and ycaret-rfe:在 x 和 y 中应该有相同数量的样本
【发布时间】:2018-09-24 07:54:11
【问题描述】:

我正在尝试从 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

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

【问题讨论】:

    标签: r r-caret rfe


    【解决方案1】:

    根据?rfe参数y应该是

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

    您的ytrain_y &lt;- 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-07
      • 2019-09-13
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多