我正在尝试运行R
包e1071
中的naiveBayes
分类器。我遇到了一个问题,预测所需的时间比训练所需的时间长得多,大约是300倍。
我想知道是否还有其他人观察到了这种情况,如果有的话,您是否有任何改进建议。
这个问题只在某些实例中出现。下面是在鸢尾花数据集上对NB分类器进行训练和预测的代码。在这里,训练和预测时间非常接近(预测需要10倍的时间而不是300倍)。我在网上找到的关于此问题的唯一其他痕迹是这里。在那个例子中,答案是确保分类变量格式化为因子。我已经这样做了,但仍然没有看到任何改进。
我已经尝试过不同的样本大小N
,发现当N
减小时,问题似乎会减轻。也许这是算法的预期行为?将N
减小10倍时,预测只需要150倍的时间,但将其增加10倍时,预测速度仍然减慢了300倍。对我来说,这些数字似乎很不可思议,特别是因为我过去曾在包含约300,000个示例的数据集上使用此算法,并发现它非常快。有些可疑,但我无法找到原因。
我正在Linux上使用R
版本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()