def test_iris(self): num_constraints = 200 C = LSML.prepare_constraints(self.iris_labels, num_constraints) lsml = LSML().fit(self.iris_points, C, verbose=False) csep = class_separation(lsml.transform(), self.iris_labels) self.assertLess(csep, 0.8) # it's pretty terrible
def sandwich_demo(): x, y = sandwich_data() knn = nearest_neighbors(x, k=2) ax = pyplot.subplot(3, 1, 1) # take the whole top row plot_sandwich_data(x, y, ax) plot_neighborhood_graph(x, knn, y, ax) ax.set_title('input space') ax.set_aspect('equal') ax.set_xticks([]) ax.set_yticks([]) num_constraints = 60 mls = [ (LMNN(), (x, y)), (ITML(), (x, ITML.prepare_constraints(y, len(x), num_constraints))), (SDML(), (x, SDML.prepare_constraints(y, len(x), num_constraints))), (LSML(), (x, LSML.prepare_constraints(y, num_constraints))) ] for ax_num, (ml,args) in zip(xrange(3,7), mls): ml.fit(*args) tx = ml.transform() ml_knn = nearest_neighbors(tx, k=2) ax = pyplot.subplot(3,2,ax_num) plot_sandwich_data(tx, y, ax) plot_neighborhood_graph(tx, ml_knn, y, ax) ax.set_title('%s space' % ml.__class__.__name__) ax.set_xticks([]) ax.set_yticks([]) pyplot.show()
def build_quadruplets(with_preprocessor=False): # builds a toy quadruplets problem X, indices = build_data() c = np.column_stack(indices) target = np.ones(c.shape[0]) # quadruplets targets are not used # anyways c, target = shuffle(c, target, random_state=SEED) if with_preprocessor: # if preprocessor, we build a 2D array of quadruplets of indices return Dataset(c, target, X, c[:, 0]) else: # if not, we build a 3D array of quadruplets of samples return Dataset(X[c], target, None, X[c[:, 0]]) quadruplets_learners = [(LSML(), build_quadruplets)] ids_quadruplets_learners = list( map(lambda x: x.__class__.__name__, [learner for (learner, _) in quadruplets_learners])) pairs_learners = [ (ITML(), build_pairs), (MMC(max_iter=2), build_pairs), # max_iter=2 for faster (SDML(), build_pairs), ] ids_pairs_learners = list( map(lambda x: x.__class__.__name__, [learner for (learner, _) in pairs_learners])) classifiers = [(Covariance(), build_classification), (LFDA(), build_classification), (LMNN(), build_classification),
def build_quadruplets(with_preprocessor=False): # builds a toy quadruplets problem X, indices = build_data() c = np.column_stack(indices) target = np.ones(c.shape[0]) # quadruplets targets are not used # anyways c, target = shuffle(c, target, random_state=SEED) if with_preprocessor: # if preprocessor, we build a 2D array of quadruplets of indices return Dataset(c, target, X, c[:, 0]) else: # if not, we build a 3D array of quadruplets of samples return Dataset(X[c], target, None, X[c[:, 0]]) quadruplets_learners = [(LSML(), build_quadruplets)] ids_quadruplets_learners = list( map(lambda x: x.__class__.__name__, [learner for (learner, _) in quadruplets_learners])) pairs_learners = [ (ITML(max_iter=2), build_pairs), # max_iter=2 to be faster (MMC(max_iter=2), build_pairs), # max_iter=2 to be faster (SDML(prior='identity', balance_param=1e-5), build_pairs) ] ids_pairs_learners = list( map(lambda x: x.__class__.__name__, [learner for (learner, _) in pairs_learners])) classifiers = [(Covariance(), build_classification), (LFDA(), build_classification), (LMNN(), build_classification),
def fit(self, X, y): num_constraints = NUM_CONSTRAINTS constraints = LSML.prepare_constraints(y, num_constraints) return super(LSML_sk, self).fit(X, constraints)