Skip to content

[MRG] Use pseudo-inverse in Covariance #206

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

Conversation

wdevazelhes
Copy link
Member

Fixes #204

This PR uses the pseudo inverse in Covariance, rather than the inverse. See #204 (comment)

@@ -35,11 +35,11 @@ def fit(self, X, y=None):
y : unused
"""
X = self._prepare_inputs(X, ensure_min_samples=2)
M = np.cov(X, rowvar = False)
if M.ndim == 0:
M = np.atleast2d(np.cov(X, rowvar = False))
Copy link
Member Author

Choose a reason for hiding this comment

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

I added an atleast2d since we don't want the metric to be scalar

Copy link
Contributor

Choose a reason for hiding this comment

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

While we're here, remove the spaces in the rowvar keyword argument.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree, done

M = np.cov(X, rowvar = False)
if M.ndim == 0:
M = np.atleast2d(np.cov(X, rowvar = False))
if len(M) == 1:
Copy link
Member Author

Choose a reason for hiding this comment

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

Because of the atleast2d, this needs to change

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we be more explicit and check M.size == 1? Would it be possible for M to be a column vector here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah yes you're right thanks
I thought that len counted the number of elements but indeed len(np.array([[1, 2, 3]])) == 1

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@wdevazelhes wdevazelhes changed the title [MRG] Use pseudo-inverse in Covariance [WIP] Use pseudo-inverse in Covariance May 21, 2019
@wdevazelhes wdevazelhes changed the title [WIP] Use pseudo-inverse in Covariance [MRG] Use pseudo-inverse in Covariance May 21, 2019
@wdevazelhes
Copy link
Member Author

I think it should be good to merge now

@@ -14,6 +14,7 @@

from .base_metric import MahalanobisMixin
from ._util import transformer_from_metric
import scipy
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nitpick: move this import up to line 13 (after the numpy import but before the sklearn)

Copy link
Member Author

Choose a reason for hiding this comment

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

That's right, done

Copy link
Member

@bellet bellet left a comment

Choose a reason for hiding this comment

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

LGTM

@bellet
Copy link
Member

bellet commented Jun 5, 2019

Maybe fix the small style issue so we can merge?

@wdevazelhes
Copy link
Member Author

Maybe fix the small style issue so we can merge?

Yep, sorry, it's done now I think we can merge

@bellet bellet merged commit efba316 into scikit-learn-contrib:master Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update covariance
3 participants