Skip to content

Commit

Permalink
Optimize internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nok committed Oct 7, 2017
1 parent 9ac0375 commit 788c8b4
Show file tree
Hide file tree
Showing 69 changed files with 795 additions and 824 deletions.
290 changes: 149 additions & 141 deletions sklearn_porter/Porter.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions sklearn_porter/Template.py
Expand Up @@ -8,7 +8,7 @@ class Template(object):
SUPPORTED_METHODS = {}
TEMPLATES = {}

def __init__(self, model, target_language='java',
def __init__(self, estimator, target_language='java',
target_method='predict', **kwargs):
# pylint: disable=unused-argument
self.target_language = str(target_language)
Expand Down Expand Up @@ -95,7 +95,7 @@ def temp(self, name, templates=None, n_indents=None, skipping=False):
else:
class_name = self.__class__.__name__
path = os.path.join(os.path.dirname(__file__), 'estimator',
self.algorithm_type, class_name, 'templates',
self.estimator_type, class_name, 'templates',
self.target_language, name + '.txt')
if os.path.isfile(path):
template = open(path, 'r').read()
Expand Down
72 changes: 36 additions & 36 deletions sklearn_porter/estimator/classifier/AdaBoostClassifier/__init__.py
Expand Up @@ -44,59 +44,59 @@ class AdaBoostClassifier(Classifier):
}
# @formatter:on

def __init__(self, model, target_language='java',
def __init__(self, estimator, target_language='java',
target_method='predict', **kwargs):
"""
Port a trained model to the syntax of a chosen programming language.
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param model : AdaBoostClassifier
An instance of a trained AdaBoostClassifier model.
:param estimator : AdaBoostClassifier
An instance of a trained AdaBoostClassifier estimator.
:param target_language : string
The target programming language.
:param target_method : string
The target method of the estimator.
"""
super(AdaBoostClassifier, self).__init__(
model, target_language=target_language,
estimator, target_language=target_language,
target_method=target_method, **kwargs)
self.model = model
self.estimator = estimator

# Check the used algorithm type:
if model.algorithm != 'SAMME.R':
if estimator.algorithm != 'SAMME.R':
msg = "The classifier doesn't support the given algorithm %s."
raise ValueError(msg, model.algorithm)
raise ValueError(msg, estimator.algorithm)

# Check type of base estimators:
if not isinstance(
model.base_estimator,
estimator.base_estimator,
sklearn.tree.tree.DecisionTreeClassifier):
msg = "The classifier doesn't support the given base estimator %s."
raise ValueError(msg, model.base_estimator)
raise ValueError(msg, estimator.base_estimator)

# Check number of base estimators:
if not model.n_estimators > 0:
if not estimator.n_estimators > 0:
msg = "The classifier hasn't any base estimators."
raise ValueError(msg)

self.model = model
self.n_classes = model.n_classes_
self.estimator = estimator
self.n_classes = estimator.n_classes_

self.models = []
self.estimators = []
self.weights = []
self.n_estimators = 0
for idx in range(self.model.n_estimators):
weight = self.model.estimator_weights_[idx]
for idx in range(self.estimator.n_estimators):
weight = self.estimator.estimator_weights_[idx]
if weight > 0:
self.models.append(self.model.estimators_[idx])
self.weights.append(self.model.estimator_weights_[idx])
self.estimators.append(self.estimator.estimators_[idx])
self.weights.append(self.estimator.estimator_weights_[idx])
self.n_estimators += 1
self.n_features = self.model.estimators_[idx].n_features_
self.n_features = self.estimator.estimators_[idx].n_features_

def export(self, class_name="Brain", method_name="predict", use_repr=True):
"""
Port a trained model to the syntax of a chosen programming language.
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
Expand Down Expand Up @@ -132,7 +132,7 @@ def predict(self):
def create_branches(self, left_nodes, right_nodes, threshold,
value, features, node, depth):
"""
Parse and port a single tree model.
Parse and port a single tree estimator.
Parameters
----------
Expand Down Expand Up @@ -183,43 +183,43 @@ def create_branches(self, left_nodes, right_nodes, threshold,
out += self.temp('join').join(clazzes) + self.temp('join')
return out

def create_single_method(self, model_index, model):
def create_single_method(self, estimator_index, estimator):
"""
Port a method for a single tree.
Parameters
----------
:param model_index : int
The model index.
:param model : AdaBoostClassifier
The model itself.
:param estimator_index : int
The estimator index.
:param estimator : AdaBoostClassifier
The estimator itself.
Returns
-------
:return : string
The created method as string.
"""
feature_indices = []
for i in model.tree_.feature:
n_features = model.n_features_
for i in estimator.tree_.feature:
n_features = estimator.n_features_
if self.n_features > 1 or (self.n_features == 1 and i >= 0):
feature_indices.append([str(j) for j in range(n_features)][i])

tree_branches = self.create_branches(
model.tree_.children_left, model.tree_.children_right,
model.tree_.threshold, model.tree_.value,
estimator.tree_.children_left, estimator.tree_.children_right,
estimator.tree_.threshold, estimator.tree_.value,
feature_indices, 0, 1)

temp_single_method = self.temp('single_method')
out = temp_single_method.format(method_name=self.method_name,
method_index=str(model_index),
method_index=str(estimator_index),
methods=tree_branches,
n_classes=self.n_classes)
return out

def create_method(self):
"""
Build the model methods or functions.
Build the estimator methods or functions.
Returns
-------
Expand All @@ -230,7 +230,7 @@ def create_method(self):
fn_names = []
temp_method_calls = self.temp('method_calls', n_indents=2,
skipping=True)
for idx, model in enumerate(self.models):
for idx, estimator in enumerate(self.estimators):
cl_name = self.class_name
fn_name = self.method_name + '_' + str(idx)
fn_name = temp_method_calls.format(class_name=cl_name,
Expand All @@ -242,8 +242,8 @@ def create_method(self):

# Generate related trees:
fns = []
for idx, model in enumerate(self.models):
tree = self.create_single_method(idx, model)
for idx, estimator in enumerate(self.estimators):
tree = self.create_single_method(idx, estimator)
fns.append(tree)
fns = '\n'.join(fns)

Expand All @@ -259,7 +259,7 @@ def create_method(self):

def create_class(self, method):
"""
Build the model class.
Build the estimator class.
Returns
-------
Expand Down
32 changes: 16 additions & 16 deletions sklearn_porter/estimator/classifier/BernoulliNB/__init__.py
Expand Up @@ -32,28 +32,28 @@ class BernoulliNB(Classifier):
}
# @formatter:on

def __init__(self, model, target_language='java',
def __init__(self, estimator, target_language='java',
target_method='predict', **kwargs):
"""
Port a trained model to the syntax of a chosen programming language.
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param model : AdaBoostClassifier
An instance of a trained BernoulliNB model.
:param estimator : AdaBoostClassifier
An instance of a trained BernoulliNB estimator.
:param target_language : string
The target programming language.
:param target_method : string
The target method of the estimator.
"""
super(BernoulliNB, self).__init__(
model, target_language=target_language,
estimator, target_language=target_language,
target_method=target_method, **kwargs)
self.model = model
self.estimator = estimator

# self.n_features = len(model.sigma_[0])
self.n_classes = len(model.classes_)
self.n_features = len(model.feature_log_prob_[0])
# self.n_features = len(estimator.sigma_[0])
self.n_classes = len(estimator.classes_)
self.n_features = len(estimator.feature_log_prob_[0])

# jll = safe_sparse_dot(X, (self.feature_log_prob_ - neg_prob).T)
# jll += self.class_log_prior_ + neg_prob.sum(axis=1)
Expand All @@ -65,14 +65,14 @@ def __init__(self, model, target_language='java',

# Create class prior probabilities:
priors = [self.temp('type').format(self.repr(p)) for p in
model.class_log_prior_]
estimator.class_log_prior_]
priors = ', '.join(priors)
self.priors = temp_arr_.format(type='double', name='priors',
values=priors)

# Create probabilities:
# probs = []
# for prob in model.feature_log_prob_:
# for prob in estimator.feature_log_prob_:
# tmp = [self.temp('type').format(repr(p)) for p in prob]
# tmp = self.temp('arr').format(', '.join(tmp))
# probs.append(tmp)
Expand All @@ -82,7 +82,7 @@ def __init__(self, model, target_language='java',
# values=probs)

# Create negative probabilities:
neg_prob = np.log(1 - np.exp(model.feature_log_prob_))
neg_prob = np.log(1 - np.exp(estimator.feature_log_prob_))
probs = []
for prob in neg_prob:
tmp = [temp_type.format(self.repr(p)) for p in prob]
Expand All @@ -92,7 +92,7 @@ def __init__(self, model, target_language='java',
self.neg_probs = temp_arr__.format(type='double', name='negProbs',
values=probs)

delta_probs = (model.feature_log_prob_ - neg_prob).T
delta_probs = (estimator.feature_log_prob_ - neg_prob).T
probs = []
for prob in delta_probs:
tmp = [temp_type.format(self.repr(p)) for p in prob]
Expand All @@ -104,7 +104,7 @@ def __init__(self, model, target_language='java',

def export(self, class_name="Brain", method_name="predict", use_repr=True):
"""
Port a trained model to the syntax of a chosen programming language.
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
Expand Down Expand Up @@ -139,7 +139,7 @@ def predict(self):

def create_method(self):
"""
Build the model method or function.
Build the estimator method or function.
Returns
-------
Expand All @@ -154,7 +154,7 @@ def create_method(self):

def create_class(self, method):
"""
Build the model class.
Build the estimator class.
Returns
-------
Expand Down
6 changes: 3 additions & 3 deletions sklearn_porter/estimator/classifier/Classifier.py
Expand Up @@ -5,7 +5,7 @@

class Classifier(Template):

def __init__(self, model, **kwargs):
def __init__(self, estimator, **kwargs):
# pylint: disable=unused-argument
super(Classifier, self).__init__(model, **kwargs)
self.algorithm_type = 'classifier'
super(Classifier, self).__init__(estimator, **kwargs)
self.estimator_type = 'classifier'
Expand Up @@ -67,30 +67,30 @@ class DecisionTreeClassifier(Classifier):
}
# @formatter:on

def __init__(self, model, target_language='java',
def __init__(self, estimator, target_language='java',
target_method='predict', **kwargs):
"""
Port a trained model to the syntax of a chosen programming language.
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
:param model : AdaBoostClassifier
An instance of a trained DecisionTreeClassifier model.
:param estimator : AdaBoostClassifier
An instance of a trained DecisionTreeClassifier estimator.
:param target_language : string
The target programming language.
:param target_method : string
The target method of the estimator.
"""
super(DecisionTreeClassifier, self).__init__(
model, target_language=target_language,
estimator, target_language=target_language,
target_method=target_method, **kwargs)
self.model = model
self.n_features = model.n_features_
self.n_classes = model.n_classes_
self.estimator = estimator
self.n_features = estimator.n_features_
self.n_classes = estimator.n_classes_

def export(self, class_name="Brain", method_name="predict", use_repr=True):
"""
Port a trained model to the syntax of a chosen programming language.
Port a trained estimator to the syntax of a chosen programming language.
Parameters
----------
Expand Down Expand Up @@ -128,7 +128,7 @@ def predict(self, class_name, method_name):
def create_branches(self, left_nodes, right_nodes, threshold,
value, features, node, depth):
"""
Parse and port a single tree model.
Parse and port a single tree estimator.
Parameters
----------
Expand Down Expand Up @@ -190,22 +190,22 @@ def create_tree(self):
The tree branches as string.
"""
feature_indices = []
for i in self.model.tree_.feature:
for i in self.estimator.tree_.feature:
n_features = self.n_features
if self.n_features > 1 or (self.n_features == 1 and i >= 0):
feature_indices.append([str(j) for j in range(n_features)][i])

indentation = 1 if self.target_language in ['java', 'js', 'php', 'ruby'] else 0
return self.create_branches(
self.model.tree_.children_left,
self.model.tree_.children_right,
self.model.tree_.threshold,
self.model.tree_.value,
self.estimator.tree_.children_left,
self.estimator.tree_.children_right,
self.estimator.tree_.threshold,
self.estimator.tree_.value,
feature_indices, 0, indentation)

def create_method(self, class_name, method_name):
"""
Build the model method or function.
Build the estimator method or function.
Returns
-------
Expand All @@ -222,7 +222,7 @@ def create_method(self, class_name, method_name):

def create_class(self, method, class_name, method_name):
"""
Build the model class.
Build the estimator class.
Returns
-------
Expand Down

0 comments on commit 788c8b4

Please sign in to comment.