Ejemplo n.º 1
0
 def test_itml(self):
     def_kwargs = {
         'convergence_threshold': 0.001,
         'gamma': 1.0,
         'max_iter': 1000,
         'preprocessor': None,
         'prior': 'identity',
         'random_state': None,
         'verbose': False
     }
     nndef_kwargs = {'gamma': 0.5}
     merged_kwargs = sk_repr_kwargs(def_kwargs, nndef_kwargs)
     self.assertEqual(remove_spaces(str(metric_learn.ITML(gamma=0.5))),
                      remove_spaces(f"ITML({merged_kwargs})"))
     def_kwargs = {
         'convergence_threshold': 0.001,
         'gamma': 1.0,
         'max_iter': 1000,
         'num_constraints': None,
         'preprocessor': None,
         'prior': 'identity',
         'random_state': None,
         'verbose': False
     }
     nndef_kwargs = {'num_constraints': 7}
     merged_kwargs = sk_repr_kwargs(def_kwargs, nndef_kwargs)
     self.assertEqual(
         remove_spaces(str(
             metric_learn.ITML_Supervised(num_constraints=7))),
         remove_spaces(f"ITML_Supervised({merged_kwargs})"))
Ejemplo n.º 2
0
 def test_itml(self):
     self.assertEqual(remove_spaces(str(metric_learn.ITML(gamma=0.5))),
                      remove_spaces("ITML(gamma=0.5)"))
     self.assertEqual(
         remove_spaces(str(
             metric_learn.ITML_Supervised(num_constraints=7))),
         remove_spaces("ITML_Supervised(num_constraints=7)"))
Ejemplo n.º 3
0
  def test_itml(self):
    self.assertEqual(str(metric_learn.ITML()), """
ITML(A0=None, convergence_threshold=0.001, gamma=1.0, max_iter=1000,
   preprocessor=None, verbose=False)
""".strip('\n'))
    self.assertEqual(str(metric_learn.ITML_Supervised()), """
ITML_Supervised(A0=None, bounds=None, convergence_threshold=0.001, gamma=1.0,
        max_iter=1000, num_constraints=None, num_labeled='deprecated',
        preprocessor=None, verbose=False)
""".strip('\n'))
Ejemplo n.º 4
0
    def test_itml(self):
        self.assertEqual(
            remove_spaces(str(metric_learn.ITML())),
            remove_spaces("""
ITML(A0='deprecated', convergence_threshold=0.001, gamma=1.0,
   max_iter=1000, preprocessor=None, prior='identity', random_state=None,
   verbose=False)
"""))
        self.assertEqual(
            remove_spaces(str(metric_learn.ITML_Supervised())),
            remove_spaces("""
ITML_Supervised(A0='deprecated', bounds='deprecated',
        convergence_threshold=0.001, gamma=1.0,
        max_iter=1000, num_constraints=None, num_labeled='deprecated',
        preprocessor=None, prior='identity', random_state=None, verbose=False)
"""))
Ejemplo n.º 5
0
  def test_string_repr(self):
    # we don't test LMNN here because it could be python_LMNN

    self.assertEqual(str(metric_learn.Covariance()), "Covariance()")

    self.assertEqual(str(metric_learn.NCA()),
                     "NCA(learning_rate=0.01, max_iter=100, num_dims=None)")

    self.assertEqual(str(metric_learn.LFDA()),
                     "LFDA(dim=None, k=7, metric='weighted')")

    self.assertEqual(str(metric_learn.ITML()), """
ITML(convergence_threshold=0.001, gamma=1.0, max_iters=1000, verbose=False)
""".strip('\n'))
    self.assertEqual(str(metric_learn.ITML_Supervised()), """
ITML_Supervised(A0=None, bounds=None, convergence_threshold=0.001, gamma=1.0,
                max_iters=1000, num_constraints=None, num_labeled=inf,
                verbose=False)
""".strip('\n'))

    self.assertEqual(str(metric_learn.LSML()),
                     "LSML(max_iter=1000, tol=0.001, verbose=False)")
    self.assertEqual(str(metric_learn.LSML_Supervised()), """
LSML_Supervised(max_iter=1000, num_constraints=None, num_labeled=inf,
                prior=None, tol=0.001, verbose=False, weights=None)
""".strip('\n'))

    self.assertEqual(str(metric_learn.SDML()), """
SDML(balance_param=0.5, sparsity_param=0.01, use_cov=True, verbose=False)
""".strip('\n'))
    self.assertEqual(str(metric_learn.SDML_Supervised()), """
SDML_Supervised(balance_param=0.5, num_constraints=None, num_labeled=inf,
                sparsity_param=0.01, use_cov=True, verbose=False)
""".strip('\n'))

    self.assertEqual(str(metric_learn.RCA()), "RCA(dim=None)")
    self.assertEqual(str(metric_learn.RCA_Supervised()),
                     "RCA_Supervised(chunk_size=2, dim=None, num_chunks=100)")

    self.assertEqual(str(metric_learn.MLKR()), """
MLKR(A0=None, alpha=0.0001, epsilon=0.01, max_iter=1000, num_dims=None)
""".strip('\n'))
Ejemplo n.º 6
0
######################################################################
# Information Theoretic Metric Learning
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# ITML uses a regularizer that automatically enforces a Semi-Definite
# Positive Matrix condition - the LogDet divergence. It uses soft
# must-link or cannot-link constraints, and a simple algorithm based on
# Bregman projections. Unlike LMNN, ITML will implicitly enforce points from
# the same class to belong to the same cluster, as you can see below.
#
# - See more in the :ref:`User Guide <itml>`
# - See more in the documentation of the class :py:class:`ITML
#   <metric_learn.ITML>`

itml = metric_learn.ITML_Supervised()
X_itml = itml.fit_transform(X, y)

plot_tsne(X_itml, y)

######################################################################
# Mahalanobis Metric for Clustering
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#
# MMC is an algorithm that will try to minimize the distance between similar
# points, while ensuring that the sum of distances between dissimilar points is
# higher than a threshold. This is done by optimizing a cost function
# subject to an inequality constraint.
#
# - See more in the :ref:`User Guide <mmc>`
# - See more in the documentation of the class :py:class:`MMC
Ejemplo n.º 7
0
import numpy as np
from sklearn.datasets import load_iris

import metric_learn

CLASSES = {
    'Covariance':
    metric_learn.Covariance(),
    'ITML_Supervised':
    metric_learn.ITML_Supervised(num_constraints=200),
    'LFDA':
    metric_learn.LFDA(k=2, dim=2),
    'LMNN':
    metric_learn.LMNN(k=5, learn_rate=1e-6, verbose=False),
    'LSML_Supervised':
    metric_learn.LSML_Supervised(num_constraints=200),
    'MLKR':
    metric_learn.MLKR(),
    'NCA':
    metric_learn.NCA(max_iter=700, n_components=2),
    'RCA_Supervised':
    metric_learn.RCA_Supervised(dim=2, num_chunks=30, chunk_size=2),
    'SDML_Supervised':
    metric_learn.SDML_Supervised(num_constraints=1500)
}


class IrisDataset(object):
    params = [sorted(CLASSES)]
    param_names = ['alg']