Skip to content

Commit ab4e1a6

Browse files
author
William de Vazelhes
committed
API: remove num_labeled parameter
1 parent c5f3175 commit ab4e1a6

File tree

6 files changed

+15
-45
lines changed

6 files changed

+15
-45
lines changed

metric_learn/constraints.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,3 @@ def chunks(self, num_chunks=100, chunk_size=2, random_state=np.random):
8888
raise ValueError('Unable to make %d chunks of %d examples each' %
8989
(num_chunks, chunk_size))
9090
return chunks
91-
92-
@staticmethod
93-
def random_subset(all_labels, num_preserved=np.inf, random_state=np.random):
94-
"""
95-
the random state object to be passed must be a numpy random seed
96-
"""
97-
n = len(all_labels)
98-
num_ignored = max(0, n - num_preserved)
99-
idx = random_state.randint(n, size=num_ignored)
100-
partial_labels = np.array(all_labels, copy=True)
101-
partial_labels[idx] = -1
102-
return Constraints(partial_labels)

metric_learn/itml.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ def metric(self):
143143
class ITML_Supervised(ITML):
144144
"""Information Theoretic Metric Learning (ITML)"""
145145
def __init__(self, gamma=1., max_iter=1000, convergence_threshold=1e-3,
146-
num_labeled=np.inf, num_constraints=None, bounds=None, A0=None,
147-
verbose=False):
146+
num_constraints=None, bounds=None, A0=None, verbose=False):
148147
"""Initialize the learner.
149148
150149
Parameters
@@ -153,8 +152,6 @@ def __init__(self, gamma=1., max_iter=1000, convergence_threshold=1e-3,
153152
value for slack variables
154153
max_iter : int, optional
155154
convergence_threshold : float, optional
156-
num_labeled : int, optional
157-
number of labels to preserve for training
158155
num_constraints: int, optional
159156
number of constraints to generate
160157
bounds : list (pos,neg) pairs, optional
@@ -167,7 +164,6 @@ def __init__(self, gamma=1., max_iter=1000, convergence_threshold=1e-3,
167164
ITML.__init__(self, gamma=gamma, max_iter=max_iter,
168165
convergence_threshold=convergence_threshold,
169166
A0=A0, verbose=verbose)
170-
self.num_labeled = num_labeled
171167
self.num_constraints = num_constraints
172168
self.bounds = bounds
173169

@@ -191,8 +187,7 @@ def fit(self, X, y, random_state=np.random):
191187
num_classes = len(np.unique(y))
192188
num_constraints = 20 * num_classes**2
193189

194-
c = Constraints.random_subset(y, self.num_labeled,
195-
random_state=random_state)
190+
c = Constraints(y)
196191
pos_neg = c.positive_negative_pairs(num_constraints,
197192
random_state=random_state)
198193
return ITML.fit(self, X, pos_neg, bounds=self.bounds)

metric_learn/lsml.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _gradient(self, metric):
132132

133133

134134
class LSML_Supervised(LSML):
135-
def __init__(self, tol=1e-3, max_iter=1000, prior=None, num_labeled=np.inf,
135+
def __init__(self, tol=1e-3, max_iter=1000, prior=None,
136136
num_constraints=None, weights=None, verbose=False):
137137
"""Initialize the learner.
138138
@@ -142,8 +142,6 @@ def __init__(self, tol=1e-3, max_iter=1000, prior=None, num_labeled=np.inf,
142142
max_iter : int, optional
143143
prior : (d x d) matrix, optional
144144
guess at a metric [default: covariance(X)]
145-
num_labeled : int, optional
146-
number of labels to preserve for training
147145
num_constraints: int, optional
148146
number of constraints to generate
149147
weights : (m,) array of floats, optional
@@ -153,7 +151,6 @@ def __init__(self, tol=1e-3, max_iter=1000, prior=None, num_labeled=np.inf,
153151
"""
154152
LSML.__init__(self, tol=tol, max_iter=max_iter, prior=prior,
155153
verbose=verbose)
156-
self.num_labeled = num_labeled
157154
self.num_constraints = num_constraints
158155
self.weights = weights
159156

@@ -177,8 +174,7 @@ def fit(self, X, y, random_state=np.random):
177174
num_classes = len(np.unique(y))
178175
num_constraints = 20 * num_classes**2
179176

180-
c = Constraints.random_subset(y, self.num_labeled,
181-
random_state=random_state)
177+
c = Constraints(y)
182178
pairs = c.positive_negative_pairs(num_constraints, same_length=True,
183179
random_state=random_state)
184180
return LSML.fit(self, X, pairs, weights=self.weights)

metric_learn/mmc.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,17 +384,15 @@ def transformer(self):
384384
class MMC_Supervised(MMC):
385385
"""Mahalanobis Metric for Clustering (MMC)"""
386386
def __init__(self, max_iter=100, max_proj=10000, convergence_threshold=1e-6,
387-
num_labeled=np.inf, num_constraints=None,
388-
A0=None, diagonal=False, diagonal_c=1.0, verbose=False):
387+
num_constraints=None, A0=None, diagonal=False,
388+
diagonal_c=1.0, verbose=False):
389389
"""Initialize the learner.
390390
391391
Parameters
392392
----------
393393
max_iter : int, optional
394394
max_proj : int, optional
395395
convergence_threshold : float, optional
396-
num_labeled : int, optional
397-
number of labels to preserve for training
398396
num_constraints: int, optional
399397
number of constraints to generate
400398
A0 : (d x d) matrix, optional
@@ -413,7 +411,6 @@ def __init__(self, max_iter=100, max_proj=10000, convergence_threshold=1e-6,
413411
convergence_threshold=convergence_threshold,
414412
A0=A0, diagonal=diagonal, diagonal_c=diagonal_c,
415413
verbose=verbose)
416-
self.num_labeled = num_labeled
417414
self.num_constraints = num_constraints
418415

419416
def fit(self, X, y, random_state=np.random):
@@ -434,8 +431,7 @@ def fit(self, X, y, random_state=np.random):
434431
num_classes = len(np.unique(y))
435432
num_constraints = 20 * num_classes**2
436433

437-
c = Constraints.random_subset(y, self.num_labeled,
438-
random_state=random_state)
434+
c = Constraints(y)
439435
pos_neg = c.positive_negative_pairs(num_constraints,
440436
random_state=random_state)
441437
return MMC.fit(self, X, pos_neg)

metric_learn/sdml.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def fit(self, X, W):
8282

8383
class SDML_Supervised(SDML):
8484
def __init__(self, balance_param=0.5, sparsity_param=0.01, use_cov=True,
85-
num_labeled=np.inf, num_constraints=None, verbose=False):
85+
num_constraints=None, verbose=False):
8686
"""
8787
Parameters
8888
----------
@@ -92,8 +92,6 @@ def __init__(self, balance_param=0.5, sparsity_param=0.01, use_cov=True,
9292
trade off between optimizer and sparseness (see graph_lasso)
9393
use_cov : bool, optional
9494
controls prior matrix, will use the identity if use_cov=False
95-
num_labeled : int, optional
96-
number of labels to preserve for training
9795
num_constraints : int, optional
9896
number of constraints to generate
9997
verbose : bool, optional
@@ -102,7 +100,6 @@ def __init__(self, balance_param=0.5, sparsity_param=0.01, use_cov=True,
102100
SDML.__init__(self, balance_param=balance_param,
103101
sparsity_param=sparsity_param, use_cov=use_cov,
104102
verbose=verbose)
105-
self.num_labeled = num_labeled
106103
self.num_constraints = num_constraints
107104

108105
def fit(self, X, y, random_state=np.random):
@@ -129,7 +126,6 @@ def fit(self, X, y, random_state=np.random):
129126
num_classes = len(np.unique(y))
130127
num_constraints = 20 * num_classes**2
131128

132-
c = Constraints.random_subset(y, self.num_labeled,
133-
random_state=random_state)
129+
c = Constraints(y)
134130
adj = c.adjacency_matrix(num_constraints, random_state=random_state)
135131
return SDML.fit(self, X, adj)

test/test_base_metric.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,25 @@ def test_itml(self):
3030
""".strip('\n'))
3131
self.assertEqual(str(metric_learn.ITML_Supervised()), """
3232
ITML_Supervised(A0=None, bounds=None, convergence_threshold=0.001, gamma=1.0,
33-
max_iter=1000, num_constraints=None, num_labeled=inf,
34-
verbose=False)
33+
max_iter=1000, num_constraints=None, verbose=False)
3534
""".strip('\n'))
3635

3736
def test_lsml(self):
3837
self.assertEqual(
3938
str(metric_learn.LSML()),
4039
"LSML(max_iter=1000, prior=None, tol=0.001, verbose=False)")
4140
self.assertEqual(str(metric_learn.LSML_Supervised()), """
42-
LSML_Supervised(max_iter=1000, num_constraints=None, num_labeled=inf,
43-
prior=None, tol=0.001, verbose=False, weights=None)
41+
LSML_Supervised(max_iter=1000, num_constraints=None, prior=None, tol=0.001,
42+
verbose=False, weights=None)
4443
""".strip('\n'))
4544

4645
def test_sdml(self):
4746
self.assertEqual(str(metric_learn.SDML()),
4847
"SDML(balance_param=0.5, sparsity_param=0.01, "
4948
"use_cov=True, verbose=False)")
5049
self.assertEqual(str(metric_learn.SDML_Supervised()), """
51-
SDML_Supervised(balance_param=0.5, num_constraints=None, num_labeled=inf,
52-
sparsity_param=0.01, use_cov=True, verbose=False)
50+
SDML_Supervised(balance_param=0.5, num_constraints=None, sparsity_param=0.01,
51+
use_cov=True, verbose=False)
5352
""".strip('\n'))
5453

5554
def test_rca(self):
@@ -72,7 +71,7 @@ def test_mmc(self):
7271
self.assertEqual(str(metric_learn.MMC_Supervised()), """
7372
MMC_Supervised(A0=None, convergence_threshold=1e-06, diagonal=False,
7473
diagonal_c=1.0, max_iter=100, max_proj=10000, num_constraints=None,
75-
num_labeled=inf, verbose=False)
74+
verbose=False)
7675
""".strip('\n'))
7776

7877
if __name__ == '__main__':

0 commit comments

Comments
 (0)