将机器学习模型转换成Java、C等

这个模块我测试了,可以使用,可得到一个目标类型语言的代码,但是跟原来的结果(输出的score值)不太一样,后来经过自己修改,输出的模型可以使用,我的机器学习模型是目标检测。

m2cgen (Model 2 Code Generator) - is a lightweight library which provides an easy way to transpile trained statistical models into a native code (Python, C, Java, Go).

  1. Installation
  2. Supported Languages
  3. Supported Models
  4. Classification Output
  5. Usage

Installation

Supported Python version is >= 3.4.

pip install m2cgen

Supported Languages

Python
Java
C
Go

Classification Output

Linear/Linear SVM

Binary
Scalar value; signed distance of the sample to the hyperplane for the second class.

Multiclass
Vector value; signed distance of the sample to the hyperplane per each class.

Comment
The output is consistent with the output of LinearClassifierMixin.decision_function.

SVM

Binary
Scalar value; signed distance of the sample to the hyperplane for the second class.

Multiclass
Vector value; one-vs-one score for each class, shape (n_samples, n_classes * (n_classes-1) / 2).

Comment
The output is consistent with the output of BaseSVC.decision_function when the decision_function_shape is set to ovo.

Tree/Random Forest/XGBoost/LightGBM

Binary
Vector value; class probabilities.

Multiclass
Vector value; class probabilities.

Comment
The output is consistent with the output of the predict_proba method of DecisionTreeClassifier/ForestClassifier/XGBClassifier/LGBMClassifier.

Usage

Here’s a simple example of how a linear model trained in Python environment can be represented in Java code:

from sklearn.datasets import load_boston
from sklearn import linear_model
import m2cgen as m2c

boston = load_boston()
X, y = boston.data, boston.target

estimator = linear_model.LinearRegression()
estimator.fit(X, y)

code = m2c.export_to_java(estimator)

output of java code:

public class Model {

    public static double score(double[] input) {
        return (((((((((((((36.45948838508965) + ((input[0]) * (-0.10801135783679647))) + ((input[1]) * (0.04642045836688297))) + ((input[2]) * (0.020558626367073608))) + ((input[3]) * (2.6867338193449406))) + ((input[4]) * (-17.76661122830004))) + ((input[5]) * (3.8098652068092163))) + ((input[6]) * (0.0006922246403454562))) + ((input[7]) * (-1.475566845600257))) + ((input[8]) * (0.30604947898516943))) + ((input[9]) * (-0.012334593916574394))) + ((input[10]) * (-0.9527472317072884))) + ((input[11]) * (0.009311683273794044))) + ((input[12]) * (-0.5247583778554867));
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值