Skip to content

Commit

Permalink
Add C# code generator (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
StrikerRUS authored and izeigerman committed Nov 30, 2019
1 parent 916552e commit 5ae584d
Show file tree
Hide file tree
Showing 27 changed files with 1,138 additions and 7 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Python Versions](https://img.shields.io/pypi/pyversions/m2cgen.svg?logo=python&logoColor=white)](https://pypi.org/project/m2cgen)
[![PyPI Version](https://img.shields.io/pypi/v/m2cgen.svg?logo=pypi&logoColor=white)](https://pypi.org/project/m2cgen)

**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, JavaScript, Visual Basic).
**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, JavaScript, Visual Basic, C#).

* [Installation](#installation)
* [Supported Languages](#supported-languages)
Expand All @@ -26,6 +26,7 @@ pip install m2cgen
## Supported Languages

- C
- C#
- Go
- Java
- JavaScript
Expand Down Expand Up @@ -100,8 +101,8 @@ public class Model {

`m2cgen` can be used as a CLI tool to generate code using serialized model objects (pickle protocol):
```
$ m2cgen <pickle_file> --language <language> [--indent <indent>]
[--class_name <class_name>] [--module_name <module_name>] [--package_name <package_name>]
$ m2cgen <pickle_file> --language <language> [--indent <indent>] [--class_name <class_name>]
[--module_name <module_name>] [--package_name <package_name>] [--namespace <namespace>]
[--recursion-limit <recursion_limit>]
```

Expand Down
25 changes: 25 additions & 0 deletions generated_code_examples/c_sharp/classification/decision_tree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace ML {
public static class Model {
public static double[] Score(double[] input) {
double[] var0;
if ((input[2]) <= (2.6)) {
var0 = new double[3] {1.0, 0.0, 0.0};
} else {
if ((input[2]) <= (4.8500004)) {
if ((input[3]) <= (1.6500001)) {
var0 = new double[3] {0.0, 1.0, 0.0};
} else {
var0 = new double[3] {0.0, 0.3333333333333333, 0.6666666666666666};
}
} else {
if ((input[3]) <= (1.75)) {
var0 = new double[3] {0.0, 0.42857142857142855, 0.5714285714285714};
} else {
var0 = new double[3] {0.0, 0.0, 1.0};
}
}
}
return var0;
}
}
}
84 changes: 84 additions & 0 deletions generated_code_examples/c_sharp/classification/lightgbm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using static System.Math;
namespace ML {
public static class Model {
public static double[] Score(double[] input) {
double var0;
if ((input[2]) > (1.8)) {
if ((input[2]) > (4.250000000000001)) {
var0 = -1.1736122903444903;
} else {
var0 = -1.1633850173886202;
}
} else {
var0 = -0.9486122853153485;
}
double var1;
if ((input[2]) > (1.8)) {
if ((input[1]) > (3.0500000000000003)) {
var1 = -0.06193194743580539;
} else {
var1 = -0.07237070828653688;
}
} else {
var1 = 0.12984943093573026;
}
double var2;
var2 = Exp(((0) + (var0)) + (var1));
double var3;
if ((input[2]) > (1.8)) {
if ((input[2]) > (4.8500000000000005)) {
var3 = -1.1807342692411888;
} else {
var3 = -0.9831932134295853;
}
} else {
var3 = -1.1952609652674462;
}
double var4;
if ((input[2]) > (1.8)) {
if ((input[2]) > (4.8500000000000005)) {
var4 = -0.05694282927518771;
} else {
var4 = 0.11960489254350348;
}
} else {
var4 = -0.07151978915296087;
}
double var5;
var5 = Exp(((0) + (var3)) + (var4));
double var6;
if ((input[2]) > (4.8500000000000005)) {
if ((input[3]) > (1.9500000000000002)) {
var6 = -0.9298942558407184;
} else {
var6 = -0.9632815288936335;
}
} else {
if ((input[2]) > (4.250000000000001)) {
var6 = -1.1322413652523249;
} else {
var6 = -1.1524760761934856;
}
}
double var7;
if ((input[2]) > (4.8500000000000005)) {
if ((input[3]) > (1.9500000000000002)) {
var7 = 0.12809276954555665;
} else {
var7 = 0.09898817876916756;
}
} else {
if ((input[2]) > (4.250000000000001)) {
var7 = -0.052710589717642864;
} else {
var7 = -0.07292857712854424;
}
}
double var8;
var8 = Exp(((0) + (var6)) + (var7));
double var9;
var9 = ((var2) + (var5)) + (var8);
return new double[3] {(var2) / (var9), (var5) / (var9), (var8) / (var9)};
}
}
}
7 changes: 7 additions & 0 deletions generated_code_examples/c_sharp/classification/linear.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ML {
public static class Model {
public static double[] Score(double[] input) {
return new double[3] {((((0.2614587435880605) + ((input[0]) * (0.42474116053569605))) + ((input[1]) * (1.3963906033045026))) + ((input[2]) * (-2.215054318516674))) + ((input[3]) * (-0.9587396176450289)), ((((1.1348839223808307) + ((input[0]) * (0.2567965976997648))) + ((input[1]) * (-1.3904789369836008))) + ((input[2]) * (0.596683023311173))) + ((input[3]) * (-1.2690022726388828)), ((((-1.2162802012560197) + ((input[0]) * (-1.6357766989177105))) + ((input[1]) * (-1.5040638728422817))) + ((input[2]) * (2.427835933129272))) + ((input[3]) * (2.3469310693367276))};
}
}
}
57 changes: 57 additions & 0 deletions generated_code_examples/c_sharp/classification/random_forest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
namespace ML {
public static class Model {
public static double[] Score(double[] input) {
double[] var0;
if ((input[3]) <= (0.8)) {
var0 = new double[3] {1.0, 0.0, 0.0};
} else {
if ((input[2]) <= (4.8500004)) {
var0 = new double[3] {0.0, 0.9795918367346939, 0.02040816326530612};
} else {
if ((input[3]) <= (1.75)) {
if ((input[3]) <= (1.6500001)) {
var0 = new double[3] {0.0, 0.25, 0.75};
} else {
var0 = new double[3] {0.0, 1.0, 0.0};
}
} else {
var0 = new double[3] {0.0, 0.0, 1.0};
}
}
}
double[] var1;
if ((input[3]) <= (0.8)) {
var1 = new double[3] {1.0, 0.0, 0.0};
} else {
if ((input[0]) <= (6.05)) {
if ((input[2]) <= (4.9)) {
var1 = new double[3] {0.0, 0.9032258064516129, 0.0967741935483871};
} else {
var1 = new double[3] {0.0, 0.0, 1.0};
}
} else {
if ((input[3]) <= (1.75)) {
var1 = new double[3] {0.0, 0.8, 0.2};
} else {
var1 = new double[3] {0.0, 0.0, 1.0};
}
}
}
return AddVectors(MulVectorNumber(var0, 0.5), MulVectorNumber(var1, 0.5));
}
private static double[] AddVectors(double[] v1, double[] v2) {
double[] result = new double[v1.Length];
for (int i = 0; i < v1.Length; ++i) {
result[i] = v1[i] + v2[i];
}
return result;
}
private static double[] MulVectorNumber(double[] v1, double num) {
double[] result = new double[v1.Length];
for (int i = 0; i < v1.Length; ++i) {
result[i] = v1[i] * num;
}
return result;
}
}
}
74 changes: 74 additions & 0 deletions generated_code_examples/c_sharp/classification/svm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using static System.Math;
namespace ML {
public static class Model {
public static double[] Score(double[] input) {
double var0;
var0 = (0) - (0.25);
double var1;
var1 = Exp((var0) * ((((Pow((5.4) - (input[0]), 2)) + (Pow((3.0) - (input[1]), 2))) + (Pow((4.5) - (input[2]), 2))) + (Pow((1.5) - (input[3]), 2))));
double var2;
var2 = Exp((var0) * ((((Pow((6.2) - (input[0]), 2)) + (Pow((2.2) - (input[1]), 2))) + (Pow((4.5) - (input[2]), 2))) + (Pow((1.5) - (input[3]), 2))));
double var3;
var3 = Exp((var0) * ((((Pow((5.0) - (input[0]), 2)) + (Pow((2.3) - (input[1]), 2))) + (Pow((3.3) - (input[2]), 2))) + (Pow((1.0) - (input[3]), 2))));
double var4;
var4 = Exp((var0) * ((((Pow((5.9) - (input[0]), 2)) + (Pow((3.2) - (input[1]), 2))) + (Pow((4.8) - (input[2]), 2))) + (Pow((1.8) - (input[3]), 2))));
double var5;
var5 = Exp((var0) * ((((Pow((5.0) - (input[0]), 2)) + (Pow((2.0) - (input[1]), 2))) + (Pow((3.5) - (input[2]), 2))) + (Pow((1.0) - (input[3]), 2))));
double var6;
var6 = Exp((var0) * ((((Pow((6.7) - (input[0]), 2)) + (Pow((3.0) - (input[1]), 2))) + (Pow((5.0) - (input[2]), 2))) + (Pow((1.7) - (input[3]), 2))));
double var7;
var7 = Exp((var0) * ((((Pow((7.0) - (input[0]), 2)) + (Pow((3.2) - (input[1]), 2))) + (Pow((4.7) - (input[2]), 2))) + (Pow((1.4) - (input[3]), 2))));
double var8;
var8 = Exp((var0) * ((((Pow((4.9) - (input[0]), 2)) + (Pow((2.4) - (input[1]), 2))) + (Pow((3.3) - (input[2]), 2))) + (Pow((1.0) - (input[3]), 2))));
double var9;
var9 = Exp((var0) * ((((Pow((6.3) - (input[0]), 2)) + (Pow((2.5) - (input[1]), 2))) + (Pow((4.9) - (input[2]), 2))) + (Pow((1.5) - (input[3]), 2))));
double var10;
var10 = Exp((var0) * ((((Pow((6.0) - (input[0]), 2)) + (Pow((2.7) - (input[1]), 2))) + (Pow((5.1) - (input[2]), 2))) + (Pow((1.6) - (input[3]), 2))));
double var11;
var11 = Exp((var0) * ((((Pow((5.7) - (input[0]), 2)) + (Pow((2.6) - (input[1]), 2))) + (Pow((3.5) - (input[2]), 2))) + (Pow((1.0) - (input[3]), 2))));
double var12;
var12 = Exp((var0) * ((((Pow((5.1) - (input[0]), 2)) + (Pow((3.8) - (input[1]), 2))) + (Pow((1.9) - (input[2]), 2))) + (Pow((0.4) - (input[3]), 2))));
double var13;
var13 = Exp((var0) * ((((Pow((4.4) - (input[0]), 2)) + (Pow((2.9) - (input[1]), 2))) + (Pow((1.4) - (input[2]), 2))) + (Pow((0.2) - (input[3]), 2))));
double var14;
var14 = Exp((var0) * ((((Pow((5.7) - (input[0]), 2)) + (Pow((4.4) - (input[1]), 2))) + (Pow((1.5) - (input[2]), 2))) + (Pow((0.4) - (input[3]), 2))));
double var15;
var15 = Exp((var0) * ((((Pow((5.8) - (input[0]), 2)) + (Pow((4.0) - (input[1]), 2))) + (Pow((1.2) - (input[2]), 2))) + (Pow((0.2) - (input[3]), 2))));
double var16;
var16 = Exp((var0) * ((((Pow((5.1) - (input[0]), 2)) + (Pow((3.3) - (input[1]), 2))) + (Pow((1.7) - (input[2]), 2))) + (Pow((0.5) - (input[3]), 2))));
double var17;
var17 = Exp((var0) * ((((Pow((5.7) - (input[0]), 2)) + (Pow((3.8) - (input[1]), 2))) + (Pow((1.7) - (input[2]), 2))) + (Pow((0.3) - (input[3]), 2))));
double var18;
var18 = Exp((var0) * ((((Pow((4.3) - (input[0]), 2)) + (Pow((3.0) - (input[1]), 2))) + (Pow((1.1) - (input[2]), 2))) + (Pow((0.1) - (input[3]), 2))));
double var19;
var19 = Exp((var0) * ((((Pow((4.5) - (input[0]), 2)) + (Pow((2.3) - (input[1]), 2))) + (Pow((1.3) - (input[2]), 2))) + (Pow((0.3) - (input[3]), 2))));
double var20;
var20 = Exp((var0) * ((((Pow((6.3) - (input[0]), 2)) + (Pow((2.7) - (input[1]), 2))) + (Pow((4.9) - (input[2]), 2))) + (Pow((1.8) - (input[3]), 2))));
double var21;
var21 = Exp((var0) * ((((Pow((6.0) - (input[0]), 2)) + (Pow((3.0) - (input[1]), 2))) + (Pow((4.8) - (input[2]), 2))) + (Pow((1.8) - (input[3]), 2))));
double var22;
var22 = Exp((var0) * ((((Pow((6.3) - (input[0]), 2)) + (Pow((2.8) - (input[1]), 2))) + (Pow((5.1) - (input[2]), 2))) + (Pow((1.5) - (input[3]), 2))));
double var23;
var23 = Exp((var0) * ((((Pow((5.8) - (input[0]), 2)) + (Pow((2.8) - (input[1]), 2))) + (Pow((5.1) - (input[2]), 2))) + (Pow((2.4) - (input[3]), 2))));
double var24;
var24 = Exp((var0) * ((((Pow((6.1) - (input[0]), 2)) + (Pow((3.0) - (input[1]), 2))) + (Pow((4.9) - (input[2]), 2))) + (Pow((1.8) - (input[3]), 2))));
double var25;
var25 = Exp((var0) * ((((Pow((7.7) - (input[0]), 2)) + (Pow((2.6) - (input[1]), 2))) + (Pow((6.9) - (input[2]), 2))) + (Pow((2.3) - (input[3]), 2))));
double var26;
var26 = Exp((var0) * ((((Pow((6.9) - (input[0]), 2)) + (Pow((3.1) - (input[1]), 2))) + (Pow((5.1) - (input[2]), 2))) + (Pow((2.3) - (input[3]), 2))));
double var27;
var27 = Exp((var0) * ((((Pow((6.3) - (input[0]), 2)) + (Pow((3.3) - (input[1]), 2))) + (Pow((6.0) - (input[2]), 2))) + (Pow((2.5) - (input[3]), 2))));
double var28;
var28 = Exp((var0) * ((((Pow((4.9) - (input[0]), 2)) + (Pow((2.5) - (input[1]), 2))) + (Pow((4.5) - (input[2]), 2))) + (Pow((1.7) - (input[3]), 2))));
double var29;
var29 = Exp((var0) * ((((Pow((6.0) - (input[0]), 2)) + (Pow((2.2) - (input[1]), 2))) + (Pow((5.0) - (input[2]), 2))) + (Pow((1.5) - (input[3]), 2))));
double var30;
var30 = Exp((var0) * ((((Pow((7.9) - (input[0]), 2)) + (Pow((3.8) - (input[1]), 2))) + (Pow((6.4) - (input[2]), 2))) + (Pow((2.0) - (input[3]), 2))));
double var31;
var31 = Exp((var0) * ((((Pow((7.2) - (input[0]), 2)) + (Pow((3.0) - (input[1]), 2))) + (Pow((5.8) - (input[2]), 2))) + (Pow((1.6) - (input[3]), 2))));
double var32;
var32 = Exp((var0) * ((((Pow((7.7) - (input[0]), 2)) + (Pow((3.8) - (input[1]), 2))) + (Pow((6.7) - (input[2]), 2))) + (Pow((2.2) - (input[3]), 2))));
return new double[3] {(((((((((((((((((((-0.08359187780790468) + ((var1) * (-0.0))) + ((var2) * (-0.0))) + ((var3) * (-0.4393498355605194))) + ((var4) * (-0.009465620856664334))) + ((var5) * (-0.16223369966927))) + ((var6) * (-0.26861888775075243))) + ((var7) * (-0.4393498355605194))) + ((var8) * (-0.4393498355605194))) + ((var9) * (-0.0))) + ((var10) * (-0.0))) + ((var11) * (-0.19673905328606292))) + ((var12) * (0.3340655283922188))) + ((var13) * (0.3435087305152051))) + ((var14) * (0.4393498355605194))) + ((var15) * (0.0))) + ((var16) * (0.28614124535416424))) + ((var17) * (0.11269159286168087))) + ((var18) * (0.0))) + ((var19) * (0.4393498355605194)), (((((((((((((((((((((-0.18563912331454907) + ((var20) * (-0.0))) + ((var21) * (-0.06014273244194299))) + ((var22) * (-0.0))) + ((var23) * (-0.031132453078851926))) + ((var24) * (-0.0))) + ((var25) * (-0.3893079321588921))) + ((var26) * (-0.06738007627290196))) + ((var27) * (-0.1225075748937126))) + ((var28) * (-0.3893079321588921))) + ((var29) * (-0.29402231709614085))) + ((var30) * (-0.3893079321588921))) + ((var31) * (-0.0))) + ((var32) * (-0.028242141062729226))) + ((var12) * (0.16634667752431267))) + ((var13) * (0.047772685163074764))) + ((var14) * (0.3893079321588921))) + ((var15) * (0.3893079321588921))) + ((var16) * (0.0))) + ((var17) * (0.0))) + ((var18) * (0.3893079321588921))) + ((var19) * (0.3893079321588921)), ((((((((((((((((((((((((0.5566649875797668) + ((var20) * (-25.563066587228416))) + ((var21) * (-38.35628154976547))) + ((var22) * (-38.35628154976547))) + ((var23) * (-0.0))) + ((var24) * (-38.35628154976547))) + ((var25) * (-0.0))) + ((var26) * (-0.0))) + ((var27) * (-0.0))) + ((var28) * (-6.2260303727828745))) + ((var29) * (-18.42781911624364))) + ((var30) * (-0.14775026537286423))) + ((var31) * (-7.169755983020096))) + ((var32) * (-0.0))) + ((var1) * (12.612328267927264))) + ((var2) * (6.565812506955159))) + ((var3) * (0.0))) + ((var4) * (38.35628154976547))) + ((var5) * (0.0))) + ((var6) * (38.35628154976547))) + ((var7) * (0.0))) + ((var8) * (0.0))) + ((var9) * (38.35628154976547))) + ((var10) * (38.35628154976547))) + ((var11) * (0.0))};
}
}
}
76 changes: 76 additions & 0 deletions generated_code_examples/c_sharp/classification/xgboost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using static System.Math;
namespace ML {
public static class Model {
public static double[] Score(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;
}
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.0552999265;
} 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.124653697;
} 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);
return new double[3] {(var2) / (var9), (var5) / (var9), (var8) / (var9)};
}
}
}
25 changes: 25 additions & 0 deletions generated_code_examples/c_sharp/regression/decision_tree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace ML {
public static class Model {
public static double Score(double[] input) {
double var0;
if ((input[5]) <= (6.941)) {
if ((input[12]) <= (14.395)) {
if ((input[7]) <= (1.43365)) {
var0 = 45.58;
} else {
var0 = 22.865022421524642;
}
} else {
var0 = 14.924358974358983;
}
} else {
if ((input[5]) <= (7.4370003)) {
var0 = 32.09534883720931;
} else {
var0 = 45.275;
}
}
return var0;
}
}
}
Loading

0 comments on commit 5ae584d

Please sign in to comment.