-
Notifications
You must be signed in to change notification settings - Fork 228
[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
[MRG] Use pseudo-inverse in Covariance #206
Conversation
metric_learn/covariance.py
Outdated
@@ -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)) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, done
metric_learn/covariance.py
Outdated
M = np.cov(X, rowvar = False) | ||
if M.ndim == 0: | ||
M = np.atleast2d(np.cov(X, rowvar = False)) | ||
if len(M) == 1: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
I think it should be good to merge now |
metric_learn/covariance.py
Outdated
@@ -14,6 +14,7 @@ | |||
|
|||
from .base_metric import MahalanobisMixin | |||
from ._util import transformer_from_metric | |||
import scipy |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's right, done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Maybe fix the small style issue so we can merge? |
Yep, sorry, it's done now I think we can merge |
Fixes #204
This PR uses the pseudo inverse in Covariance, rather than the inverse. See #204 (comment)