【发布时间】:2019-03-31 13:29:40
【问题描述】:
我是 caret 包的新手(通常是使用 r 和 caret 进行机器学习)。我使用来自西雅图的公开数据集,我想从中预测未来传入请求的类别(按分类)。
首先,我对我的数据集进行 80/20 拆分。我想通过使用插入符号的 knnImpute 功能来估算数据中的一些 NA。经过相当长的运行时间后,我收到以下错误消息:
Error in dimnames(x) <- dn :
length of 'dimnames' [2] not equal to array extent
我做错了什么,我该如何解决这个问题?
有更多关于此错误的帖子。不幸的是,我没有找到合适的解决方案来帮助我解决我的问题...
我的数据集 (v1.0) 如下所示:
> dataset %>% str()
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 170657 obs. of 9 variables:
$ request_type : Factor w/ 29 levels "Abandoned_Vehicle",..: 10 10 10 10 10 10 10 10 10 10 ...
$ city_department: Factor w/ 8 levels "Center","City_Light",..: 3 3 3 3 3 3 3 3 3 3 ...
$ neighborhood : Factor w/ 91 levels "Adams","Alki",..: 1 1 4 4 10 13 21 21 21 24 ...
$ weekday : Ord.factor w/ 7 levels "So"<"Mo"<"Di"<..: 5 2 2 5 1 3 6 4 4 2 ...
$ month : Ord.factor w/ 12 levels "Jän"<"Feb"<"Mär"<..: 4 6 1 3 4 3 2 4 7 5 ...
$ cal_week : num 15 23 2 10 17 10 6 16 29 21 ...
$ holiday : Factor w/ 2 levels "noholiday","holiday": 1 1 1 1 1 1 1 1 1 1 ...
$ businessday : Factor w/ 2 levels "businessday",..: 1 1 1 1 2 1 1 1 1 1 ...
$ goodfriday : Factor w/ 2 levels "nogoodfriday",..: 1 1 1 1 1 1 1 1 1 1 ...
> dataset %>% skim()
Skim summary statistics
n obs: 170657
n variables: 9
── Variable type:factor ───────────────────────────────────────────────────────────────────────────────────────
variable missing complete n n_unique top_counts ordered
businessday 0 170657 170657 2 bus: 136087, nob: 34570, NA: 0 FALSE
city_department 0 170657 170657 8 Pol: 54916, Pub: 38171, Dep: 34712, Fin: 25471 FALSE
goodfriday 0 170657 170657 2 nog: 170140, goo: 517, NA: 0 FALSE
holiday 0 170657 170657 2 noh: 167514, hol: 3143, NA: 0 FALSE
month 0 170657 170657 12 Aug: 15247, Okt: 14807, Sep: 14785, Mär: 14781 TRUE
neighborhood 6447 164210 170657 91 NA: 6447, Bro: 4975, Uni: 3941, Wal: 3919 FALSE
request_type 0 170657 170657 29 Aba: 34478, Cus: 22275, Ill: 22033, Par: 16521 FALSE
weekday 0 170657 170657 7 Di: 28972, Mi: 28734, Mo: 28721, Do: 27298 TRUE
── Variable type:numeric ──────────────────────────────────────────────────────────────────────────────────────
variable missing complete n mean sd p0 p25 p50 p75 p100 hist
cal_week 0 170657 170657 26.52 14.78 1 14 27 39 53 ▇▇▇▇▇▇▆▆
我的拆分代码:
set.seed(100)
split <- createDataPartition(dataset$request_type, p=0.8, list=FALSE)
train <- dataset[split,]
train_x = train[, 2:8]
train_y = train$request_type
test <- dataset[-split,]
test_x = test[, 2:8]
test_y = test$request_type
我的插补代码:
model.preprocessed.imputed <- preProcess(train, method='knnImpute')
model.preprocessed.imputed
train <- predict(model.preprocessed.imputed, newdata = train)
Wenn 运行预测,我收到错误消息
Error in dimnames(x) <- dn :
length of 'dimnames' [2] not equal to array extent
从回溯中我得到以下信息:
Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent
3. `colnames<-`(`*tmp*`, value = miss_names)
2. predict.preProcess(PreProcess.MissingDatamodel, newdata = train)
1. predict(PreProcess.MissingDatamodel, newdata = train)
2019 年 4 月 2 日更新
我的数据集的第一个版本(v1.0)向我展示了一个混合类:
> dataset %>% str()
Classes ‘spec_tbl_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 170657 obs. of 9 variables:
由于我发现一些帖子表明插入符号可能对 tibbles 有奇怪的反应,因此我尝试将我的数据集转换为通用数据框 (v1.1):
dataset <- as.data.frame(dataset)
dataset %>% str()
'data.frame': 170657 obs. of 9 variables:
$ request_type : Factor w/ 29 levels "Abandoned.Vehicle",..: 10 10 10 10 10 10 10 10 10 10 ...
$ city_department: Factor w/ 8 levels "Center","City.Light",..: 3 3 3 3 3 3 3 3 3 3 ...
$ neighborhood : Factor w/ 91 levels "Adams","Alki",..: 1 1 4 4 10 13 21 21 21 24 ...
$ weekday : Ord.factor w/ 7 levels "So"<"Mo"<"Di"<..: 5 2 2 5 1 3 6 4 4 2 ...
$ month : Ord.factor w/ 12 levels "Jän"<"Feb"<"Mär"<..: 4 6 1 3 4 3 2 4 7 5 ...
$ cal_week : num 15 23 2 10 17 10 6 16 29 21 ...
$ holiday : Factor w/ 2 levels "noholiday","holiday": 1 1 1 1 1 1 1 1 1 1 ...
$ businessday : Factor w/ 2 levels "businessday",..: 1 1 1 1 2 1 1 1 1 1 ...
$ goodfriday : Factor w/ 2 levels "nogoodfriday",..: 1 1 1 1 1 1 1 1 1 1 ...
dataset %>% skim()
Skim summary statistics
n obs: 170657
n variables: 9
── Variable type:factor ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
variable missing complete n n_unique top_counts ordered
businessday 0 170657 170657 2 bus: 136087, nob: 34570, NA: 0 FALSE
city_department 0 170657 170657 8 Pol: 54916, Pub: 38171, Dep: 34712, Fin: 25471 FALSE
goodfriday 0 170657 170657 2 nog: 170140, goo: 517, NA: 0 FALSE
holiday 0 170657 170657 2 noh: 167514, hol: 3143, NA: 0 FALSE
month 0 170657 170657 12 Aug: 15247, Okt: 14807, Sep: 14785, Mär: 14781 TRUE
neighborhood 6447 164210 170657 91 NA: 6447, Bro: 4975, Uni: 3941, Wal: 3919 FALSE
request_type 0 170657 170657 29 Aba: 34478, Cus: 22275, Ill: 22033, Par: 16521 FALSE
weekday 0 170657 170657 7 Di: 28972, Mi: 28734, Mo: 28721, Do: 27298 TRUE
── Variable type:numeric ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
variable missing complete n mean sd p0 p25 p50 p75 p100 hist
cal_week 0 170657 170657 26.52 14.78 1 14 27 39 53 ▇▇▇▇▇▇▆▆
虽然现在才属于 data.frame 类,但它并没有解决我的问题。
【问题讨论】:
-
我们在哪里可以找到数据集?你能添加一个
dput吗? -
仅使用数据集的前 200 行即可复制该错误。有趣的是,如果省略前 100 行,则在使用 100:5000 行时不会发生错误,但在 100:10000 时会出现错误。我还没有找到问题的确切根源。
-
我也有直觉认为它与我的数据集对象有关。在我进一步在线搜索解决方案的过程中,我还发现插入符号函数似乎不是 100% 与小标题兼容的指标。所以我尝试使用“dataset