Skip to content

Commit

Permalink
Fix inittime (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt authored Sep 28, 2017
1 parent 3424df6 commit 00981a6
Show file tree
Hide file tree
Showing 20 changed files with 221 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/sources/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The CHANGELOG for the current development version is available at

- the "S" vector from SVD in `PrincipalComponentAnalysis` are now scaled so that the eigenvalues via `solver='eigen'` and `solver='svd'` now store eigenvalues that have the same magnitudes. [#251](https://github.com/rasbt/mlxtend/pull/251)
- The parameters for `StackingClassifier`, `StackingCVClassifier`, `StackingRegressor`, `StackingCVRegressor`, and `EnsembleVoteClassifier` can now be tuned using scikit-learn's `GridSearchCV` ([#254](https://github.com/rasbt/mlxtend/pull/254) via [James Bourbeau](https://github.com/jrbourbeau))
- Fix issues with `self._init_time` parameter in `_IterativeModel` subclasses. [#256](https://github.com/rasbt/mlxtend/pull/256)

### Version 0.8.0 (2017-09-09)

Expand Down
4 changes: 3 additions & 1 deletion mlxtend/_base/_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#
# License: BSD 3 clause

from time import time


class _BaseModel(object):

def __init__(self):
pass
self._init_time = time()

def _check_arrays(self, X, y=None):
if isinstance(X, list):
Expand Down
2 changes: 2 additions & 0 deletions mlxtend/_base/_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# License: BSD 3 clause

import numpy as np
from time import time


class _Classifier(object):
Expand Down Expand Up @@ -75,6 +76,7 @@ def fit(self, X, y, init_params=True):
self._check_target_array(y)
if hasattr(self, 'self.random_seed') and self.random_seed:
self._rgen = np.random.RandomState(self.random_seed)
self._init_time = time()
self._fit(X=X, y=y, init_params=init_params)
self._is_fitted = True
return self
Expand Down
2 changes: 2 additions & 0 deletions mlxtend/_base/_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# License: BSD 3 clause

import numpy as np
from time import time


class _Cluster(object):
Expand Down Expand Up @@ -36,6 +37,7 @@ def fit(self, X, init_params=True):
self._check_arrays(X=X)
if hasattr(self, 'self.random_seed') and self.random_seed:
self._rgen = np.random.RandomState(self.random_seed)
self._init_time = time()
self._fit(X=X, init_params=init_params)
self._is_fitted = True
return self
Expand Down
2 changes: 1 addition & 1 deletion mlxtend/_base/_iterative_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _print_progress(self, iteration, n_iter,
if not hasattr(self, 'ela_str_'):
self.ela_str_ = '00:00:00'
if not iteration % time_interval:
ela_sec = time() - self.init_time_
ela_sec = time() - self._init_time
self.ela_str_ = self._to_hhmmss(ela_sec)
s += ' | Elapsed: %s' % self.ela_str_
if self.print_progress > 2:
Expand Down
2 changes: 2 additions & 0 deletions mlxtend/_base/_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# License: BSD 3 clause

import numpy as np
from time import time


class _Regressor(object):
Expand Down Expand Up @@ -44,6 +45,7 @@ def fit(self, X, y, init_params=True):
self._check_target_array(y)
if hasattr(self, 'self.random_seed') and self.random_seed:
self._rgen = np.random.RandomState(self.random_seed)
self._init_time = time()
self._fit(X=X, y=y, init_params=init_params)
self._is_fitted = True
return self
Expand Down
4 changes: 4 additions & 0 deletions mlxtend/classifier/adaline.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def __init__(self, eta=0.01, epochs=50,
minibatches=None, random_seed=None,
print_progress=0):

_BaseModel.__init__(self)
_IterativeModel.__init__(self)
_Classifier.__init__(self)

self.eta = eta
self.minibatches = minibatches
self.epochs = epochs
Expand Down
4 changes: 4 additions & 0 deletions mlxtend/classifier/logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def __init__(self, eta=0.01, epochs=50,
random_seed=None,
print_progress=0):

_BaseModel.__init__(self)
_IterativeModel.__init__(self)
_Classifier.__init__(self)

self.eta = eta
self.epochs = epochs
self.l2_lambda = l2_lambda
Expand Down
6 changes: 6 additions & 0 deletions mlxtend/classifier/multilayerperceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def __init__(self, eta=0.5, epochs=50,
minibatches=1, random_seed=None,
print_progress=0):

_BaseModel.__init__(self)
_Classifier.__init__(self)
_IterativeModel.__init__(self)
_MultiClass.__init__(self)
_MultiLayer.__init__(self)

if len(hidden_layers) > 1:
raise AttributeError('Currently, only 1 hidden layer is supported')
self.hidden_layers = hidden_layers
Expand Down
4 changes: 4 additions & 0 deletions mlxtend/classifier/perceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class Perceptron(_BaseModel, _IterativeModel, _Classifier):
def __init__(self, eta=0.1, epochs=50, random_seed=None,
print_progress=0):

_BaseModel.__init__(self)
_IterativeModel.__init__(self)
_Classifier.__init__(self)

self.eta = eta
self.epochs = epochs
self.random_seed = random_seed
Expand Down
7 changes: 6 additions & 1 deletion mlxtend/classifier/softmax_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .._base import _Classifier


class SoftmaxRegression(_BaseModel, _IterativeModel, _MultiClass, _Classifier):
class SoftmaxRegression(_BaseModel, _IterativeModel, _Classifier, _MultiClass):

"""Softmax regression classifier.
Expand Down Expand Up @@ -66,6 +66,11 @@ def __init__(self, eta=0.01, epochs=50,
random_seed=None,
print_progress=0):

_BaseModel.__init__(self)
_IterativeModel.__init__(self)
_Classifier.__init__(self)
_MultiClass.__init__(self)

self.eta = eta
self.epochs = epochs
self.l2 = l2
Expand Down
27 changes: 27 additions & 0 deletions mlxtend/classifier/tests/test_adaline.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,33 @@ def test_gradient_descent():
assert((y1 == ada.predict(X_std)).all())


def test_print_progress_1():
ada = Adaline(epochs=30,
eta=0.01,
minibatches=1,
print_progress=1,
random_seed=1)
ada.fit(X_std, y1)


def test_print_progress_2():
ada = Adaline(epochs=30,
eta=0.01,
minibatches=1,
print_progress=2,
random_seed=1)
ada.fit(X_std, y1)


def test_print_progress_3():
ada = Adaline(epochs=30,
eta=0.01,
minibatches=1,
print_progress=3,
random_seed=1)
ada.fit(X_std, y1)


def test_score_function():
ada = Adaline(epochs=30,
eta=0.01,
Expand Down
27 changes: 27 additions & 0 deletions mlxtend/classifier/tests/test_logistic_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,33 @@ def test_logistic_regression_gd():
assert acc == 1.0, "Acc: %s" % acc


def test_print_progress_1():
lr = LogisticRegression(epochs=100,
eta=0.01,
minibatches=1,
print_progress=1,
random_seed=1)
lr.fit(X, y)


def test_print_progress_2():
lr = LogisticRegression(epochs=100,
eta=0.01,
minibatches=1,
print_progress=2,
random_seed=1)
lr.fit(X, y)


def test_print_progress_3():
lr = LogisticRegression(epochs=100,
eta=0.01,
minibatches=1,
print_progress=3,
random_seed=1)
lr.fit(X, y)


def test_score_function():
lr = LogisticRegression(epochs=100,
eta=0.01,
Expand Down
30 changes: 30 additions & 0 deletions mlxtend/classifier/tests/test_multilayerperceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@ def test_multiclass_gd_acc():
assert (y == mlp.predict(X)).all()


def test_progress_1():
mlp = MLP(epochs=1,
eta=0.05,
hidden_layers=[10],
minibatches=1,
print_progress=1,
random_seed=1)
mlp.fit(X, y)


def test_progress_2():
mlp = MLP(epochs=1,
eta=0.05,
hidden_layers=[10],
minibatches=1,
print_progress=2,
random_seed=1)
mlp.fit(X, y)


def test_progress_3():
mlp = MLP(epochs=1,
eta=0.05,
hidden_layers=[10],
minibatches=1,
print_progress=3,
random_seed=1)
mlp.fit(X, y)


def test_predict_proba():
mlp = MLP(epochs=20,
eta=0.05,
Expand Down
15 changes: 15 additions & 0 deletions mlxtend/classifier/tests/test_perceptron.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ def test_standardized_iris_data():
assert (y0 == ppn.predict(X_std)).all(), ppn.predict(X_std)


def test_progress_1():
ppn = Perceptron(epochs=15, eta=0.01, random_seed=1, print_progress=1)
ppn = ppn.fit(X_std, y0)


def test_progress_2():
ppn = Perceptron(epochs=15, eta=0.01, random_seed=1, print_progress=2)
ppn = ppn.fit(X_std, y0)


def test_progress_3():
ppn = Perceptron(epochs=15, eta=0.01, random_seed=1, print_progress=3)
ppn = ppn.fit(X_std, y0)


def test_score_function():
ppn = Perceptron(epochs=15, eta=0.01, random_seed=1)
ppn = ppn.fit(X_std, y0)
Expand Down
30 changes: 30 additions & 0 deletions mlxtend/classifier/tests/test_softmax_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,36 @@ def test_binary_logistic_regression_sgd():
assert (y_bin == lr.predict(X_bin)).all()


def test_progress_1():
lr = SoftmaxRegression(epochs=1,
eta=0.005,
minibatches=1,
print_progress=1,
random_seed=1)

lr.fit(X_bin, y_bin) # 0, 1 class


def test_progress_2():
lr = SoftmaxRegression(epochs=1,
eta=0.005,
minibatches=1,
print_progress=2,
random_seed=1)

lr.fit(X_bin, y_bin) # 0, 1 class


def test_progress_3():
lr = SoftmaxRegression(epochs=1,
eta=0.005,
minibatches=1,
print_progress=3,
random_seed=1)

lr.fit(X_bin, y_bin) # 0, 1 class


def test_binary_l2_regularization_gd():
t = np.array([[-0.17, 0.17],
[-2.26, 2.26]])
Expand Down
4 changes: 3 additions & 1 deletion mlxtend/cluster/kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def __init__(self, k, max_iter=10,
convergence_tolerance=1e-05,
random_seed=None, print_progress=0):

_BaseModel.__init__(self)
_Cluster.__init__(self)
_IterativeModel.__init__(self)
self.k = k
self.max_iter = max_iter
self.convergence_tolerance = convergence_tolerance
Expand All @@ -71,7 +74,6 @@ def _fit(self, X, init_params=True):
Called in self.fit
"""

n_samples = X.shape[0]

if init_params:
Expand Down
24 changes: 24 additions & 0 deletions mlxtend/cluster/tests/test_kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ def test_three_blobs_multi():
assert (y_pred == y).all()


def test_print_progress_1():
km = Kmeans(k=3,
max_iter=50,
random_seed=1,
print_progress=1)
km.fit(X)


def test_print_progress_2():
km = Kmeans(k=3,
max_iter=50,
random_seed=1,
print_progress=2)
km.fit(X)


def test_print_progress_3():
km = Kmeans(k=3,
max_iter=50,
random_seed=1,
print_progress=3)
km.fit(X)


def test_three_blobs_1sample():
km = Kmeans(k=3,
max_iter=50,
Expand Down
3 changes: 3 additions & 0 deletions mlxtend/regressor/linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def __init__(self, eta=0.01, epochs=50,
minibatches=None, random_seed=None,
print_progress=0):

_BaseModel.__init__(self)
_IterativeModel.__init__(self)
_Regressor.__init__(self)
self.eta = eta
self.epochs = epochs
self.minibatches = minibatches
Expand Down
Loading

0 comments on commit 00981a6

Please sign in to comment.