r - e1071 包 : naiveBayes prediction is slow

标签 r classification naivebayes

我正在尝试运行 naiveBayes来自 R 的分类器包裹e1071 .我遇到了一个问题,即预测所需的时间比训练所需的时间长约 300 倍。

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

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

我玩过样本大小 N问题似乎减轻了 N减少。也许这是算法的预期行为?递减N增加 10 倍会导致预测仅慢 150 倍,但增加 10 倍会产生类似的 300 倍减速。这些数字对我来说似乎很疯狂,尤其是因为我过去曾在具有约 300,000 个示例的数据集上使用此算法,并且发现它非常快。似乎有些可疑,但我无法弄清楚是什么。

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

下面的代码应该可以在任何机器上重现。仅供引用,我的机器将虹膜分类计时为 0.003 秒,虹膜预测计时为 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()

最佳答案

我遇到了同样的问题。我需要在一些大矩阵(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 在 predict 函数中如此缓慢的原因是因为它们本质上使用了双 for 循环。从 2017 年初开始,已经有一个拉取请求至少对其中一个进行了矢量化,但尚未被接受。

关于r - e1071 包 : naiveBayes prediction is slow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40879624/

相关文章:

regression - 朴素贝叶斯回归

machine-learning - 了解朴素贝叶斯

r - 如何检查多列是否按行等效,忽略 NA 值

r - 如何创建签名的 S3 url

python - 如何将字符串数据分类为整数?

machine-learning - python中适合稀疏高维特征的理想分类器(具有层次分类)

matlab - 朴素贝叶斯分类器和判别分析的准确性还差得很远

r - 解码 SparkR 数据框

r - 如何将日期格式从 YYYY/MM/DD 更改为 DD/MM/YYYY

python-3.x - 反向传播神经网络