【问题标题】:save xgb model trained using caret to database将使用插入符号训练的 xgb 模型保存到数据库
【发布时间】:2018-01-23 06:46:44
【问题描述】:

我使用带有 traincontrol 的 caret 包训练了一个 xgb 模型。在将模型保存到本地 SQLite 数据库之前,我序列化并转换为字符。我能够保存它。但是在从数据库中检索它并对其进行反序列化时,它会引发错误 unserialize(charToRaw(xgbModel))

反序列化错误(charToRaw(selected_model$Model)): ReadItem: 未知类型 57,可能是更高版本的 R 写的

【问题讨论】:

    标签: r sqlite r-caret xgboost


    【解决方案1】:

    要保存由 caret 训练的 xgboost 模型,您需要保存为 raw。这是一个例子:

    library(mlbench) #for the data set
    library(caret)
    library(xgboost) 
    
    data(Sonar)  
    
    caret_model <- train(x = Sonar[,1:60],
                         y = Sonar$Class,
                         method = "xgbTree",
                         trControl = trainControl(method = "cv", number = 2),
                         tuneLength = 2)
    

    保存原始模型:

    raw_model <- xgb.save.raw(caret_model$finalModel)
    

    对其进行序列化并将其保存到本地数据库。当您反序列化时:

    caret_model2 <- xgb.load(raw_model)
    
    preds <- predict(caret_model2, as.matrix(Sonar[,1:60]))
    
    head(preds)
    #output
    0.08729284 0.04738589 0.05628103 0.04275921 0.02574497 0.00655277
    

    那么你可以使用caret_model2来预测。

    这在“保存和加载模型”下提到了here

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 2019-10-01
      • 2019-02-09
      • 1970-01-01
      • 2021-09-26
      • 2015-12-30
      • 2015-09-17
      • 2017-02-02
      • 2022-06-10
      相关资源
      最近更新 更多