def runMMC(): # Run MMC from metric_learn import MMC """ Learn MMC (Mahalanobis Metrics for Clustering) Model """ mmc = MMC() mmc.fit(pairs, y) # learn the MMC model print("Mahalanobis Matrix : ", mmc.get_mahalanobis_matrix())
def test_iris(self): # Generate full set of constraints for comparison with reference # implementation mask = self.iris_labels[None] == self.iris_labels[:, None] a, b = np.nonzero(np.triu(mask, k=1)) c, d = np.nonzero(np.triu(~mask, k=1)) # Full metric n_features = self.iris_points.shape[1] mmc = MMC(convergence_threshold=0.01, init=np.eye(n_features) / 10) mmc.fit(*wrap_pairs(self.iris_points, [a, b, c, d])) expected = [[+0.000514, +0.000868, -0.001195, -0.001703], [+0.000868, +0.001468, -0.002021, -0.002879], [-0.001195, -0.002021, +0.002782, +0.003964], [-0.001703, -0.002879, +0.003964, +0.005648]] assert_array_almost_equal(expected, mmc.get_mahalanobis_matrix(), decimal=6) # Diagonal metric mmc = MMC(diagonal=True) mmc.fit(*wrap_pairs(self.iris_points, [a, b, c, d])) expected = [0, 0, 1.210220, 1.228596] assert_array_almost_equal(np.diag(expected), mmc.get_mahalanobis_matrix(), decimal=6) # Supervised Full mmc = MMC_Supervised() mmc.fit(self.iris_points, self.iris_labels) csep = class_separation(mmc.transform(self.iris_points), self.iris_labels) self.assertLess(csep, 0.15) # Supervised Diagonal mmc = MMC_Supervised(diagonal=True) mmc.fit(self.iris_points, self.iris_labels) csep = class_separation(mmc.transform(self.iris_points), self.iris_labels) self.assertLess(csep, 0.2)
def test_iris(self): # Generate full set of constraints for comparison with reference implementation n = self.iris_points.shape[0] mask = (self.iris_labels[None] == self.iris_labels[:,None]) a, b = np.nonzero(np.triu(mask, k=1)) c, d = np.nonzero(np.triu(~mask, k=1)) # Full metric mmc = MMC(convergence_threshold=0.01) mmc.fit(*wrap_pairs(self.iris_points, [a,b,c,d])) expected = [[+0.000514, +0.000868, -0.001195, -0.001703], [+0.000868, +0.001468, -0.002021, -0.002879], [-0.001195, -0.002021, +0.002782, +0.003964], [-0.001703, -0.002879, +0.003964, +0.005648]] assert_array_almost_equal(expected, mmc.get_mahalanobis_matrix(), decimal=6) # Diagonal metric mmc = MMC(diagonal=True) mmc.fit(*wrap_pairs(self.iris_points, [a,b,c,d])) expected = [0, 0, 1.210220, 1.228596] assert_array_almost_equal(np.diag(expected), mmc.get_mahalanobis_matrix(), decimal=6) # Supervised Full mmc = MMC_Supervised() mmc.fit(self.iris_points, self.iris_labels) csep = class_separation(mmc.transform(self.iris_points), self.iris_labels) self.assertLess(csep, 0.15) # Supervised Diagonal mmc = MMC_Supervised(diagonal=True) mmc.fit(self.iris_points, self.iris_labels) csep = class_separation(mmc.transform(self.iris_points), self.iris_labels) self.assertLess(csep, 0.2)
# in this task we want points where the first feature is close to be closer to each other, # no matter how close the second feature is y = [1, 1, -1, -1] """ Learn MMC (Mahalanobis Metrics for Clustering) Model """ mmc = MMC() mmc.fit(pairs, y) # learn the MMC model """ Return the decision function used to classify the pairs """ print("debug 1: ", mmc.decision_function(pairs)) """ Returns a copy of the Mahalanobis matrix learned by the metric learner """ print("debug 2: ", mmc.get_mahalanobis_matrix()) """ Returns a function that takes as input two 1D arrays and outputs the learned metric score on these two points. """ f = mmc.get_metric() print("debug 3: ", f) """ Predicts the learned metric between input pairs """ example_pairs = [ [[1.2, 7.5], [1.3, 8.5]] ] #[1.2, 7.5] # error - ValueError: 3D array of formed tuples expected by MMC. print("debug 4 : ", mmc.predict(example_pairs))
pairs.append(pair) y.append(1) for row2 in normalized_n_pairs.iterrows(): print('row2 : ', row2) lon2 = row2[1]["lon_0"] lat2 = row2[1]["lat_0"] alt2 = row2[1]["properties_alt_m"] gt_tuple2 = [lon2, lat2, alt2] obs_lon2 = row2[1]["position::longitude_degrees"] obs_lat2 = row2[1]["position::latitude_degrees"] obs_alt2 = row2[1]["position::altitude_meters"] obs_tuple2 = [obs_lon2, obs_lat2, obs_alt2] pair2 = [gt_tuple2, obs_tuple2] pairs.append(pair2) y.append(-1) print('debug : pairs >> ', pairs) print('debug : y >> ', y) # Run MMC from metric_learn import MMC """ Learn MMC (Mahalanobis Metrics for Clustering) Model """ mmc = MMC() mmc.fit(pairs, y) # learn the MMC model print("Mahalanobis Matrix : ", mmc.get_mahalanobis_matrix())