Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
193 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
# | ||
# License: BSD 3 clause | ||
|
||
__version__ = '0.4.2dev' | ||
__version__ = '0.4.2dev0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Sebastian Raschka 2014-2016 | ||
# mlxtend Machine Learning Library Extensions | ||
# Author: Sebastian Raschka <sebastianraschka.com> | ||
# | ||
# License: BSD 3 clause | ||
|
||
import numpy as np | ||
from mlxtend.preprocessing import one_hot | ||
from nose.tools import raises | ||
|
||
|
||
def test_default(): | ||
y = np.array([0, 1, 2, 3, 4, 2]) | ||
expect = np.array([[1., 0., 0., 0., 0.], | ||
[0., 1., 0., 0., 0.], | ||
[0., 0., 1., 0., 0.], | ||
[0., 0., 0., 1., 0.], | ||
[0., 0., 0., 0., 1.], | ||
[0., 0., 1., 0., 0.]], dtype='float') | ||
out = one_hot(y) | ||
np.testing.assert_array_equal(expect, out) | ||
|
||
|
||
def test_autoguessing(): | ||
y = np.array([0, 4, 0, 4]) | ||
expect = np.array([[1., 0., 0., 0., 0.], | ||
[0., 0., 0., 0., 1.], | ||
[1., 0., 0., 0., 0.], | ||
[0., 0., 0., 0., 1.]], dtype='float') | ||
out = one_hot(y) | ||
np.testing.assert_array_equal(expect, out) | ||
|
||
|
||
def test_list(): | ||
y = [0, 1, 2, 3, 4, 2] | ||
expect = np.array([[1., 0., 0., 0., 0.], | ||
[0., 1., 0., 0., 0.], | ||
[0., 0., 1., 0., 0.], | ||
[0., 0., 0., 1., 0.], | ||
[0., 0., 0., 0., 1.], | ||
[0., 0., 1., 0., 0.]], dtype='float') | ||
out = one_hot(y) | ||
np.testing.assert_array_equal(expect, out) | ||
|
||
|
||
@raises(AttributeError) | ||
def test_multidim_list(): | ||
y = [[0, 1, 2, 3, 4, 2]] | ||
one_hot(y) | ||
|
||
|
||
@raises(AttributeError) | ||
def test_multidim_array(): | ||
y = np.array([[0], [1], [2], [3], [4], [2]]) | ||
one_hot(y) | ||
|
||
|
||
def test_oneclass(): | ||
np.testing.assert_array_equal(one_hot([0]), | ||
np.array([[0.]], dtype='float')) | ||
|
||
|
||
def test_list_morelabels(): | ||
y = [0, 1] | ||
expect = np.array([[1., 0., 0.], | ||
[0., 1., 0.]], dtype='float') | ||
out = one_hot(y, num_labels=3) | ||
np.testing.assert_array_equal(expect, out) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Sebastian Raschka 2014-2016 | ||
# mlxtend Machine Learning Library Extensions | ||
# Author: Sebastian Raschka <sebastianraschka.com> | ||
# | ||
# License: BSD 3 clause | ||
|
||
import numpy as np | ||
from mlxtend.preprocessing import DenseTransformer | ||
from sklearn.datasets import load_iris | ||
from sklearn.pipeline import make_pipeline | ||
from sklearn.grid_search import GridSearchCV | ||
from sklearn.ensemble import RandomForestClassifier | ||
from sklearn.preprocessing import StandardScaler | ||
from sklearn.feature_extraction.text import TfidfTransformer | ||
from scipy.sparse import issparse | ||
|
||
|
||
iris = load_iris() | ||
X, y = iris.data, iris.target | ||
|
||
|
||
def test_dense_to_dense(): | ||
todense = DenseTransformer(return_copy=False) | ||
np.testing.assert_array_equal(X, todense.transform(X)) | ||
|
||
|
||
def test_sparse_to_dense(): | ||
todense = DenseTransformer() | ||
tfidf = TfidfTransformer() | ||
X_t = tfidf.fit_transform([[1, 2, 3]]) | ||
assert issparse(X_t) | ||
X_dense = todense.transform(X_t) | ||
expect = np.array([[0.26726124, 0.53452248, 0.80178373]]) | ||
assert np.allclose(X_dense, expect) | ||
|
||
|
||
def test_pipeline(): | ||
rf = RandomForestClassifier() | ||
param_grid = [{'randomforestclassifier__n_estimators': [1, 5, 10]}] | ||
pipe = make_pipeline(StandardScaler(), DenseTransformer(), rf) | ||
grid = GridSearchCV(pipe, param_grid, cv=3, n_jobs=1) | ||
grid.fit(X, y) |