在预测KNN Imputation时如何改善计算时间?

时间:2017-01-23 18:40:45

标签: performance r-caret knn imputation

我觉得我的数据集的运行时间非常慢,这就是代码:

    library(caret)
    library(data.table)
    knnImputeValues <- preProcess(mainData[trainingRows, imputeColumns], method = c("zv", "knnImpute"))
    knnTransformed <- predict(knnImputeValues, mainData[ 1:1000, imputeColumns])

PreProcess进入knnImputeValues的速度相当快,但预测功能需要花费大量时间。当我在数据子集上计算时,这就是结果:

testtime <- system.time(knnTransformed <- predict(knnImputeValues, mainData[ 1:15000, imputeColumns
testtime

user     969.78
system   38.70 
elapsed  1010.72 

此外,应该注意插入符号预处理使用“RANN”。

现在我的完整数据集是:

 str(mainData[ , imputeColumns])
'data.frame':   1809032 obs. of  16 variables:
 $ V1: int  3 5 5 4 4 4 3 4 3 3 ...
 $ V2: Factor w/ 3 levels "1000000","1500000",..: 1 1 3 1 1 1 1 3 1 1 ...
 $ V3: Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
 $ V4: int  2 5 5 12 4 5 11 8 7 8 ...
 $ V5: int  2 0 0 2 0 0 1 3 2 8 ...
 $ V6: int  648 489 489 472 472 472 497 642 696 696 ...
 $ V7: Factor w/ 4 levels "","N","U","Y": 4 1 1 1 1 1 1 1 1 1 ...
 $ V8: int  0 0 0 0 0 0 0 1 1 1 ...
 $ V9: num  0 0 0 0 0 ...
 $ V10: Factor w/ 56 levels "1","2","3","4",..: 45 19 19 19 19 19 19 46 46 46 ...
 $ V11: Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
 $ V12: num  2 5 5 12 4 5 11 8 7 8 ...
 $ V13: num  2 0 0 2 0 0 1 3 2 8 ...
 $ V14: Factor w/ 4 levels "1","2","3","4": 2 2 2 2 2 2 2 2 3 3 ...
 $ V15: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 2 2 2 ...
 $ V16: num  657 756 756 756 756 ...

那么有什么我做错了,或者这是典型的运行时间需要多长时间?如果你背后的信封外推(我知道这不是完全准确的)你会得到33天?

看起来系统时间非常短,用户时间很长,这是正常的吗?

我的电脑是一台笔记本电脑,配有Intel(R)Core(TM)i5-6300U CPU @ 2.40Ghz处理器。

另外这会改善预测函数的运行时间吗?

cl <- makeCluster(4)
registerDoParallel()

我试过了,除了所有处理器在我的任务管理器中看起来更活跃之外,它似乎没有什么区别。

焦点问题:我正在使用Caret软件包对180万行进行KNN Imputation,我目前的工作方式将需要一个多月才能运行,我该如何编写这样的方式我可以在更快的时间内完成它(如果可能的话)?

感谢您提供的任何帮助。答案很可能就是“这需要多长时间不要打扰”我只是想排除任何可能的错误。

1 个答案:

答案 0 :(得分:0)

您可以通过imputation套餐和使用可以从Github安装的帐篷加快速度:

Sys.setenv("PKG_CXXFLAGS"="-std=c++0x")
devtools::install_github("alexwhitworth/imputation")

Canopies使用便宜的距离度量 - 在这种情况下距离数据均值向量 - 得到近似邻域。一般来说,我们希望保持每个尺寸的檐篷&lt; 100k所以对于1.8M行,我们将使用20个檐篷:

library("imputation")
to_impute <- mainData[trainingRows, imputeColumns] ## OP undefined
imputed <- kNN_impute(to_impute, k= 10, q= 2, verbose= TRUE, 
                      parallel= TRUE, n_canopies= 20)

注意:

插补包需要数字数据输入。您的str输出中有几个因子变量。他们会导致失败。

如果你有错过的行,你也会得到一些平均向量插补。

# note this example data is too small for canopies to be useful
# meant solely to illustrate
set.seed(2143L)
x1 <- matrix(rnorm(1000), 100, 10)
x1[sample(1:1000, size= 50, replace= FALSE)] <- NA
x_imp <- kNN_impute(x1, k=5, q=2, n_canopies= 10)
sum(is.na(x_imp[[1]])) # 0

# with fully missing rows
x2 <- x1; x2[5,] <- NA
x_imp <- kNN_impute(x2, k=5, q=2, n_canopies= 10)
[1] "Computing canopies kNN solution provided within canopies"
[1] "Canopies complete... calculating kNN."
row(s) 1 are entirely missing. 
                     These row(s)' values will be imputed to column means.
Warning message:
In FUN(X[[i]], ...) :
  Rows with entirely missing values imputed to column means.