如何在R(脱字符号)中使用“ knnImpute”修复“ dimnames(x)<-dn中的错误:'dimnames'[2]的长度不等于数组范围”

时间:2019-03-31 13:29:41

标签: r r-caret

我对插入符号包不熟悉(通常对于使用r和插入符号进行机器学习)。我使用了西雅图的一个公开可用的数据集,我想据此预测未来传入请求的类别(按分类)。

首先,我对数据集进行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)

运行预测,我得到了错误消息

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:

由于我发现一些帖子表明插入符号可能会对小动作产生奇怪的反应,因此我尝试将数据集转换为通用数据框(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类,但它不能解决我的问题。

1 个答案:

答案 0 :(得分:1)

我想我找到了问题的根源

我最初使用tidyverse的readr :: read_csv(),它以某种方式给了我一个奇怪行为的数据对象(注释中也提到了滥用-感谢您的输入):

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转换成一个因素(“缺失”),所以我不必删除数据并冒着丢失信息的风险。