forked from nok/sklearn-porter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasics_embedded.pct.py
144 lines (126 loc) · 3.87 KB
/
basics_embedded.pct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# %% [markdown]
# # sklearn-porter
#
# Repository: [https://github.com/nok/sklearn-porter](https://github.com/nok/sklearn-porter)
#
# ## DecisionTreeClassifier
#
# Documentation: [sklearn.tree.DecisionTreeClassifier](http://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html)
# %%
import sys
sys.path.append('../../../../..')
# %% [markdown]
# ### Load data
# %%
from sklearn.datasets import load_iris
iris_data = load_iris()
X = iris_data.data
y = iris_data.target
print(X.shape, y.shape)
# %% [markdown]
# ### Train classifier
# %%
from sklearn.tree import tree
clf = tree.DecisionTreeClassifier()
clf.fit(X, y)
# %% [markdown]
# ### Transpile classifier
# %%
from sklearn_porter import Porter
porter = Porter(clf, language='java')
output = porter.export(embed_data=True)
print(output)
# class DecisionTreeClassifier {
#
# private static int findMax(int[] nums) {
# int index = 0;
# for (int i = 0; i < nums.length; i++) {
# index = nums[i] > nums[index] ? i : index;
# }
# return index;
# }
#
# public static int predict(double[] features) {
# int[] classes = new int[3];
#
# if (features[3] <= 0.800000011920929) {
# classes[0] = 50;
# classes[1] = 0;
# classes[2] = 0;
# } else {
# if (features[3] <= 1.75) {
# if (features[2] <= 4.950000047683716) {
# if (features[3] <= 1.6500000357627869) {
# classes[0] = 0;
# classes[1] = 47;
# classes[2] = 0;
# } else {
# classes[0] = 0;
# classes[1] = 0;
# classes[2] = 1;
# }
# } else {
# if (features[3] <= 1.550000011920929) {
# classes[0] = 0;
# classes[1] = 0;
# classes[2] = 3;
# } else {
# if (features[2] <= 5.450000047683716) {
# classes[0] = 0;
# classes[1] = 2;
# classes[2] = 0;
# } else {
# classes[0] = 0;
# classes[1] = 0;
# classes[2] = 1;
# }
# }
# }
# } else {
# if (features[2] <= 4.8500001430511475) {
# if (features[0] <= 5.950000047683716) {
# classes[0] = 0;
# classes[1] = 1;
# classes[2] = 0;
# } else {
# classes[0] = 0;
# classes[1] = 0;
# classes[2] = 2;
# }
# } else {
# classes[0] = 0;
# classes[1] = 0;
# classes[2] = 43;
# }
# }
# }
#
# return findMax(classes);
# }
#
# public static void main(String[] args) {
# if (args.length == 4) {
#
# // Features:
# double[] features = new double[args.length];
# for (int i = 0, l = args.length; i < l; i++) {
# features[i] = Double.parseDouble(args[i]);
# }
#
# // Prediction:
# int prediction = DecisionTreeClassifier.predict(features);
# System.out.println(prediction);
#
# }
# }
# }
# %% [markdown]
# ### Run classification in Java
# %%
# Save classifier:
# with open('DecisionTreeClassifier.java', 'w') as f:
# f.write(output)
# Compile model:
# $ javac -cp . DecisionTreeClassifier.java
# Run classification:
# $ java DecisionTreeClassifier 1 2 3 4