Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG + 1]Deprecating 1D inputs in fast_mcd #5234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions sklearn/covariance/robust_covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,7 @@ def fast_mcd(X, support_fraction=None,
"""
random_state = check_random_state(random_state)

X = np.asarray(X)
if X.ndim == 1:
X = np.reshape(X, (1, -1))
warnings.warn("Only one sample available. "
"You may want to reshape your data array")
X = check_array(X, ensure_min_samples=2, estimator='fast_mcd')
n_samples, n_features = X.shape

# minimum breakdown value
Expand Down Expand Up @@ -609,7 +605,7 @@ def fit(self, X, y=None):
Returns self.

"""
X = check_array(X)
X = check_array(X, ensure_min_samples=2, estimator='MinCovDet')
random_state = check_random_state(self.random_state)
n_samples, n_features = X.shape
# check that the empirical covariance is full rank
Expand Down
17 changes: 16 additions & 1 deletion sklearn/covariance/tests/test_robust_covariance.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

from sklearn.utils.testing import assert_almost_equal
from sklearn.utils.testing import assert_array_almost_equal
from sklearn.utils.testing import assert_raises
from sklearn.utils.testing import assert_raises, assert_warns
from sklearn.utils.testing import assert_raise_message
from sklearn.utils.validation import NotFittedError

from sklearn import datasets
from sklearn.covariance import empirical_covariance, MinCovDet, \
EllipticEnvelope
from sklearn.covariance import fast_mcd

X = datasets.load_iris().data
X_1d = X[:, 0]
Expand All @@ -40,6 +42,19 @@ def test_mcd():
launch_mcd_on_dataset(500, 1, 100, 0.001, 0.001, 350)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with the style of the other functions in this file and the project in general, please remove this blank line.

def test_fast_mcd_on_invalid_input():
X = np.arange(100)
assert_raise_message(ValueError, 'fast_mcd expects at least 2 samples',
fast_mcd, X)


def test_mcd_class_on_invalid_input():
X = np.arange(100)
mcd = MinCovDet()
assert_raise_message(ValueError, 'MinCovDet expects at least 2 samples',
mcd.fit, X)


def launch_mcd_on_dataset(n_samples, n_features, n_outliers, tol_loc, tol_cov,
tol_support):

Expand Down
4 changes: 4 additions & 0 deletions sklearn/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ def check_array(array, accept_sparse=None, dtype="numeric", order=None,

if ensure_2d:
if array.ndim == 1:
if ensure_min_samples >= 2:
raise ValueError("%s expects at least 2 samples provided "
"in a 2 dimensional array-like input"
% estimator_name)
warnings.warn(
"Passing 1d arrays as data is deprecated in 0.17 and will"
"raise ValueError in 0.19. Reshape your data either using "
Expand Down