【问题标题】:Problem crating a Ranger model with R to use for MLflow使用 R 创建 Ranger 模型以用于 MLflow 时出现问题
【发布时间】:2021-09-15 03:37:20
【问题描述】:

我正在尝试在 R 中使用 MLflow。根据https://www.mlflow.org/docs/latest/models.html#r-function-crate,模型需要使用 crate 风格。我的模型使用了 ranger 包中实现的随机森林功能:

model <- ranger::ranger(formula    = model_formula, 
                        data       = trainset,
                        importance = "impurity", 
                        probability=T, 
                        num.trees  = 500, 
                        mtry       = 10)

模型本身有效,我可以在测试集上进行预测:

test_prediction <- predict(model, testset)

下一步,我尝试将模型带入 crate 风格。我在这里按照https://docs.databricks.com/_static/notebooks/mlflow/mlflow-quick-start-r.html中显示的方法。

predictor <- crate(function(x) predict(model,.x))

然而,当我在测试集上应用“预测器”时,这会导致错误

predictor(testset)
Error in predict(model, .x) : could not find function "predict"

有谁知道如何解决这个问题?我必须在 crate 函数中以不同的方式传输预测函数吗?非常感谢任何帮助;-)

【问题讨论】:

    标签: r mlflow crate


    【解决方案1】:

    根据我的经验,Databricks 快速入门指南是错误的。

    根据Carrierdocumentation,在 crate 内部调用非基本函数时需要使用显式命名空间。由于 predict 实际上是 stats 包的一部分,因此您需要指定 stats::predict。此外,由于您的 crate 函数依赖于名为 model 的全局对象,因此您还需要将其作为参数传递给 crate 函数。

    您的代码最终会看起来像这样(我无法在您的确切用例上对其进行测试,因为我没有您的数据,但这适用于 Databricks 中的 MLflow):

    model <- ranger::ranger(formula    = model_formula, 
                            data       = trainset,
                            importance = "impurity", 
                            probability=T, 
                            num.trees  = 500, 
                            mtry       = 10)
    
    predictor <- crate(function(x) {
        stats::predict(model,x)
        }, model = model)
    
    predictor(testset)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-23
      • 2021-07-06
      • 1970-01-01
      • 2019-03-24
      • 2019-11-06
      • 2020-11-13
      • 2016-05-08
      • 1970-01-01
      相关资源
      最近更新 更多