Ошибка при прогнозировании с использованием модели GBM и пакета mrsdeploy - PullRequest
0 голосов
/ 01 апреля 2020

Я сталкиваюсь с проблемой при вызове Forex.gbm при использовании функции mrsdeploy после публикации модели в качестве службы. Ниже приведены два кода, один из них - демонстрационный код, в котором функция предсказания для модели glm отлично работает как локально, так и после публикации службы, но код GBM работает только локально а не после публикации модели

### -------------------------------------
library(mrsdeploy)

library(gbm)

-----------------Demo Code works fine both locally and after publishing service--------------

carsModel <- glm(formula = am ~ hp + wt, data = mtcars, family = binomial)

#function that can use the model
manualTransmission <- function(hp, wt) {
   newdata <- data.frame(hp = hp, wt = wt)
   predict(carsModel, newdata, type = "response")
}
# test function locally by printing results
print(manualTransmission(120, 2.8)) 

output=> 0.6418125    

remoteLogin("http://localhost:12800",
            username = "admin", 
            password = "****",
            session = FALSE)


api <- publishService(
   "mtService",
   code = manualTransmission,
   model = carsModel,
   inputs = list(hp = "numeric", wt = "numeric"),
   outputs = list(answer = "numeric"),
   v = "v1.0.0"
)
#test model prediction using api call
result <- api$manualTransmission(120, 2.8)

print(result$output("answer")) 
output=> 0.6418125
#Demo Code Ends


###################GBM Code
#load  model object
new_model <- readRDS("C:\\Users\\c11421\\Desktop\\new_model.RDS")

# Produce a prediction function that can use the model
salarypred <- function(PEAAK_EMI_TS,CITY_TIER,TOTAL_SANC_AMT_CC,TIME_SINCE_FIRST_CC,MIN_LIVE_AMT_BY_TENNURE_BL_AND_P,CUSTOMER_COMPANY_CATEGORY_PL,TS_AVG_SANC_CLOSED_PL,AVG_LIVE_AMT_BY_TENNURE_THICK,MIN_SANC_LIVE_REV,AGE_AT_POS,AVG_PAID_LIVE_HL,TIME_SINCE_LAST_LIVE_CC,MIN_MOB_BL_AND_PL,TS_MIN_SANC_CLOSED_AL,MIN_LIVE_AMT_BY_TENNURE_AL,AVG_TENNURE_BL_AND_PL,RESTYPE_RENTED,GENDER
) {
  newdata <- data.frame(PEAAK_EMI_TS=PEAAK_EMI_TS , CITY_TIER=CITY_TIER , TOTAL_SANC_AMT_CC=TOTAL_SANC_AMT_CC , TIME_SINCE_FIRST_CC=TIME_SINCE_FIRST_CC , MIN_LIVE_AMT_BY_TENNURE_BL_AND_P=MIN_LIVE_AMT_BY_TENNURE_BL_AND_P , CUSTOMER_COMPANY_CATEGORY_PL=CUSTOMER_COMPANY_CATEGORY_PL , TS_AVG_SANC_CLOSED_PL=TS_AVG_SANC_CLOSED_PL , AVG_LIVE_AMT_BY_TENNURE_THICK=AVG_LIVE_AMT_BY_TENNURE_THICK , MIN_SANC_LIVE_REV=MIN_SANC_LIVE_REV , AGE_AT_POS=AGE_AT_POS , AVG_PAID_LIVE_HL=AVG_PAID_LIVE_HL , TIME_SINCE_LAST_LIVE_CC=TIME_SINCE_LAST_LIVE_CC , MIN_MOB_BL_AND_PL=MIN_MOB_BL_AND_PL , TS_MIN_SANC_CLOSED_AL=TS_MIN_SANC_CLOSED_AL , MIN_LIVE_AMT_BY_TENNURE_AL=MIN_LIVE_AMT_BY_TENNURE_AL , AVG_TENNURE_BL_AND_PL=AVG_TENNURE_BL_AND_PL , RESTYPE_RENTED=RESTYPE_RENTED , GENDER=GENDER)

  predict.gbm(new_model, newdata, type = "response",n.trees=2722)
}

# test function locally by printing results
print(salarypred(10.61928526,'Tier1',12.16838635,166,-999,'zzzzz',-999,71,82603,42,-999,166,-999,945000,-999,-999,0,'MALE'))  

output=>10.8409

remoteLogin("http://localhost:12800", 
            username = "admin", 
            password = "*****",
            session = FALSE)


 api <- publishService(
   "salService",
   code = salarypred,
   model = new_model,
  inputs = list(PEAAK_EMI_TS= "numeric",CITY_TIER= "character",TOTAL_SANC_AMT_CC= "numeric",TIME_SINCE_FIRST_CC= "numeric",MIN_LIVE_AMT_BY_TENNURE_BL_AND_P= "numeric",CUSTOMER_COMPANY_CATEGORY_PL="character",TS_AVG_SANC_CLOSED_PL= "numeric",AVG_LIVE_AMT_BY_TENNURE_THICK= "numeric",MIN_SANC_LIVE_REV= "numeric",AGE_AT_POS= "numeric",AVG_PAID_LIVE_HL= "numeric",TIME_SINCE_LAST_LIVE_CC= "numeric",MIN_MOB_BL_AND_PL= "numeric",TS_MIN_SANC_CLOSED_AL= "numeric",MIN_LIVE_AMT_BY_TENNURE_AL= "numeric",AVG_TENNURE_BL_AND_PL= "numeric",RESTYPE_RENTED= "numeric",GENDER="character"),
   outputs = list(answer = "numeric"),
   v = "v1.0.0"
 )


#test model prediction using api call
result <- api$salarypred(10.61928526,'Tier1',12.16838635,166,-999,'zzzzz',-999,71,82603,42,-999,166,-999,945000,-999,-999,0,'MALE')

print(result$output("answer"))

 output=>NULL

result[["errorMessage"]]

#Error Message as below
"Error in predict.gbm(new_model, newdata, type = "response", n.trees = 2722) : could not find function "predict.gbm"answer could not be returned."
...