【问题标题】:e1071 Package: naiveBayes prediction is slowe1071 包:naiveBayes 预测很慢
【发布时间】:2017-04-14 06:14:50
【问题描述】:

我正在尝试从Re1071 运行naiveBayes 分类器。我遇到了一个问题,即预测所需的时间比训练所需的时间长,大约是 300 倍。

我想知道是否有其他人观察到这种行为,如果有,您是否对如何改进它有任何建议。

此问题仅在某些情况下出现。下面,我有在 Iris 数据集上训练和预测 NB 分类器的代码。在这里,训练和预测时间非常接近(预测需要 10 倍而不是 300 倍)。我可以在网上找到的关于这个问题的唯一其他痕迹是here。在那种情况下,答案是确保将分类变量格式化为因子。我已经这样做了,但仍然没有看到任何改进。

我玩过N 的样本大小,随着N 的减少,问题似乎得到了缓解。也许这是算法的预期行为?将N 减少 10 倍会导致预测仅慢 150 倍,但增加 10 倍会产生类似的 300 倍减速。这些数字对我来说似乎很疯狂,特别是因为我过去曾在大约 300,000 个示例的数据集上使用过这个算法,并且发现它非常快。有些东西看起来很可疑,但我不知道是什么。

我在 Linux 上使用 R 版本 3.3.1。 e1071 软件包是最新的(2015 版)。

下面的代码应该可以在任何机器上重现。仅供参考,我的机器将 Iris 分类计时为 0.003 秒,Iris 预测计时为 0.032 秒,模拟数据分类计时为 0.045 秒,结果预测计时为 15.205 秒。如果您得到的数字与这些不同,请告诉我,因为这可能是我本地计算机上的问题。

# Remove everything from the environment and clear out memory
rm(list = ls())
gc()

# Load required packages and datasets
require(e1071)
data(iris)

# Custom function: tic/toc function to time the execution
tic <- function(gcFirst = TRUE, type=c("elapsed", "user.self", "sys.self"))
{
  type <- match.arg(type)
  assign(".type", type, envir=baseenv())
  if(gcFirst) gc(FALSE)
  tic <- proc.time()[type]         
  assign(".tic", tic, envir=baseenv())
  invisible(tic)
}

toc <- function()
{
  type <- get(".type", envir=baseenv())
  toc <- proc.time()[type]
  tic <- get(".tic", envir=baseenv())
  print(toc - tic)
  invisible(toc)
}

# set seed for reproducibility
set.seed(12345)

#---------------------------------
# 1. Naive Bayes on Iris data
#---------------------------------
tic()
model.nb.iris <- naiveBayes(Species~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,data=iris)
toc()
tic()
pred.nb.iris <- predict(model.nb.iris, iris, type="raw")
toc()

#---------------------------------
# 2. Simulate data and reproduce NB error
#---------------------------------
# Hyperparameters
L <- 5   # no. of locations
N <- 1e4*L

# Data
married        <- 1*(runif(N,0.0,1.0)>.45)
kids           <- 1*(runif(N,0.0,1.0)<.22)
birthloc       <- sample(1:L,N,TRUE)
major          <- 1*(runif(N,0.0,1.0)>.4)
exper          <- 15+4*rnorm(N)
exper[exper<0] <- 0
migShifter     <- 2*runif(N,0.0,1.0)-1
occShifter     <- 2*runif(N,0.0,1.0)-1
X <- data.frame(rep.int(1,N),birthloc,migShifter,occShifter,major,married,kids,exper,exper^2,exper^3)
colnames(X)[1] <- "constant"
rm(married)
rm(kids)
rm(birthloc)
rm(major)
rm(exper)
rm(occShifter)

# Parameters and errors
Gamma <- 15*matrix(runif(7*L), nrow=7, ncol=L)
eps <- matrix(rnorm(N*L, 0, 1), nrow=N, ncol=L)

# Deterministic portion of probabilities
u <- matrix(rep.int(0,N*L), nrow=N, ncol=L)
for (l in 1:L) {
    u[ ,l] = (X$birthloc==l)*Gamma[1,l] +
    X$major*Gamma[2,l]         + X$married*Gamma[3,l]              
    X$kids*Gamma[4,l]          + X$exper*Gamma[5,l]              
    X$occShifter*Gamma[6,l]    + X$migShifter*X$married*Gamma[7,l]
    eps[ ,l]
}

choice <- apply(u, 1, which.max)

# Add choice to data frame
dat <- cbind(choice,X)

# factorize categorical variables for estimation
dat$major      <- as.factor(dat$major)
dat$married    <- as.factor(dat$married)
dat$kids       <- as.factor(dat$kids)
dat$birthloc   <- as.factor(dat$birthloc)
dat$choice     <- as.factor(dat$choice)

tic()
model.nb <- naiveBayes(choice~birthloc+major+married+kids+exper+occShifter+migShifter,data=dat,laplace=3)
toc()
tic()
pred.nb <- predict(model.nb, dat, type="raw")
toc()

【问题讨论】:

  • 如果你不需要每个类的条件后验概率,它会快一点(在我的机器上大约 11 秒,而不是大约 15 秒)。
  • 谢谢@sandipan!我确实需要这些,但我感谢您在您的机器上运行代码!
  • 更新:根据包维护者的说法,这些计算时间并不奇怪。一切似乎都按预期运行。
  • 好的,但是作为一个生成模型,训练时间不应该比预测时间长吗?这有点违反直觉。
  • 您可以查看其他朴素贝叶斯实现 :) cran.r-project.org/web/packages/naivebayes/index.html

标签: r classification naivebayes


【解决方案1】:

我遇到了同样的问题。我需要在一些大矩阵(10000 行,1000-2000 列)上运行朴素贝叶斯并预测很多次(1000 次)。由于我有一些时间,我决定实现我自己的朴素贝叶斯实现以使其更快一点:

https://cran.r-project.org/web/packages/fastNaiveBayes/index.html

我做了一些工作并用它创建了一个包:https://cran.r-project.org/web/packages/fastNaiveBayes/index.html。现在使用伯努利事件模型快了大约 330 倍。此外,它实现了多项式事件模型(甚至更快)和高斯模型(稍快)。最后,一个混合模型,可以为不同的列使用不同的事件模型并将它们组合起来!

e1071 在预测函数中如此缓慢的原因是因为它们本质上使用了双 for 循环。从 2017 年左右开始,已经有一个拉取请求开放,其中至少对其中一个进行了矢量化,但尚未被接受。

【讨论】:

猜你喜欢
  • 2020-10-21
  • 1970-01-01
  • 2017-06-25
  • 2012-12-30
  • 2012-10-01
  • 2016-07-26
  • 2018-10-31
  • 1970-01-01
  • 2017-07-10
相关资源
最近更新 更多