Skip to content

Commit

Permalink
Duplicate templates for the ExtraTreesClassifier
Browse files Browse the repository at this point in the history
  • Loading branch information
Darius Morawiec committed Oct 31, 2017
1 parent 407c505 commit a17c77c
Show file tree
Hide file tree
Showing 29 changed files with 129 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
{method}

int main(int argc, const char * argv[]) {{
float atts[argc-1];
float features[argc-1];
int i;
for (i = 1; i < argc; i++) {{
atts[i-1] = atof(argv[i]);
features[i-1] = atof(argv[i]);
}}
printf("%d", {method_name}(atts));
printf("%d", {method_name}(features));
return 0;
}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{methods}
int {method_name} (float atts[]) {{
int {method_name} (float features[]) {{
int n_classes = {n_classes};
int classes[n_classes];
int i;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
classes[{method_name}(features)]++;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
int {method_name}_{method_id}(float atts[]) {{
int {method_name}_{method_id}(float features[]) {{
int classes[{n_classes}];
{tree_branches}
int class_idx = 0;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ class {class_name} {{

public static void main(String[] args) {{
if (args.length == {n_features}) {{
float[] atts = new float[args.length];
float[] features = new float[args.length];
for (int i = 0, l = args.length; i < l; i++) {{
atts[i] = Float.parseFloat(args[i]);
features[i] = Float.parseFloat(args[i]);
}}
System.out.println({class_name}.{method_name}(atts));
System.out.println({class_name}.{method_name}(features));
}}
}}
}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{methods}
public static int {method_name}(float[] atts) {{
public static int {method_name}(float[] features) {{
int n_classes = {n_classes};
int[] classes = new int[n_classes];
{method_calls}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
classes[{class_name}.{method_name}(features)]++;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public static int {method_name}_{method_id}(float[] atts) {{
public static int {method_name}_{method_id}(float[] features) {{
int[] classes = new int[{n_classes}];
{tree_branches}
int class_idx = 0;
Expand All @@ -11,4 +11,3 @@ public static int {method_name}_{method_id}(float[] atts) {{
}}
return class_idx;
}}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,66 @@
var {class_name} = function() {{
var {class_name} = function(data) {{

{method}
var Tree = function(lChilds, rChilds, thresholds, indices, classes) {{
this.lChilds = lChilds;
this.rChilds = rChilds;
this.thresholds = thresholds;
this.indices = indices;
this.classes = classes;

this.predict = function(features, node) {{
if (this.thresholds[node] != -2) {{
if (features[this.indices[node]] <= this.thresholds[node]) {{
return this.predict(features, this.lChilds[node]);
}} else {{
return this.predict(features, this.rChilds[node]);
}}
}}
return findMax(this.classes[node]);
}};
}};

var findMax = function(nums) {{
var index = 0;
for (var i = 0; i < nums.length; i++) {{
index = nums[i] > nums[index] ? i : index;
}}
return index;
}};

this.trees = Array();
for (var i = 0; i < data.length; i++) {{
var tree = new Tree(data[i]['left_childs'], data[i]['right_childs'],
data[i]['thresholds'], data[i]['indices'],
data[i]['classes']);
this.trees.push(tree);
}}

this.{method_name} = function(features) {{
var nClasses = this.trees[0].classes.length;
var classes = new Array(nClasses).fill(0);
for (var i = 0; i < trees.length; i++) {{
classes[trees[i].predict(features)]++;
}}
return findMax(classes);
}};

}};

if (typeof process !== 'undefined' && typeof process.argv !== 'undefined') {{
if (process.argv.length - 2 == {n_features}) {{
var argv = process.argv.slice(2);
var prediction = new {class_name}().{method_name}(argv);
if (process.argv.length - 2 === {n_features}) {{

// Features:
var features = process.argv.slice(2);

// Parameters:
var data = [
{model_data}
];

// Prediction:
var clf = new {class_name}(data);
var prediction = clf.{method_name}(features);
console.log(prediction);

}}
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var {class_name} = function() {{

var findMax = function(nums) {{
var index = 0;
for (var i = 0; i < nums.length; i++) {{
index = nums[i] > nums[index] ? i : index;
}}
return index;
}};

var trees = new Array();

{method}

}};

if (typeof process !== 'undefined' && typeof process.argv !== 'undefined') {{
if (process.argv.length - 2 == {n_features}) {{

// Features:
var features = process.argv.slice(2);

// Prediction:
var prediction = new {class_name}().{method_name}(features);
console.log(prediction);

}}
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{methods}
this.{method_name} = function(features) {{
var classes = new Array({n_classes}).fill(0);
for (var i = 0; i < trees.length; i++) {{
classes[trees[i](features)]++;
}}
return findMax(classes);
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
classes[{method_name}(features)]++;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trees.push(function(features) {{
var classes = new Array({n_classes});
{tree_branches}

return findMax(classes);
}});

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{methods}
public static function {method_name}($atts) {{
public static function {method_name}($features) {{
$n_classes = {n_classes};
$classes = array_fill(0, $n_classes, 0);
{method_calls}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$classes[{class_name}::{method_name}($features)]++;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public static function {method_name}_{method_id}($atts) {{
public static function {method_name}_{method_id}($features) {{
$n_classes = {n_classes};
$classes = array_fill(0, $n_classes, 0);
{tree_branches}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class {class_name}

{method}

end

if ARGV.length == {n_features}
features = ARGV.collect {{ |i| i.to_f }}
puts {class_name}.{method_name}(features)
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{methods}
def self.{method_name} (atts)
def self.{method_name} (features)
classes = Array.new({n_classes}, 0)
{method_calls}
pos_max = classes.each_with_index.select {{|e, i| e==classes.max}}.map &:last
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
idx = {class_name}.{method_name}(features); classes[idx] = classes[idx] + 1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def self.{method_name}_{method_id} (atts)
def self.{method_name}_{method_id} (features)
classes = Array.new({n_classes}, 0)
{tree_branches}
pos_max = classes.each_with_index.select {{|e, i| e==classes.max}}.map &:last
Expand Down

This file was deleted.

0 comments on commit a17c77c

Please sign in to comment.