-
Notifications
You must be signed in to change notification settings - Fork 169
/
basics_embedded.py
104 lines (90 loc) · 2.93 KB
/
basics_embedded.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
# -*- coding: utf-8 -*-
from sklearn.tree import tree
from sklearn.datasets import load_iris
from sklearn_porter import Porter
iris_data = load_iris()
X = iris_data.data
y = iris_data.target
clf = tree.DecisionTreeClassifier()
clf.fit(X, y)
porter = Porter(clf)
output = porter.export(embed_data=True)
print(output)
"""
class DecisionTreeClassifier {
public static int predict(double[] atts) {
int[] classes = new int[3];
if (atts[2] <= 2.45000004768) {
classes[0] = 50;
classes[1] = 0;
classes[2] = 0;
} else {
if (atts[3] <= 1.75) {
if (atts[2] <= 4.94999980927) {
if (atts[3] <= 1.65000009537) {
classes[0] = 0;
classes[1] = 47;
classes[2] = 0;
} else {
classes[0] = 0;
classes[1] = 0;
classes[2] = 1;
}
} else {
if (atts[3] <= 1.54999995232) {
classes[0] = 0;
classes[1] = 0;
classes[2] = 3;
} else {
if (atts[2] <= 5.44999980927) {
classes[0] = 0;
classes[1] = 2;
classes[2] = 0;
} else {
classes[0] = 0;
classes[1] = 0;
classes[2] = 1;
}
}
}
} else {
if (atts[2] <= 4.85000038147) {
if (atts[0] <= 5.94999980927) {
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);
}
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 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);
}
}
}
"""