作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用新的 CatBoost 算法(python 版本)并尝试将我的模型导出到 txt 文件以将我的模型传输到 C/Java 实现。查看文档我只找到了 save_model方法只接受两种格式的文件:1. 二进制 2. Apple 的 CoreML
这些格式都不适合我,所以也许有其他方法可以实现?
最佳答案
没有办法直接执行此操作:Catboost 到目前为止不支持模型序列化。
但是,Catboost 已经可以将模型转换为 CoreML,并且有一个 CoreML 工具可以将模型序列化为类似 JSON 的文本。享受最小的例子:
from sklearn import datasets
iris = datasets.load_iris()
import catboost
# the shortest possible model specification
cls = catboost.CatBoostClassifier(loss_function='MultiClass', iterations=1, depth=1)
cls.fit(iris.data, iris.target)
# save model to CoreML format
cls.save_model(
"iris.mlmodel",
format="coreml",
export_parameters={
'prediction_type': 'probability'
}
)
# there is a CoreML tool for model serialization
import coremltools
model = coremltools.models.model.MLModel("iris.mlmodel")
model.get_spec()
您可能需要阅读 coremltools documentation 才能完全理解此代码打印的内容,但您可以像这样阅读输出:“有一棵有 2 个叶子的树的集合 - 在叶子 0 中,类 0 占主导地位,在叶子 1 - 类 1 和 2。转到叶子 1,如果特征 3 大于 0.8,否则转到叶子 0"
specificationVersion: 1
description {
input {
name: "feature_3"
type {
doubleType {
}
}
}
output {
name: "prediction"
type {
multiArrayType {
shape: 3
dataType: DOUBLE
}
}
}
predictedFeatureName: "prediction"
predictedProbabilitiesName: "prediction"
metadata {
shortDescription: "Catboost model"
versionString: "1.0.0"
author: "Mr. Catboost Dumper"
}
}
treeEnsembleRegressor {
treeEnsemble {
nodes {
nodeBehavior: LeafNode
evaluationInfo {
evaluationValue: 0.05084745649058943
}
evaluationInfo {
evaluationIndex: 1
evaluationValue: -0.025423728245294732
}
evaluationInfo {
evaluationIndex: 2
evaluationValue: -0.025423728245294732
}
}
nodes {
nodeId: 1
nodeBehavior: LeafNode
evaluationInfo {
evaluationValue: -0.02752293516463098
}
evaluationInfo {
evaluationIndex: 1
evaluationValue: 0.01376146758231549
}
evaluationInfo {
evaluationIndex: 2
evaluationValue: 0.013761467582315471
}
}
nodes {
nodeId: 2
nodeBehavior: BranchOnValueGreaterThan
branchFeatureIndex: 3
branchFeatureValue: 0.800000011920929
trueChildNodeId: 1
}
numPredictionDimensions: 3
basePredictionValue: 0.0
basePredictionValue: 0.0
basePredictionValue: 0.0
}
postEvaluationTransform: Classification_SoftMax
}
这种方法有一个缺点:CoreML 不支持 Catboost 使用分类特征的方式。因此,如果您想序列化具有分类特征的模型,则需要在训练前对它们进行单热编码。
关于python - 如何将 CatBoost 模型导出为文本以便将来解析为 if-else 决策树?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45455970/
我是一名优秀的程序员,十分优秀!