Skip to content

Commit

Permalink
Merge pull request #2 from neurodata/staging
Browse files Browse the repository at this point in the history
Staging updates
  • Loading branch information
EYezerets authored Oct 20, 2020
2 parents d14a83c + 2f30a75 commit 18acf01
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 28 deletions.
65 changes: 42 additions & 23 deletions proglearn/forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class LifelongClassificationForest(ClassificationProgressiveLearner):
Parameters
----------
n_estimators : int, default=100
The number of estimators used in the Lifelong Classification Forest
default_n_estimators : int, default=100
The number of trees used in the Lifelong Classification Forest
used if 'n_estimators' is not fed to add_{task, transformer}.
default_tree_construction_proportion : int, default=0.67
The proportions of the input data set aside to train each decision
Expand All @@ -40,12 +41,12 @@ class LifelongClassificationForest(ClassificationProgressiveLearner):

def __init__(
self,
n_estimators=100,
default_n_estimators=100,
default_tree_construction_proportion=0.67,
default_finite_sample_correction=False,
default_max_depth=30,
):
self.n_estimators = n_estimators
self.default_n_estimators = default_n_estimators
self.default_tree_construction_proportion = default_tree_construction_proportion
self.default_finite_sample_correction = default_finite_sample_correction
self.default_max_depth = default_max_depth
Expand All @@ -65,9 +66,10 @@ def add_task(
X,
y,
task_id=None,
tree_construction_proportion=None,
finite_sample_correction=None,
max_depth=None,
n_estimators="default",
tree_construction_proportion="default",
finite_sample_correction="default",
max_depth="default",
):
"""
adds a task with id task_id, max tree depth max_depth, given input data matrix X
Expand All @@ -87,29 +89,34 @@ def add_task(
task_id : obj, default=None
The id corresponding to the task being added.
tree_construction_proportion : int, default=None
n_estimators : int or str, default='default'
The number of trees used for the given task.
tree_construction_proportion : int or str, default='default'
The proportions of the input data set aside to train each decision
tree. The remainder of the data is used to fill in voting posteriors.
The default is used if 'None' is provided.
The default is used if 'default' is provided.
finite_sample_correction : bool, default=False
finite_sample_correction : bool or str, default='default'
Boolean indicating whether this learner will have finite sample correction.
The default is used if 'None' is provided.
The default is used if 'default' is provided.
max_depth : int, default=30
max_depth : int or str, default='default'
The maximum depth of a tree in the Lifelong Classification Forest.
The default is used if 'None' is provided.
The default is used if 'default' is provided.
Returns
-------
self : LifelongClassificationForest
The object itself.
"""
if tree_construction_proportion is None:
if n_estimators == "default":
n_estimators = self.default_n_estimators
if tree_construction_proportion == "default":
tree_construction_proportion = self.default_tree_construction_proportion
if finite_sample_correction is None:
if finite_sample_correction == "default":
finite_sample_correction = self.default_finite_sample_correction
if max_depth is None:
if max_depth == "default":
max_depth = self.default_max_depth

self.pl_.add_task(
Expand All @@ -121,7 +128,7 @@ def add_task(
1 - tree_construction_proportion,
0,
],
num_transformers=self.n_estimators,
num_transformers=n_estimators,
transformer_kwargs={"kwargs": {"max_depth": max_depth}},
voter_kwargs={
"classes": np.unique(y),
Expand All @@ -131,7 +138,14 @@ def add_task(
)
return self

def add_transformer(self, X, y, transformer_id=None, max_depth=None):
def add_transformer(
self,
X,
y,
transformer_id=None,
n_estimators="default",
max_depth="default",
):
"""
adds a transformer with id transformer_id and max tree depth max_depth, trained on
given input data matrix, X, and output data matrix, y, to the Lifelong Classification Forest.
Expand All @@ -149,24 +163,29 @@ def add_transformer(self, X, y, transformer_id=None, max_depth=None):
transformer_id : obj, default=None
The id corresponding to the transformer being added.
max_depth : int, default=30
n_estimators : int or str, default='default'
The number of trees used for the given task.
max_depth : int or str, default='default'
The maximum depth of a tree in the UncertaintyForest.
The default is used if 'None' is provided.
The default is used if 'default' is provided.
Returns
-------
self : LifelongClassificationForest
The object itself.
"""
if max_depth is None:
if n_estimators == "default":
n_estimators = self.default_n_estimators
if max_depth == "default":
max_depth = self.default_max_depth

self.pl_.add_transformer(
X,
y,
transformer_kwargs={"kwargs": {"max_depth": max_depth}},
transformer_id=transformer_id,
num_transformers=self.n_estimators,
num_transformers=n_estimators,
)

return self
Expand Down Expand Up @@ -257,7 +276,7 @@ def fit(self, X, y):
The object itself.
"""
self.lf_ = LifelongClassificationForest(
n_estimators=self.n_estimators,
default_n_estimators=self.n_estimators,
default_finite_sample_correction=self.finite_sample_correction,
default_max_depth=self.max_depth,
default_tree_construction_proportion=self.tree_construction_proportion,
Expand Down
8 changes: 4 additions & 4 deletions proglearn/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def __init__(
default_decider_kwargs={},
)

def add_task(self, X, y, task_id=None, transformer_voter_decider_split=None):
def add_task(self, X, y, task_id=None, transformer_voter_decider_split="default"):
"""
adds a task with id task_id, given input data matrix X
and output data matrix y, to the Lifelong Classification Network
Expand All @@ -111,18 +111,18 @@ def add_task(self, X, y, task_id=None, transformer_voter_decider_split=None):
task_id: obj
The id corresponding to the task being added.
transformer_voter_decider_split: ndarray, default=None
transformer_voter_decider_split: ndarray or str, default='default'
1D array of length 3 corresponding to the proportions of data used to train the
transformer(s) corresponding to the task_id, to train the voter(s) from the
transformer(s) to the task_id, and to train the decider for task_id, respectively.
The default is used if 'None' is provided.
The default is used if 'default' is provided.
Returns
-------
self : LifelongClassificationNetwork
The object itself.
"""
if transformer_voter_decider_split is None:
if transformer_voter_decider_split == "default":
transformer_voter_decider_split = (
self.default_transformer_voter_decider_split
)
Expand Down
2 changes: 1 addition & 1 deletion proglearn/tests/test_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_correct_default_kwargs(self):

def test_correct_default_n_estimators(self):
l2f = LifelongClassificationForest()
assert l2f.n_estimators == 100
assert l2f.default_n_estimators == 100

def test_correct_true_initilization_finite_sample_correction(self):
l2f = LifelongClassificationForest(default_finite_sample_correction=True)
Expand Down

0 comments on commit 18acf01

Please sign in to comment.