Skip to content

Commit

Permalink
Merge 1471dfc into b0a0889
Browse files Browse the repository at this point in the history
  • Loading branch information
izeigerman committed Feb 11, 2019
2 parents b0a0889 + 1471dfc commit 93f7444
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 4 deletions.
36 changes: 32 additions & 4 deletions README.md
Expand Up @@ -6,6 +6,13 @@

**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).

## Installation

```
pip install m2cgen
```


## Supported languages

- Python
Expand Down Expand Up @@ -50,11 +57,32 @@
</tbody>
</table>

## Installation

```
pip install m2cgen
```
## Classification Output
<table>
<thead>
<tr>
<th width="10%"></th>
<th width="35%">Binary</th>
<th width="35%">Multiclass</th>
<th width="20%">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<th>Linear</th>
<td>Scalar value; signed distance of the sample to the hyperplane for the second class </td>
<td>Vector value; signed distance of the sample to the hyperplane per each class</td>
<td>The output is consistent with the output of `LinearClassifierMixin.decision_function`</td>
</tr>
<tr>
<th>Tree/Random Forest/XGBoost</th>
<td>Vector value; class probabilities</td>
<td>Vector value; class probabilities</td>
<td>The output is consistent with the output of the `predict_proba` method of `DecisionTreeClassifier`/`ForestClassifier`/`XGBClassifier`</td>
</tr>
</tbody>
</table>

## Usage

Expand Down
76 changes: 76 additions & 0 deletions generated_code_examples/c/classification/xgboost.c
@@ -0,0 +1,76 @@
#include <math.h>
void assign_array(double source[], double *target, int size) {
for(int i = 0; i < size; ++i)
target[i] = source[i];
}
void score(double * input, double * output) {
double var0;
if ((input[2]) >= (2.5999999)) {
var0 = -0.0731707439;
} else {
var0 = 0.142857149;
}
double var1;
if ((input[2]) >= (2.5999999)) {
var1 = -0.0705206916;
} else {
var1 = 0.12477719;
}
double var2;
var2 = exp(((0.5) + (var0)) + (var1));
double var3;
if ((input[2]) >= (2.5999999)) {
if ((input[2]) >= (4.85000038)) {
var3 = -0.0578680299;
} else {
var3 = 0.132596686;
}
} else {
var3 = -0.0714285821;
}
double var4;
if ((input[2]) >= (2.5999999)) {
if ((input[2]) >= (4.85000038)) {
var4 = -0.0552999191;
} else {
var4 = 0.116139404;
}
} else {
var4 = -0.0687687024;
}
double var5;
var5 = exp(((0.5) + (var3)) + (var4));
double var6;
if ((input[2]) >= (4.85000038)) {
if ((input[3]) >= (1.75)) {
var6 = 0.142011836;
} else {
var6 = 0.0405405387;
}
} else {
if ((input[3]) >= (1.6500001)) {
var6 = 0.0428571403;
} else {
var6 = -0.0730659068;
}
}
double var7;
if ((input[2]) >= (4.85000038)) {
if ((input[3]) >= (1.75)) {
var7 = 0.124653712;
} else {
var7 = 0.035562478;
}
} else {
if ((input[3]) >= (1.6500001)) {
var7 = 0.0425687581;
} else {
var7 = -0.0704230517;
}
}
double var8;
var8 = exp(((0.5) + (var6)) + (var7));
double var9;
var9 = ((var2) + (var5)) + (var8);
assign_array((double[]){(var2) / (var9), (var5) / (var9), (var8) / (var9)}, output, 3);
}
31 changes: 31 additions & 0 deletions generated_code_examples/c/regression/xgboost.c
@@ -0,0 +1,31 @@
double score(double * input) {
double var0;
if ((input[12]) >= (9.72500038)) {
if ((input[12]) >= (19.8299999)) {
var0 = 1.1551429;
} else {
var0 = 1.8613131;
}
} else {
if ((input[5]) >= (6.94099998)) {
var0 = 3.75848508;
} else {
var0 = 2.48056006;
}
}
double var1;
if ((input[12]) >= (7.68499994)) {
if ((input[12]) >= (15)) {
var1 = 1.24537706;
} else {
var1 = 1.92129695;
}
} else {
if ((input[5]) >= (7.43700027)) {
var1 = 3.96021533;
} else {
var1 = 2.51493931;
}
}
return ((0.5) + (var0)) + (var1);
}
83 changes: 83 additions & 0 deletions generated_code_examples/java/classification/xgboost.java
@@ -0,0 +1,83 @@
public class Model {

public static double[] score(double[] input) {
double var0;
var0 = Math.exp(subroutine0(input));
double var1;
var1 = Math.exp(subroutine1(input));
double var2;
var2 = Math.exp(subroutine2(input));
double var3;
var3 = ((var0) + (var1)) + (var2);
return new double[] {(var0) / (var3), (var1) / (var3), (var2) / (var3)};
}
public static double subroutine0(double[] input) {
double var0;
if ((input[2]) >= (2.5999999)) {
var0 = -0.0731707439;
} else {
var0 = 0.142857149;
}
double var1;
if ((input[2]) >= (2.5999999)) {
var1 = -0.0705206916;
} else {
var1 = 0.12477719;
}
return ((0.5) + (var0)) + (var1);
}
public static double subroutine1(double[] input) {
double var0;
if ((input[2]) >= (2.5999999)) {
if ((input[2]) >= (4.85000038)) {
var0 = -0.0578680299;
} else {
var0 = 0.132596686;
}
} else {
var0 = -0.0714285821;
}
double var1;
if ((input[2]) >= (2.5999999)) {
if ((input[2]) >= (4.85000038)) {
var1 = -0.0552999191;
} else {
var1 = 0.116139404;
}
} else {
var1 = -0.0687687024;
}
return ((0.5) + (var0)) + (var1);
}
public static double subroutine2(double[] input) {
double var0;
if ((input[2]) >= (4.85000038)) {
if ((input[3]) >= (1.75)) {
var0 = 0.142011836;
} else {
var0 = 0.0405405387;
}
} else {
if ((input[3]) >= (1.6500001)) {
var0 = 0.0428571403;
} else {
var0 = -0.0730659068;
}
}
double var1;
if ((input[2]) >= (4.85000038)) {
if ((input[3]) >= (1.75)) {
var1 = 0.124653712;
} else {
var1 = 0.035562478;
}
} else {
if ((input[3]) >= (1.6500001)) {
var1 = 0.0425687581;
} else {
var1 = -0.0704230517;
}
}
return ((0.5) + (var0)) + (var1);
}
}
37 changes: 37 additions & 0 deletions generated_code_examples/java/regression/xgboost.java
@@ -0,0 +1,37 @@
public class Model {

public static double score(double[] input) {
return subroutine0(input);
}
public static double subroutine0(double[] input) {
double var0;
if ((input[12]) >= (9.72500038)) {
if ((input[12]) >= (19.8299999)) {
var0 = 1.1551429;
} else {
var0 = 1.8613131;
}
} else {
if ((input[5]) >= (6.94099998)) {
var0 = 3.75848508;
} else {
var0 = 2.48056006;
}
}
double var1;
if ((input[12]) >= (7.68499994)) {
if ((input[12]) >= (15)) {
var1 = 1.24537706;
} else {
var1 = 1.92129695;
}
} else {
if ((input[5]) >= (7.43700027)) {
var1 = 3.96021533;
} else {
var1 = 2.51493931;
}
}
return ((0.5) + (var0)) + (var1);
}
}
49 changes: 49 additions & 0 deletions generated_code_examples/python/classification/xgboost.py
@@ -0,0 +1,49 @@
import numpy as np
def score(input):
if (input[2]) >= (2.5999999):
var0 = -0.0731707439
else:
var0 = 0.142857149
if (input[2]) >= (2.5999999):
var1 = -0.0705206916
else:
var1 = 0.12477719
var2 = np.exp(((0.5) + (var0)) + (var1))
if (input[2]) >= (2.5999999):
if (input[2]) >= (4.85000038):
var3 = -0.0578680299
else:
var3 = 0.132596686
else:
var3 = -0.0714285821
if (input[2]) >= (2.5999999):
if (input[2]) >= (4.85000038):
var4 = -0.0552999191
else:
var4 = 0.116139404
else:
var4 = -0.0687687024
var5 = np.exp(((0.5) + (var3)) + (var4))
if (input[2]) >= (4.85000038):
if (input[3]) >= (1.75):
var6 = 0.142011836
else:
var6 = 0.0405405387
else:
if (input[3]) >= (1.6500001):
var6 = 0.0428571403
else:
var6 = -0.0730659068
if (input[2]) >= (4.85000038):
if (input[3]) >= (1.75):
var7 = 0.124653712
else:
var7 = 0.035562478
else:
if (input[3]) >= (1.6500001):
var7 = 0.0425687581
else:
var7 = -0.0704230517
var8 = np.exp(((0.5) + (var6)) + (var7))
var9 = ((var2) + (var5)) + (var8)
return np.asarray([(var2) / (var9), (var5) / (var9), (var8) / (var9)])
22 changes: 22 additions & 0 deletions generated_code_examples/python/regression/xgboost.py
@@ -0,0 +1,22 @@
def score(input):
if (input[12]) >= (9.72500038):
if (input[12]) >= (19.8299999):
var0 = 1.1551429
else:
var0 = 1.8613131
else:
if (input[5]) >= (6.94099998):
var0 = 3.75848508
else:
var0 = 2.48056006
if (input[12]) >= (7.68499994):
if (input[12]) >= (15):
var1 = 1.24537706
else:
var1 = 1.92129695
else:
if (input[5]) >= (7.43700027):
var1 = 3.96021533
else:
var1 = 2.51493931
return ((0.5) + (var0)) + (var1)
12 changes: 12 additions & 0 deletions tools/generate_code_examples.py
Expand Up @@ -11,6 +11,7 @@
import os
import sys
import itertools
import xgboost
from sklearn import linear_model, tree, ensemble

import m2cgen as m2c
Expand All @@ -21,6 +22,7 @@
TREE_PARAMS = dict(random_state=RANDOM_SEED, max_leaf_nodes=5)
FOREST_PARAMS = dict(
n_estimators=2, random_state=RANDOM_SEED, max_leaf_nodes=5)
XGBOOST_PARAMS = dict(n_estimators=2, random_state=RANDOM_SEED, max_depth=2)


EXAMPLE_LANGUAGES = [
Expand Down Expand Up @@ -60,6 +62,16 @@
ensemble.RandomForestClassifier(**FOREST_PARAMS),
utils.train_model_classification,
),
(
"regression", "xgboost",
xgboost.XGBRegressor(**XGBOOST_PARAMS),
utils.train_model_regression,
),
(
"classification", "xgboost",
xgboost.XGBClassifier(**XGBOOST_PARAMS),
utils.train_model_classification,
),
]


Expand Down

0 comments on commit 93f7444

Please sign in to comment.