【问题标题】:How to fix "Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent" in R (caret) with 'knnImpute'如何使用'knnImpute'在R(插入符号)中修复“dimnames(x)<-dn中的错误:'dimnames'[2]的长度不等于数组范围”
【发布时间】: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

标签: r r-caret


【解决方案1】:

我想我找到了问题的根源:

我最初使用 tidyverse 的 readr::read_csv(),它以某种方式给了我一个行为怪异的数据对象(如 cmets 中所述的误用 - 感谢您的输入):

dataset <- read_csv("data/DataSet.csv") %>% clean_names()

使用 read.csv() 后,我的数据集中不再有 NA,并且插入符号的所有函数突然都适用于我的数据:

dataset <- read.csv("data/DataSet.csv", stringsAsFactors = FALSE) %>% clean_names()

也许这个发现对其他人也有帮助,因为我浪费了大量时间来寻找由错误数据集对象导致的错误消息。

更新

现在我知道为什么没有 NA 的 anmymore 了。我发现 read.csv() 读取了 NA,但将它们设为空字符串 (""),而 read_csv() 明确地将它们设为 NA。我只是将 NA 也转换为一个因素(“缺失”),因此我不必删除数据并冒丢失信息的风险。

【讨论】:

    猜你喜欢
    • 2018-04-06
    • 2022-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多