2015-06-02 04:08:31 +08:00
|
|
|
from nose.tools import assert_equal
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
2015-11-24 22:05:07 +08:00
|
|
|
from sklearn.utils import testing
|
2015-08-04 04:08:45 +08:00
|
|
|
from sklearn.preprocessing import FunctionTransformer
|
2015-06-02 04:08:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _make_func(args_store, kwargs_store, func=lambda X, *a, **k: X):
|
|
|
|
|
def _func(X, *args, **kwargs):
|
|
|
|
|
args_store.append(X)
|
|
|
|
|
args_store.extend(args)
|
|
|
|
|
kwargs_store.update(kwargs)
|
|
|
|
|
return func(X)
|
|
|
|
|
|
|
|
|
|
return _func
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_delegate_to_func():
|
|
|
|
|
# (args|kwargs)_store will hold the positional and keyword arguments
|
2015-07-31 00:48:04 +08:00
|
|
|
# passed to the function inside the FunctionTransformer.
|
2015-06-02 04:08:31 +08:00
|
|
|
args_store = []
|
|
|
|
|
kwargs_store = {}
|
|
|
|
|
X = np.arange(10).reshape((5, 2))
|
2015-11-24 22:05:07 +08:00
|
|
|
testing.assert_array_equal(
|
2015-07-31 00:48:04 +08:00
|
|
|
FunctionTransformer(_make_func(args_store, kwargs_store)).transform(X),
|
2015-06-02 04:08:31 +08:00
|
|
|
X,
|
|
|
|
|
'transform should have returned X unchanged',
|
|
|
|
|
)
|
|
|
|
|
|
2015-12-08 02:13:40 +08:00
|
|
|
# The function should only have received X.
|
2015-06-02 04:08:31 +08:00
|
|
|
assert_equal(
|
|
|
|
|
args_store,
|
2015-07-31 00:48:04 +08:00
|
|
|
[X],
|
2015-06-02 04:08:31 +08:00
|
|
|
'Incorrect positional arguments passed to func: {args}'.format(
|
|
|
|
|
args=args_store,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
assert_equal(
|
|
|
|
|
kwargs_store,
|
|
|
|
|
{},
|
|
|
|
|
'Unexpected keyword arguments passed to func: {args}'.format(
|
|
|
|
|
args=kwargs_store,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
2015-07-31 00:48:04 +08:00
|
|
|
# reset the argument stores.
|
2015-06-09 00:53:25 +08:00
|
|
|
args_store[:] = [] # python2 compatible inplace list clear.
|
2015-07-31 00:48:04 +08:00
|
|
|
kwargs_store.clear()
|
|
|
|
|
y = object()
|
2015-06-02 04:08:31 +08:00
|
|
|
|
2015-11-24 22:05:07 +08:00
|
|
|
testing.assert_array_equal(
|
2015-07-31 00:48:04 +08:00
|
|
|
FunctionTransformer(
|
2015-06-02 04:08:31 +08:00
|
|
|
_make_func(args_store, kwargs_store),
|
2015-07-31 00:48:04 +08:00
|
|
|
pass_y=True,
|
|
|
|
|
).transform(X, y),
|
2015-06-02 04:08:31 +08:00
|
|
|
X,
|
|
|
|
|
'transform should have returned X unchanged',
|
|
|
|
|
)
|
|
|
|
|
|
2015-12-08 02:13:40 +08:00
|
|
|
# The function should have received X and y.
|
2015-06-02 04:08:31 +08:00
|
|
|
assert_equal(
|
|
|
|
|
args_store,
|
2015-07-31 00:48:04 +08:00
|
|
|
[X, y],
|
2015-06-02 04:08:31 +08:00
|
|
|
'Incorrect positional arguments passed to func: {args}'.format(
|
|
|
|
|
args=args_store,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
assert_equal(
|
|
|
|
|
kwargs_store,
|
2015-07-31 00:48:04 +08:00
|
|
|
{},
|
|
|
|
|
'Unexpected keyword arguments passed to func: {args}'.format(
|
2015-06-02 04:08:31 +08:00
|
|
|
args=kwargs_store,
|
|
|
|
|
),
|
|
|
|
|
)
|
2015-07-31 00:48:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_np_log():
|
|
|
|
|
X = np.arange(10).reshape((5, 2))
|
|
|
|
|
|
|
|
|
|
# Test that the numpy.log example still works.
|
2015-11-24 22:05:07 +08:00
|
|
|
testing.assert_array_equal(
|
2015-08-04 04:08:45 +08:00
|
|
|
FunctionTransformer(np.log1p).transform(X),
|
|
|
|
|
np.log1p(X),
|
2015-07-31 00:48:04 +08:00
|
|
|
)
|
2015-11-24 22:05:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_kw_arg():
|
|
|
|
|
X = np.linspace(0, 1, num=10).reshape((5, 2))
|
|
|
|
|
|
|
|
|
|
F = FunctionTransformer(np.around, kw_args=dict(decimals=3))
|
|
|
|
|
|
|
|
|
|
# Test that rounding is correct
|
|
|
|
|
testing.assert_array_equal(F.transform(X),
|
|
|
|
|
np.around(X, decimals=3))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_kw_arg_update():
|
|
|
|
|
X = np.linspace(0, 1, num=10).reshape((5, 2))
|
|
|
|
|
|
|
|
|
|
F = FunctionTransformer(np.around, kw_args=dict(decimals=3))
|
|
|
|
|
|
|
|
|
|
F.kw_args['decimals'] = 1
|
|
|
|
|
|
|
|
|
|
# Test that rounding is correct
|
|
|
|
|
testing.assert_array_equal(F.transform(X),
|
|
|
|
|
np.around(X, decimals=1))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_kw_arg_reset():
|
|
|
|
|
X = np.linspace(0, 1, num=10).reshape((5, 2))
|
|
|
|
|
|
|
|
|
|
F = FunctionTransformer(np.around, kw_args=dict(decimals=3))
|
|
|
|
|
|
|
|
|
|
F.kw_args = dict(decimals=1)
|
|
|
|
|
|
|
|
|
|
# Test that rounding is correct
|
|
|
|
|
testing.assert_array_equal(F.transform(X),
|
2016-04-12 13:26:30 +08:00
|
|
|
np.around(X, decimals=1))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_inverse_transform():
|
|
|
|
|
X = np.array([1, 4, 9, 16]).reshape((2, 2))
|
|
|
|
|
|
|
|
|
|
# Test that inverse_transform works correctly
|
|
|
|
|
F = FunctionTransformer(
|
|
|
|
|
func=np.sqrt,
|
|
|
|
|
inverse_func=np.around, inv_kw_args=dict(decimals=3))
|
|
|
|
|
testing.assert_array_equal(
|
|
|
|
|
F.inverse_transform(F.transform(X)),
|
|
|
|
|
np.around(np.sqrt(X), decimals=3))
|