def test_convergence_simple_example(capsys):
  # LMNN should converge on this simple example, which it did not with
  # this issue: https://github.com/metric-learn/metric-learn/issues/88
  X, y = make_classification(random_state=0)
  lmnn = python_LMNN(verbose=True)
  lmnn.fit(X, y)
  out, _ = capsys.readouterr()
  assert "LMNN converged with objective" in out
示例#2
0
def test_convergence_simple_example(capsys):
    # LMNN should converge on this simple example, which it did not with
    # this issue: https://github.com/metric-learn/metric-learn/issues/88
    X, y = make_classification(random_state=0)
    lmnn = python_LMNN(verbose=True)
    lmnn.fit(X, y)
    out, _ = capsys.readouterr()
    assert "LMNN converged with objective" in out
示例#3
0
    def _process_inputs(self, X, labels):
        num_pts = X.shape[0]
        assert len(labels) == num_pts
        unique_labels, self.label_inds = np.unique(labels, return_inverse=True)
        self.labels = np.arange(len(unique_labels))
        self.X = X

        # self.L = np.eye(X.shape[1])
        model_lmnn = lmnn.python_LMNN(k=4, min_iter=50, learn_rate=1e-7)
        model_lmnn.fit(X=X[:100], labels=labels[:100])
        self.L = model_lmnn.transformer()
def test_no_twice_same_objective(capsys):
  # test that the objective function never has twice the same value
  # see https://github.com/metric-learn/metric-learn/issues/88
  X, y = make_classification(random_state=0)
  lmnn = python_LMNN(verbose=True)
  lmnn.fit(X, y)
  out, _ = capsys.readouterr()
  lines = re.split("\n+", out)
  # we get only objectives from each line:
  # the regexp matches a float that follows an integer (the iteration
  # number), and which is followed by a (signed) float (delta obj). It
  # matches for instance:
  # 3 **1113.7665747189938** -3.182774197440267 46431.0200999999999998e-06
  objectives = [re.search("\d* (?:(\d*.\d*))[ | -]\d*.\d*", s)
                for s in lines]
  objectives = [match.group(1) for match in objectives if match is not None]
  # we remove the last element because it can be equal to the penultimate
  # if the last gradient update is null
  assert len(objectives[:-1]) == len(set(objectives[:-1]))
示例#5
0
def test_no_twice_same_objective(capsys):
    # test that the objective function never has twice the same value
    # see https://github.com/metric-learn/metric-learn/issues/88
    X, y = make_classification(random_state=0)
    lmnn = python_LMNN(verbose=True)
    lmnn.fit(X, y)
    out, _ = capsys.readouterr()
    lines = re.split("\n+", out)
    # we get only objectives from each line:
    # the regexp matches a float that follows an integer (the iteration
    # number), and which is followed by a (signed) float (delta obj). It
    # matches for instance:
    # 3 **1113.7665747189938** -3.182774197440267 46431.0200999999999998e-06
    objectives = [
        re.search("\d* (?:(\d*.\d*))[ | -]\d*.\d*", s) for s in lines
    ]
    objectives = [match.group(1) for match in objectives if match is not None]
    # we remove the last element because it can be equal to the penultimate
    # if the last gradient update is null
    assert len(objectives[:-1]) == len(set(objectives[:-1]))
示例#6
0
train_sets_my_model_100 = my_model_100.transform(data[:110])
test_sets_my_model_100 = my_model_100.transform(data[110:])

knn_my_mode_100 = neighbors.KNeighborsClassifier(n_neighbors=3)
knn_my_mode_100.fit(train_sets_my_model_100, target[:110])
print(my_model_100.metric())
print("knn_before:", knn_before.score(data[110:], target[110:]))
print("knn_after:", knn_my_mode_100.score(test_sets_my_model_100,
                                          target[110:]))
print("my_mode_100 improve:",(knn_my_mode_100.score(test_sets_my_model_100,target[110:])-
       knn_before.score(data[110:],target[110:]))/\
      knn_before.score(data[110:],target[110:])*100,"%")
'''
LMNN
'''
lmnn = lmnn.python_LMNN(k=3, learn_rate=1e-6)
#lmnn =  My_model.python_My_ML(learn_rate=1,alpha=1,max_iter=10)
lmnn.fit(data[:110], target[:110])
#print(lmnn.metric())
train_sets_lmnn = lmnn.transform(data[:110])
test_sets_lmnn = lmnn.transform(data[110:])

knn_lmnn = neighbors.KNeighborsClassifier(n_neighbors=3)
knn_lmnn.fit(train_sets_lmnn, target[:110])
print(lmnn.metric())
print("knn_before:", knn_before.score(data[110:], target[110:]))
print("knn_after:", knn_lmnn.score(test_sets_lmnn, target[110:]))
print("lmnn improve:",(knn_lmnn.score(test_sets_lmnn,target[110:])-
       knn_before.score(data[110:],target[110:]))/\
      knn_before.score(data[110:],target[110:])*100,"%")
示例#7
0
    '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, learning_rate=0.01, num_dims=2),
    'RCA_Supervised': metric_learn.RCA_Supervised(dim=2, num_chunks=30,
                                                  chunk_size=2),
    'SDML_Supervised': metric_learn.SDML_Supervised(num_constraints=1500),
}

try:
  from metric_learn.lmnn import python_LMNN
  if python_LMNN is not metric_learn.LMNN:
    CLASSES['python_LMNN'] = python_LMNN(k=5, learn_rate=1e-6, verbose=False)
except ImportError:
  pass


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

  def setup(self, alg):
    iris_data = load_iris()
    self.iris_points = iris_data['data']
    self.iris_labels = iris_data['target']

  def time_fit(self, alg):
    np.random.seed(5555)
示例#8
0
    '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, num_dims=2),
    'RCA_Supervised': metric_learn.RCA_Supervised(dim=2, num_chunks=30,
                                                  chunk_size=2),
    'SDML_Supervised': metric_learn.SDML_Supervised(num_constraints=1500),
}

try:
  from metric_learn.lmnn import python_LMNN
  if python_LMNN is not metric_learn.LMNN:
    CLASSES['python_LMNN'] = python_LMNN(k=5, learn_rate=1e-6, verbose=False)
except ImportError:
  pass


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

  def setup(self, alg):
    iris_data = load_iris()
    self.iris_points = iris_data['data']
    self.iris_labels = iris_data['target']

  def time_fit(self, alg):
    np.random.seed(5555)