tidymodels 如何为朴素贝叶斯模型设置先验

tidymodels how to set priors for a Naive Bayes model

我想使用 tidymodels 框架训练带有朴素贝叶斯分类器的模型。

Tidymodels 使用 discrim 包,它本身使用 klaR 包来估计朴素贝叶斯模型。

可以在 tidymodels 框架中指定一个 NB 模型,例如:

naive_Bayes(
  mode = "classification",
  engine = "klaR",
  smoothness = NULL,
  Laplace = NULL)

检查 klaR 包中的原始 NB 函数时,虽然,它有一个参数:

prior
the prior probabilities of class membership. If unspecified, the class proportions for the training set are used. If present, the probabilities should be specified in the order of the factor levels.

问题是,我还没有找到在 tidymodel 管道中指定这些先验的方法。仅将先前参数添加到上面显示的 naive_Bayes 函数,不起作用并引发错误:

Error in naive_Bayes(prior = rep(0.2, 5)) : 
  unused argument (prior = rep(0.2, 5))

知道在哪里可以进行参数设置吗?

您可以将此模型参数设置为 engine argument:

library(discrim)
#> Loading required package: parsnip

parabolic_prior <- runif(n = 500)

klar_mod <-
    naive_Bayes() %>%
    set_engine("klaR", prior = parabolic_prior) 

translate(klar_mod)
#> Naive Bayes Model Specification (classification)
#> 
#> Engine-Specific Arguments:
#>   prior = parabolic_prior
#> 
#> Computational engine: klaR 
#> 
#> Model fit template:
#> discrim::klar_bayes_wrapper(x = missing_arg(), y = missing_arg(), 
#>     prior = parabolic_prior, usekernel = TRUE)

klar_fit <- fit(klar_mod, class ~ ., data = parabolic)

reprex package (v2.0.1)

创建于 2022-01-26