r - 如何为 mlr predict 编写预测函数以在 AzureML 中作为 Web 服务上传?
问题描述
我正在尝试将 AzureML 中的 R 模型作为 web 服务上传,模型使用 R 中的 mlr 包及其预测函数,mlr predict 的输出是“PredictionClassif”“Prediction”的表,用于线性模型,如我使用的回归
PredictAction <- function(inputdata){
predict(RegModel, inputdata, type="response")
}
这在 Azure 中运行良好。
当我使用 mlr 包进行具有预测类型概率的分类时,我必须编写的预测函数为,
PredictAction <- function(inputdata){
require(mlr)
predict(randomForest,newdata=inputdata)
}
调用函数时
publishWebService(ws, fun, name, inputSchema)
它产生一个错误
converting `inputSchema` to data frame
Error in convertArgsToAMLschema(lapply(x, class)) :
Error: data type "table" not supported
因为 predict 函数会生成一个我不知道如何转换或修改的表,所以我给出了输出模式
publishWebService(ws, fun, name, inputSchema,outputschema)
我不确定如何指定输出模式https://cran.r-project.org/web/packages/AzureML/AzureML.pdf
outputschema 是一个列表,来自 mlr 的预测函数产生类的输出
class(pred_randomForest)
"PredictionClassif" "Prediction"
并且数据输出是一个数据帧
class(pred_randomForest$data)
"data.frame"
我正在寻求有关 publishWebService 函数中输出模式语法的帮助,或者我是否必须添加该函数的任何其他参数。不确定问题出在哪里,AzureML 是否无法读取包装好的模型,或者 mlr 的预测函数是否在 AzureML 中正确执行。
在 AzureML 中出现以下错误
Execute R Script Piped (RPackage) : The following error occurred during evaluation of R script: R_tryEval: return error: Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "c('FilterModel', 'BaseWrapperModel', 'WrappedModel')"
解决方案
这是在 R 中使用 XGBoost 库的示例:
library("xgboost") # the main algorithm
##Load the Azure workspace. You can find the ID and the pass in your workspace
ws <- workspace(
id = "Your workspace ID",
auth = "Your Auth Pass"
)
##Download the dataset
dataset <- download.datasets(ws, name = "Breast cancer data", quote="\"")
## split the dataset to get train and score data
## 75% of the sample size
smp_size <- floor(0.75 * nrow(dataset))
## set the seed to make your partition reproductible
set.seed(123)
## get index to split the dataset
train_ind <- sample(seq_len(nrow(dataset)), size = smp_size)
##Split train and test data
train_dataset <- dataset[train_ind, ]
test_dataset <- dataset[-train_ind, ]
#Get the features columns
features<-train_dataset[ , ! colnames(train_dataset) %in% c("Class") ]
#get the label column
labelCol <-train_dataset[,c("Class")]
#convert to data matrix
test_gboost<-data.matrix(test_dataset)
train_gboost<-data.matrix(train_dataset)
#train model
bst <- xgboost(data = train_gboost, label = train_dataset$Class, max.depth = 2, eta = 1,
nround = 2, objective = "binary:logistic")
#predict the model
pred <- predict(bst,test_gboost )
#Score model
test_dataset$Scorelabel<-pred
test_dataset$Scoreclasses<- as.factor(as.numeric(pred >= 0.5))
#Create
# Scoring Function
predict_xgboost <- function(new_data){
predictions <- predict(bst, data.matrix(new_data))
output <- data.frame(new_data, ScoredLabels =predictions)
output
}
#Publish the score function
api <- publishWebService(
ws,
fun = predict_xgboost,
name = "xgboost classification",
inputSchema = as.data.frame(as.table(train_gboost)),
data.frame = TRUE)
推荐阅读
- python - 来自 .select_related("...") 的列表
- html - CSS - 响应式指向元素
- reactjs - 反应组件中的测试钩子
- python - 如何创建for循环以在Python中为数组中的每个元素添加一个值
- python - pyOpenCL 应对次数增加
- excel - 将选定的 Word 表格复制到 Excel
- azure-devops - 使用逻辑应用中的服务主体连接到 MS Forms 连接器
- html - 在 powerquery-m 中使用 Html.Table 一次转换 2 列
- javascript - 嵌套承诺拒绝行为
- python - 调整大小方法未实现 Python