0
votes

I am trying to upload a R Model in AzureML as webservice, model uses mlr package in R and its predict function, the output of mlr predict is a table of "PredictionClassif" "Prediction", for the linear model like Regression I use

PredictAction <- function(inputdata){
  predict(RegModel, inputdata, type="response")
}

This is working perfectly fine in Azure.

When I use mlr package for classification with predict type probability, the predict function I have to write as,

PredictAction <- function(inputdata){
  require(mlr)
  predict(randomForest,newdata=inputdata)
}

When calling the function

publishWebService(ws, fun, name, inputSchema)

It produces an Error as

converting `inputSchema` to data frame
Error in convertArgsToAMLschema(lapply(x, class)) : 
  Error: data type "table" not supported

as the predict function produces a table which I don't know how to convert or modify, so I give the outputschema

publishWebService(ws, fun, name, inputSchema,outputschema)

I am not sure how to specify the outputschema https://cran.r-project.org/web/packages/AzureML/AzureML.pdf

outputschema is a list, the predict function from mlr produces the output of class

class(pred_randomForest)
"PredictionClassif" "Prediction"

and the data output is a dataframe

class(pred_randomForest$data)
"data.frame"

I am seeking help on the syntax for outputschema in publishWebService function, or whether I have to add any other arguments of the function. Not sure where is the issue, whether AzureML can't read the wrapped Model or whether the predict function of mlr is executed properly in AzureML.

Getting Following Error in 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')" 
1

1 Answers

0
votes

here is the example of using XGBoost library in R:

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)