gpt4 book ai didi

r - 当有一个角色作为 ID 的变量时,为什么部署带有香根草的 tidymodel 会抛出错误?

转载 作者:行者123 更新时间:2023-12-05 04:33:30 25 4
gpt4 key购买 nike

我无法部署带有香根草的 tidymodel,并且无法在模型包含一个变量并将角色作为配方中的 ID 时得到预测。请参阅图像中的以下错误:

{"error": "500 - 内部服务器错误",“消息”:“错误:缺少以下必需的列:'Fake_ID'。\n”

虚拟示例的代码如下。我是否需要从模型和配方中删除 ID 变量才能使 Plumber API 正常工作?

#Load libraries
library(recipes)
library(parsnip)
library(workflows)
library(pins)
library(plumber)
library(stringi)



#Upload data
data(Sacramento, package = "modeldata")


#Create fake IDs for testing
Sacramento$Fake_ID <- stri_rand_strings(nrow(Sacramento), 10)


# Train model
Sacramento_recipe <- recipe(formula = price ~ type + sqft + beds + baths + zip + Fake_ID, data = Sacramento) %>%
update_role(Fake_ID, new_role = "ID") %>%
step_zv(all_predictors())

rf_spec <- rand_forest(mode = "regression") %>% set_engine("ranger")

rf_fit <-
workflow() %>%
add_model(rf_spec) %>%
add_recipe(Sacramento_recipe) %>%
fit(Sacramento)


# Create vetiver object
v <- vetiver::vetiver_model(rf_fit, "sacramento_rf")
v


# Allow for model versioning and sharing
model_board <- board_temp()
model_board %>% vetiver_pin_write(v)


# Deploying model
pr() %>%
vetiver_api(v) %>%
pr_run(port = 8088)

Running the example of the Plumber API

最佳答案

截至今天,香根草寻找“霉菌”workflows::extract_mold(rf_fit) 并且仅获取预测变量以创建 ptype。但是当您从工作流中进行预测时,它确实需要所有变量,包括非预测变量。如果您已经使用非预测器训练了模型,那么从今天开始,您可以通过传入自定义 ptype 来使 API 工作:

library(recipes)
#> Loading required package: dplyr
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
#>
#> Attaching package: 'recipes'
#> The following object is masked from 'package:stats':
#>
#> step
library(parsnip)
library(workflows)
library(pins)
library(plumber)
library(stringi)

data(Sacramento, package = "modeldata")
Sacramento$Fake_ID <- stri_rand_strings(nrow(Sacramento), 10)


Sacramento_recipe <-
recipe(formula = price ~ type + sqft + beds + baths + zip + Fake_ID,
data = Sacramento) %>%
update_role(Fake_ID, new_role = "ID") %>%
step_zv(all_predictors())

rf_spec <- rand_forest(mode = "regression") %>% set_engine("ranger")

rf_fit <-
workflow() %>%
add_model(rf_spec) %>%
add_recipe(Sacramento_recipe) %>%
fit(Sacramento)


library(vetiver)
## this is probably easiest because this model uses a simple formula
## if there is more complex preprocessing, select the variables
## from `Sacramento` via dplyr or similar
sac_ptype <- extract_recipe(rf_fit) %>%
bake(new_data = Sacramento, -all_outcomes()) %>%
vctrs::vec_ptype()

v <- vetiver_model(rf_fit, "sacramento_rf", save_ptype = sac_ptype)
v
#>
#> ── sacramento_rf ─ <butchered_workflow> model for deployment
#> A ranger regression modeling workflow using 6 features

pr() %>%
vetiver_api(v)
#> # Plumber router with 2 endpoints, 4 filters, and 0 sub-routers.
#> # Use `pr_run()` on this object to start the API.
#> ├──[queryString]
#> ├──[body]
#> ├──[cookieParser]
#> ├──[sharedSecret]
#> ├──/ping (GET)
#> └──/predict (POST)

reprex package 创建于 2022-03-10 (v2.0.1)

您是否正在使用非预测变量训练生产模型?你介意吗opening an issue on GitHub多解释一下您的用例?

关于r - 当有一个角色作为 ID 的变量时,为什么部署带有香根草的 tidymodel 会抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71397075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com