Skip to content

Commit

Permalink
Add dynamic class name and update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
nok committed Oct 15, 2017
1 parent 59a0e91 commit 710a854
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 119 deletions.

Large diffs are not rendered by default.

Expand Up @@ -21,7 +21,7 @@
"""
import java.util.*;
class Brain {
class KNeighborsClassifier {
private int nNeighbors;
private int nTemplates;
Expand All @@ -30,7 +30,7 @@ class Brain {
private double[][] X;
private int[] y;
public Brain(int nNeighbors, int nTemplates, int nClasses, double power, double[][] X, int[] y) {
public KNeighborsClassifier(int nNeighbors, int nTemplates, int nClasses, double power, double[][] X, int[] y) {
this.nNeighbors = nNeighbors;
this.nTemplates = nTemplates;
this.nClasses = nClasses;
Expand All @@ -48,7 +48,7 @@ class Brain {
}
}
private static double compDist(double[] temp, double[] cand, double q) {
private static double compute(double[] temp, double[] cand, double q) {
double dist = 0.;
double diff;
for (int i = 0, l = temp.length; i < l; i++) {
Expand Down Expand Up @@ -80,7 +80,7 @@ class Brain {
double minDist = Double.POSITIVE_INFINITY;
double curDist;
for (int i = 0; i < this.nTemplates; i++) {
curDist = Brain.compDist(this.X[i], features, this.power);
curDist = KNeighborsClassifier.compute(this.X[i], features, this.power);
if (curDist <= minDist) {
minDist = curDist;
classIdx = y[i];
Expand All @@ -90,7 +90,7 @@ class Brain {
int[] classes = new int[this.nClasses];
ArrayList<Neighbor> dists = new ArrayList<Neighbor>();
for (int i = 0; i < this.nTemplates; i++) {
dists.add(new Neighbor(y[i], Brain.compDist(this.X[i], features, this.power)));
dists.add(new Neighbor(y[i], KNeighborsClassifier.compute(this.X[i], features, this.power)));
}
Collections.sort(dists, new Comparator<Neighbor>() {
@Override
Expand Down Expand Up @@ -126,7 +126,7 @@ class Brain {
int[] y = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
// Prediction:
Brain clf = new Brain(3, 150, 3, 2, X, y);
KNeighborsClassifier clf = new KNeighborsClassifier(3, 150, 3, 2, X, y);
int estimation = clf.predict(features);
System.out.println(estimation);
Expand Down
96 changes: 50 additions & 46 deletions examples/estimator/classifier/KNeighborsClassifier/js/basics.ipynb

Large diffs are not rendered by default.

Expand Up @@ -19,7 +19,7 @@
print(output)

"""
var Brain = function(nNeighbors, nTemplates, nClasses, power, X, y) {
var KNeighborsClassifier = function(nNeighbors, nTemplates, nClasses, power, X, y) {
this.nNeighbors = nNeighbors;
this.nTemplates = nTemplates;
Expand Down Expand Up @@ -107,7 +107,7 @@
var y = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2];
// Estimator:
var clf = new Brain(3, 150, 3, 2, X, y);
var clf = new KNeighborsClassifier(3, 150, 3, 2, X, y);
var prediction = clf.predict(features);
console.log(prediction);
Expand Down
13 changes: 10 additions & 3 deletions sklearn_porter/Porter.py
Expand Up @@ -147,7 +147,7 @@ def __init__(self, estimator, language='java', method='predict', **kwargs):
# Create instance with all parameters:
self.template = clazz(**self.__dict__)

def export(self, class_name='Brain', method_name='predict',
def export(self, class_name=None, method_name=None,
use_repr=True, details=False, **kwargs):
# pylint: disable=unused-argument
"""
Expand All @@ -156,10 +156,10 @@ def export(self, class_name='Brain', method_name='predict',
Parameters
----------
:param class_name : string, default: 'Brain'
:param class_name : string, default: None
The name for the ported class.
:param method_name : string, default: 'predict'
:param method_name : string, default: None
The name for the ported method.
:param use_repr : bool, default: True
Expand All @@ -175,6 +175,13 @@ def export(self, class_name='Brain', method_name='predict',
The ported model as string or a dictionary
with further information.
"""

if class_name is None:
class_name = self.estimator_name

if method_name is None:
method_name = self.target_method

output = self.template.export(class_name=class_name,
method_name=method_name,
use_repr=use_repr)
Expand Down
Expand Up @@ -94,15 +94,15 @@ def __init__(self, estimator, target_language='java',
self.n_estimators += 1
self.n_features = self.estimator.estimators_[idx].n_features_

def export(self, class_name="Brain", method_name="predict", use_repr=True):
def export(self, class_name, method_name, use_repr=True):
"""
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param class_name: string, default: 'Brain'
:param class_name: string
The name of the class in the returned result.
:param method_name: string, default: 'predict'
:param method_name: string
The name of the method in the returned result.
:param use_repr : bool, default True
Whether to use repr() for floating-point values or not.
Expand Down
6 changes: 3 additions & 3 deletions sklearn_porter/estimator/classifier/BernoulliNB/__init__.py
Expand Up @@ -91,15 +91,15 @@ def __init__(self, estimator, target_language='java',
self.del_probs = temp_arr__.format(type='double', name='delProbs',
values=probs)

def export(self, class_name="Brain", method_name="predict", use_repr=True):
def export(self, class_name, method_name, use_repr=True):
"""
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param class_name: string, default: 'Brain'
:param class_name: string
The name of the class in the returned result.
:param method_name: string, default: 'predict'
:param method_name: string
The name of the method in the returned result.
:param use_repr : bool, default True
Whether to use repr() for floating-point values or not.
Expand Down
Expand Up @@ -88,15 +88,15 @@ def __init__(self, estimator, target_language='java',
self.n_features = estimator.n_features_
self.n_classes = estimator.n_classes_

def export(self, class_name="Brain", method_name="predict", use_repr=True):
def export(self, class_name, method_name, use_repr=True):
"""
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param class_name: string, default: 'Brain'
:param class_name: string
The name of the class in the returned result.
:param method_name: string, default: 'predict'
:param method_name: string
The name of the method in the returned result.
:param use_repr : bool, default True
Whether to use repr() for floating-point values or not.
Expand Down
6 changes: 3 additions & 3 deletions sklearn_porter/estimator/classifier/GaussianNB/__init__.py
Expand Up @@ -87,15 +87,15 @@ def __init__(self, estimator, target_language='java',
self.thetas = temp_arr__.format(type='double', name='thetas',
values=thetas)

def export(self, class_name="Brain", method_name="predict", use_repr=True):
def export(self, class_name, method_name, use_repr=True):
"""
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param class_name: string, default: 'Brain'
:param class_name: string
The name of the class in the returned result.
:param method_name: string, default: 'predict'
:param method_name: string
The name of the method in the returned result.
:param use_repr : bool, default True
Whether to use repr() for floating-point values or not.
Expand Down
Expand Up @@ -72,7 +72,7 @@ def __init__(self, estimator, target_language='java',
msg = "Only 'uniform' weights are supported for this classifier."
raise NotImplementedError(msg)

def export(self, class_name="Brain", method_name="predict", use_repr=True):
def export(self, class_name, method_name, use_repr=True):
"""
Port a trained estimator to the syntax of a chosen programming language.
Expand Down
2 changes: 1 addition & 1 deletion sklearn_porter/estimator/classifier/LinearSVC/__init__.py
Expand Up @@ -89,7 +89,7 @@ def __init__(self, estimator, target_language='java',
self.is_binary = self.n_classes == 2
self.prefix = 'binary' if self.is_binary else 'multi'

def export(self, class_name="Brain", method_name="predict", use_repr=True):
def export(self, class_name, method_name, use_repr=True):
"""
Port a trained estimator to the syntax of a chosen programming language.
Expand Down
6 changes: 3 additions & 3 deletions sklearn_porter/estimator/classifier/MLPClassifier/__init__.py
Expand Up @@ -105,15 +105,15 @@ def output_activation_functions(self):
"""Get list of supported activation functions for the output layer."""
return ['softmax', 'logistic']

def export(self, class_name='Brain', method_name='predict', use_repr=True):
def export(self, class_name, method_name, use_repr=True):
"""
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param class_name: string, default: 'Brain'
:param class_name: string
The name of the class in the returned result.
:param method_name: string, default: 'predict'
:param method_name: string
The name of the method in the returned result.
:param use_repr : bool, default True
Whether to use repr() for floating-point values or not.
Expand Down
Expand Up @@ -98,15 +98,15 @@ def __init__(self, estimator, target_language='java',
self.n_estimators += 1
self.n_features = self.estimator.estimators_[idx].n_features_

def export(self, class_name="Brain", method_name="predict", use_repr=True):
def export(self, class_name, method_name, use_repr=True):
"""
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param class_name: string, default: 'Brain'
:param class_name: string
The name of the class in the returned result.
:param method_name: string, default: 'predict'
:param method_name: string
The name of the method in the returned result.
:param use_repr : bool, default True
Whether to use repr() for floating-point values or not.
Expand Down
3 changes: 1 addition & 2 deletions sklearn_porter/estimator/classifier/SVC/__init__.py
Expand Up @@ -97,8 +97,7 @@ def __init__(self, estimator, target_language='java',
self.is_binary = self.n_classes == 2
self.prefix = 'binary' if self.is_binary else 'multi'

def export(self, class_name="Brain", method_name="predict",
use_repr=True, use_file=False):
def export(self, class_name, method_name, use_repr=True, use_file=False):
"""
Port a trained estimator to the syntax of a chosen programming language.
Expand Down
6 changes: 3 additions & 3 deletions sklearn_porter/estimator/regressor/MLPRegressor/__init__.py
Expand Up @@ -81,15 +81,15 @@ def hidden_activation_functions(self):
"""Get list of supported activation functions for the hidden layers."""
return ['relu', 'identity', 'tanh', 'logistic']

def export(self, class_name='Brain', method_name='predict', use_repr=True):
def export(self, class_name, method_name, use_repr=True):
"""
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param class_name: string, default: 'Brain'
:param class_name: string
The name of the class in the returned result.
:param method_name: string, default: 'predict'
:param method_name: string
The name of the method in the returned result.
:param use_repr : bool, default True
Whether to use repr() for floating-point values or not.
Expand Down

0 comments on commit 710a854

Please sign in to comment.