Skip to content

Commit

Permalink
Implement fixes suggested by @jnothman
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinewdg committed Oct 22, 2016
1 parent c6e71bf commit 6052f33
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions doc/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Enhancements
(`#7506` <https://github.com/scikit-learn/scikit-learn/pull/7506>_) by
`Narine Kokhlikyan`_.

- Added ``norm_order`` parameter to :class:`feature_selection.SelectFromModel`
to enable selection of the norm order when ``coef_`` is more than 1D

Bug fixes
.........

Expand Down
2 changes: 1 addition & 1 deletion sklearn/feature_selection/from_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class SelectFromModel(BaseEstimator, SelectorMixin):
norm_order : non-zero int, inf, -inf, default 1
Order of the norm used to filter the vectors of coefficients below
``threshold`` in the case where the ``coeff_`` attribute of the
``threshold`` in the case where the ``coef_`` attribute of the
estimator is of dimension 2.
Attributes
Expand Down
4 changes: 3 additions & 1 deletion sklearn/feature_selection/tests/test_from_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ def test_feature_importances_2d_coef():
for threshold, func in zip(["mean", "median"], [np.mean, np.median]):
for order in [1, 2, np.inf]:
# Fit SelectFromModel a multi-class problem
transformer = SelectFromModel(estimator=LogisticRegression(), threshold=threshold, norm_order=order)
transformer = SelectFromModel(estimator=LogisticRegression(),
threshold=threshold,
norm_order=order)
transformer.fit(X, y)
assert_true(hasattr(transformer.estimator_, 'coef_'))
X_new = transformer.transform(X)
Expand Down
9 changes: 8 additions & 1 deletion sklearn/utils/fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,20 @@ def __getstate__(self):

def norm(X, ord=None, axis=None):
"""
Handles the axis parameter for the norm function in old versions of numpy.
Handles the axis parameter for the norm function
in old versions of numpy.
"""

if axis is None or X.ndim == 1:
result = np.linalg.norm(X, ord=ord)
return result

if axis not in (0, 1):
raise NotImplementedError("""
The fix that adds axis parameter to the old numpy
norm only works for 1D or 2D arrays.
""")

if axis == 0:
X = X.T

Expand Down
1 change: 1 addition & 0 deletions sklearn/utils/tests/test_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def test_masked_array_obj_dtype_pickleable():
assert_array_equal(marr.data, marr_pickled.data)
assert_array_equal(marr.mask, marr_pickled.mask)


def test_norm():
X = np.array([[-2, 4, 5],
[1, 3, -4],
Expand Down

0 comments on commit 6052f33

Please sign in to comment.