|
| 1 | +import numpy as np |
| 2 | +from base_metric import BaseMetricLearner |
| 3 | + |
| 4 | + |
| 5 | +class RCA(BaseMetricLearner): |
| 6 | + '''Relevant Components Analysis (RCA) |
| 7 | + 'Learning distance functions using equivalence relations', ICML 2003 |
| 8 | + ''' |
| 9 | + |
| 10 | + def __init__(self, dim=None): |
| 11 | + ''' |
| 12 | + dim : embedding dimension (default: original dimension of data) |
| 13 | + ''' |
| 14 | + self.dim = dim |
| 15 | + |
| 16 | + def transformer(self): |
| 17 | + return self._transformer |
| 18 | + |
| 19 | + def _process_inputs(self, X, Y): |
| 20 | + X = np.asanyarray(X) |
| 21 | + self.X = X |
| 22 | + n, d = X.shape |
| 23 | + |
| 24 | + if self.dim is None: |
| 25 | + self.dim = d |
| 26 | + elif not 0 < self.dim <= d: |
| 27 | + raise ValueError('Invalid embedding dimension, must be in [1,%d]' % d) |
| 28 | + |
| 29 | + Y = np.asanyarray(Y) |
| 30 | + num_chunks = Y.max() + 1 |
| 31 | + |
| 32 | + return X, Y, num_chunks, d |
| 33 | + |
| 34 | + def fit(self, data, chunks): |
| 35 | + ''' |
| 36 | + data : (n,d) array-like, input data |
| 37 | + chunks : (n,) array-like |
| 38 | + chunks[i] == -1 -> point i doesn't belong to any chunklet |
| 39 | + chunks[i] == j -> point i belongs to chunklet j |
| 40 | + ''' |
| 41 | + data, chunks, num_chunks, d = self._process_inputs(data, chunks) |
| 42 | + |
| 43 | + # mean center |
| 44 | + data -= data.mean(axis=0) |
| 45 | + |
| 46 | + # mean center each chunklet separately |
| 47 | + chunk_mask = chunks != -1 |
| 48 | + chunk_data = data[chunk_mask] |
| 49 | + chunk_labels = chunks[chunk_mask] |
| 50 | + for c in xrange(num_chunks): |
| 51 | + mask = chunk_labels == c |
| 52 | + chunk_data[mask] -= chunk_data[mask].mean(axis=0) |
| 53 | + |
| 54 | + # "inner" covariance of chunk deviations |
| 55 | + inner_cov = np.cov(chunk_data, rowvar=0, bias=1) |
| 56 | + |
| 57 | + # Fisher Linear Discriminant projection |
| 58 | + if self.dim < d: |
| 59 | + total_cov = np.cov(data[chunk_mask], rowvar=0) |
| 60 | + tmp = np.linalg.lstsq(total_cov, inner_cov)[0] |
| 61 | + vals, vecs = np.linalg.eig(tmp) |
| 62 | + inds = np.argsort(vals)[:self.dim] |
| 63 | + A = vecs[:,inds] |
| 64 | + inner_cov = A.T.dot(inner_cov).dot(A) |
| 65 | + self._transformer = _inv_sqrtm(inner_cov).dot(A.T) |
| 66 | + else: |
| 67 | + self._transformer = _inv_sqrtm(inner_cov).T |
| 68 | + |
| 69 | + |
| 70 | +def _inv_sqrtm(x): |
| 71 | + '''Computes x^(-1/2)''' |
| 72 | + vals, vecs = np.linalg.eigh(x) |
| 73 | + return (vecs / np.sqrt(vals)).dot(vecs.T) |
0 commit comments