Skip to content

Implementation of MMC #61

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 7 commits into from
May 26, 2017
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Metric Learning algorithms in Python.
- Local Fisher Discriminant Analysis (LFDA)
- Relative Components Analysis (RCA)
- Metric Learning for Kernel Regression (MLKR)
- Mahalanobis Metric for Clustering (MMC)

**Dependencies**

Expand Down
1 change: 1 addition & 0 deletions metric_learn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
from .lfda import LFDA
from .rca import RCA, RCA_Supervised
from .mlkr import MLKR
from .mmc import MMC, MMC_Supervised
12 changes: 12 additions & 0 deletions metric_learn/_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import numpy as np


# hack around lack of axis kwarg in older numpy versions
try:
np.linalg.norm([[4]], axis=1)
except TypeError:
def vector_norm(X):
return np.apply_along_axis(np.linalg.norm, 1, X)
else:
def vector_norm(X):
return np.linalg.norm(X, axis=1)
19 changes: 5 additions & 14 deletions metric_learn/itml.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from .base_metric import BaseMetricLearner
from .constraints import Constraints
from ._util import vector_norm


class ITML(BaseMetricLearner):
Expand Down Expand Up @@ -54,10 +55,10 @@ def _process_inputs(self, X, constraints, bounds):
self.X_ = X = check_array(X)
# check to make sure that no two constrained vectors are identical
a,b,c,d = constraints
ident = _vector_norm(X[a] - X[b]) > 1e-9
a, b = a[ident], b[ident]
ident = _vector_norm(X[c] - X[d]) > 1e-9
c, d = c[ident], d[ident]
no_ident = vector_norm(X[a] - X[b]) > 1e-9
a, b = a[no_ident], b[no_ident]
no_ident = vector_norm(X[c] - X[d]) > 1e-9
c, d = c[no_ident], d[no_ident]
# init bounds
if bounds is None:
self.bounds_ = np.percentile(pairwise_distances(X), (5, 95))
Expand Down Expand Up @@ -138,16 +139,6 @@ def fit(self, X, constraints, bounds=None):
def metric(self):
return self.A_

# hack around lack of axis kwarg in older numpy versions
try:
np.linalg.norm([[4]], axis=1)
except TypeError:
def _vector_norm(X):
return np.apply_along_axis(np.linalg.norm, 1, X)
else:
def _vector_norm(X):
return np.linalg.norm(X, axis=1)


class ITML_Supervised(ITML):
"""Information Theoretic Metric Learning (ITML)"""
Expand Down
Loading