如何修复 cor() 函数的 'incompatible dimensions' 错误
How to fix 'incompatible dimensions' error from cor() function
我正在使用 predict() 函数来预测 blackFriday_test 中的 Purchase 变量。当我将 cor() 与这些变量一起用作参数时,我收到一条 'incompatible dimensions' 错误消息。
我试着查看 blackFriday_test 中 Purchas 变量的维度,即 107516,但预测值结果仅为 32955。
数据是从 https://www.kaggle.com/mehdidag/black-friday 下载的。
library(caret)
blackFriday <- read.csv("BlackFriday.csv", stringsAsFactors = T)
这里我去掉了前两个特征,因为它们是标识符
nblackFriday <- blackFriday[, 3:12]
set.seed(189)
train <- sample(nrow(nblackFriday), as.integer(0.8 * nrow(nblackFriday)), replace = F)
blackFriday_train <- nblackFriday[train, ]
blackFriday_test <- nblackFriday[-train, ]
从存在它们的两个变量中删除 NA
nblackFriday$Product_Category_2 <- ifelse(is.na(nblackFriday$Product_Category_2), mean(nblackFriday$Product_Category_2, na.rm = T), nblackFriday$Product_Category_2)
nblackFriday$Product_Category_3 <- ifelse(is.na(nblackFriday$Product_Category_3), mean(nblackFriday$Product_Category_3, na.rm = T), nblackFriday$Product_Category_3)
blackFriday_train$Product_Category_2 <- nblackFriday$Product_Category_2[train]
blackFriday_train$Product_Category_3 <- nblackFriday$Product_Category_3[train]
m <- train(Purchase ~ ., data = blackFriday_train, method = "rpart")
p <- predict(m, blackFriday_test)
cor(p, blackFriday_test$Purchase)
```
#This is where I get the error
I expect the number of predicted values to be the same as the number of rows in blackFriday_test, but they are not.
您替换了训练数据集中的缺失值,但没有替换测试数据集中的缺失值。因此,您对测试的预测低于测试数据集的行数。
您应该在整个数据集上应用您的操作(例如替换 NA),然后在 train/test 中拆分。这样,您的两个数据集就会相似,并且预测会更好。
您在训练集中替换了 NA,但在测试集中没有,因此这些情况被忽略了。
> head(blackFriday_test)
Gender Age Occupation City_Category Stay_In_Current_City_Years Marital_Status Product_Category_1
3 F 0-17 10 A 2 0 12
6 M 26-35 15 A 3 0 1
15 F 51-55 9 A 1 0 5
16 F 51-55 9 A 1 0 4
21 M 26-35 12 C 4+ 1 5
22 M 26-35 12 C 4+ 1 8
Product_Category_2 Product_Category_3 Purchase
3 NA NA 1422
6 2 NA 15227
15 8 14 5378
16 5 NA 2079
21 14 NA 8584
22 NA NA 9872
就像您对训练集所做的那样估算它们,它按预期工作。
blackFriday_test$Product_Category_2 <- nblackFriday$Product_Category_2[-train]
blackFriday_test$Product_Category_3 <- nblackFriday$Product_Category_3[-train]
p <- predict(m, blackFriday_test)
> length(p) == nrow(blackFriday_test)
[1] TRUE
> cor(p, blackFriday_test$Purchase)
[1] 0.7405558
尝试使用插入符号本身的分区和预处理功能。对我来说,它们有助于避免这些类型的简单错误。
我正在使用 predict() 函数来预测 blackFriday_test 中的 Purchase 变量。当我将 cor() 与这些变量一起用作参数时,我收到一条 'incompatible dimensions' 错误消息。
我试着查看 blackFriday_test 中 Purchas 变量的维度,即 107516,但预测值结果仅为 32955。
数据是从 https://www.kaggle.com/mehdidag/black-friday 下载的。
library(caret)
blackFriday <- read.csv("BlackFriday.csv", stringsAsFactors = T)
这里我去掉了前两个特征,因为它们是标识符
nblackFriday <- blackFriday[, 3:12]
set.seed(189)
train <- sample(nrow(nblackFriday), as.integer(0.8 * nrow(nblackFriday)), replace = F)
blackFriday_train <- nblackFriday[train, ]
blackFriday_test <- nblackFriday[-train, ]
从存在它们的两个变量中删除 NA
nblackFriday$Product_Category_2 <- ifelse(is.na(nblackFriday$Product_Category_2), mean(nblackFriday$Product_Category_2, na.rm = T), nblackFriday$Product_Category_2)
nblackFriday$Product_Category_3 <- ifelse(is.na(nblackFriday$Product_Category_3), mean(nblackFriday$Product_Category_3, na.rm = T), nblackFriday$Product_Category_3)
blackFriday_train$Product_Category_2 <- nblackFriday$Product_Category_2[train]
blackFriday_train$Product_Category_3 <- nblackFriday$Product_Category_3[train]
m <- train(Purchase ~ ., data = blackFriday_train, method = "rpart")
p <- predict(m, blackFriday_test)
cor(p, blackFriday_test$Purchase)
```
#This is where I get the error
I expect the number of predicted values to be the same as the number of rows in blackFriday_test, but they are not.
您替换了训练数据集中的缺失值,但没有替换测试数据集中的缺失值。因此,您对测试的预测低于测试数据集的行数。
您应该在整个数据集上应用您的操作(例如替换 NA),然后在 train/test 中拆分。这样,您的两个数据集就会相似,并且预测会更好。
您在训练集中替换了 NA,但在测试集中没有,因此这些情况被忽略了。
> head(blackFriday_test)
Gender Age Occupation City_Category Stay_In_Current_City_Years Marital_Status Product_Category_1
3 F 0-17 10 A 2 0 12
6 M 26-35 15 A 3 0 1
15 F 51-55 9 A 1 0 5
16 F 51-55 9 A 1 0 4
21 M 26-35 12 C 4+ 1 5
22 M 26-35 12 C 4+ 1 8
Product_Category_2 Product_Category_3 Purchase
3 NA NA 1422
6 2 NA 15227
15 8 14 5378
16 5 NA 2079
21 14 NA 8584
22 NA NA 9872
就像您对训练集所做的那样估算它们,它按预期工作。
blackFriday_test$Product_Category_2 <- nblackFriday$Product_Category_2[-train]
blackFriday_test$Product_Category_3 <- nblackFriday$Product_Category_3[-train]
p <- predict(m, blackFriday_test)
> length(p) == nrow(blackFriday_test)
[1] TRUE
> cor(p, blackFriday_test$Purchase)
[1] 0.7405558
尝试使用插入符号本身的分区和预处理功能。对我来说,它们有助于避免这些类型的简单错误。