def fisher_discriminant(X, Y): model = LFDA() model.fit(X, Y) return model.transform(X), model.metric()
def process_lfda(self, **option): '''Metric Learning algorithm: LFDA''' GeneExp = self.GeneExp_train Label = self.Label_train lfda = LFDA(**option) lfda.fit(GeneExp, Label) self.Trans['LFDA'] = lfda.transformer()
def runLFDA(X_train, X_test, y_train, y_test): transformer = LFDA() transformer.fit(X_train, y_train) X_train_proj = transformer.transform(X_train) X_test_proj = transformer.transform(X_test) np.save('X_train_LFDA', X_train_proj) np.save('X_test_LFDA', X_test_proj) return X_train_proj, X_test_proj
def test_iris(self): lfda = LFDA(k=2, n_components=2) lfda.fit(self.iris_points, self.iris_labels) csep = class_separation(lfda.transform(self.iris_points), self.iris_labels) self.assertLess(csep, 0.15) # Sanity checks for learned matrices. self.assertEqual(lfda.get_mahalanobis_matrix().shape, (4, 4)) self.assertEqual(lfda.components_.shape, (2, 4))
def test_iris(self): lfda = LFDA(k=2, num_dims=2) lfda.fit(self.iris_points, self.iris_labels) csep = class_separation(lfda.transform(), self.iris_labels) self.assertLess(csep, 0.15) # Sanity checks for learned matrices. self.assertEqual(lfda.metric().shape, (4, 4)) self.assertEqual(lfda.transformer().shape, (2, 4))
def test_lfda(self): lfda = LFDA(k=2, n_components=2) lfda.fit(self.X, self.y) res_1 = lfda.transform(self.X) lfda = LFDA(k=2, n_components=2) res_2 = lfda.fit_transform(self.X, self.y) # signs may be flipped, that's okay assert_array_almost_equal(abs(res_1), abs(res_2))
def test_lfda(self): lfda = LFDA(k=2, num_dims=2) lfda.fit(self.X, self.y) res_1 = lfda.transform() lfda = LFDA(k=2, num_dims=2) res_2 = lfda.fit_transform(self.X, self.y) # signs may be flipped, that's okay if np.sign(res_1[0, 0]) != np.sign(res_2[0, 0]): res_2 *= -1 assert_array_almost_equal(res_1, res_2)
def test_lfda(self): lfda = LFDA(k=2, num_dims=2) lfda.fit(self.X, self.y) res_1 = lfda.transform(self.X) lfda = LFDA(k=2, num_dims=2) res_2 = lfda.fit_transform(self.X, self.y) # signs may be flipped, that's okay if np.sign(res_1[0,0]) != np.sign(res_2[0,0]): res_2 *= -1 assert_array_almost_equal(res_1, res_2)
class LFDA: def __init__(self): """Initializes the LFDA model""" self.metric_model = LFDA_ml() self.X_tr = None self.y_train = None self.X_te = None def fit(self, X_tr, y_train): """Fits the model to the prescribed data.""" self.X_tr = X_tr self.y_train = y_train return self.metric_model.fit(X_tr, y_train) def transform(self, X): """Transforms the test data according to the model""" return self.metric_model.transform(X) def predict_proba(self, X_te): """Predicts the probabilities of each of the test samples""" test_samples = X_te.shape[0] self.X_tr = self.transform(self.X_tr) clf = NearestCentroid() clf.fit(self.X_tr, self.y_train) centroids = clf.centroids_ probabilities = np.zeros((test_samples, centroids.shape[0])) for sample in xrange(test_samples): probabilities[sample] = sk_nearest_neighbour_proba( centroids, X_te[sample, :]) return probabilities
def test_preprocessor_supervised(preprocessor, points, y_points): """Tests different ways to use the preprocessor argument: an array, a class callable, and a function callable, with a supervised algorithm """ lfda = LFDA(preprocessor=preprocessor) lfda.fit(points, y_points)
X_train = np.load(osp.join(args.data_root, 'feature_train.npy')) y_train = np.load(osp.join(args.data_root, 'label_train.npy')) X_test = np.load(osp.join(args.data_root, 'feature_test.npy')) y_test = np.load(osp.join(args.data_root, 'label_test.npy')) return X_train, X_test, y_train, y_test if __name__ == '__main__': parser = argparse.ArgumentParser("LFDA") parser.add_argument('--data-root', default='./data/raw_split') parser.add_argument('--n-components', type=int, default=2) args = parser.parse_args() name = f"{args.n_components}" data_save_folder = f"./data/LFDA/{name}" makedirs(data_save_folder) X_train, X_test, y_train, y_test = load_split(args) print(X_train.shape) t = time.time() lfda = LFDA(n_components=args.n_components) lfda.fit(X_train, y_train) np.save(osp.join(data_save_folder, "feature_train.npy"), lfda.transform(X_train)) np.save(osp.join(data_save_folder, "label_train.npy"), y_train) np.save(osp.join(data_save_folder, "feature_test.npy"), lfda.transform(X_test)) np.save(osp.join(data_save_folder, "label_test.npy"), y_test)
def main(params): initialize_results_dir(params.get('results_dir')) backup_params(params, params.get('results_dir')) print('>>> loading data...') X_train, y_train, X_test, y_test = LoaderFactory().create( name=params.get('dataset'), root=params.get('dataset_dir'), random=True, seed=params.getint('split_seed'))() print('<<< data loaded') print('>>> computing psd matrix...') if params.get('algorithm') == 'identity': psd_matrix = np.identity(X_train.shape[1], dtype=X_train.dtype) elif params.get('algorithm') == 'nca': nca = NCA(init='auto', verbose=True, random_state=params.getint('algorithm_seed')) nca.fit(X_train, y_train) psd_matrix = nca.get_mahalanobis_matrix() elif params.get('algorithm') == 'lmnn': lmnn = LMNN(init='auto', verbose=True, random_state=params.getint('algorithm_seed')) lmnn.fit(X_train, y_train) psd_matrix = lmnn.get_mahalanobis_matrix() elif params.get('algorithm') == 'itml': itml = ITML_Supervised(verbose=True, random_state=params.getint('algorithm_seed')) itml.fit(X_train, y_train) psd_matrix = itml.get_mahalanobis_matrix() elif params.get('algorithm') == 'lfda': lfda = LFDA() lfda.fit(X_train, y_train) psd_matrix = lfda.get_mahalanobis_matrix() elif params.get('algorithm') == 'arml': learner = TripleLearner( optimizer=params.get('optimizer'), optimizer_params={ 'lr': params.getfloat('lr'), 'momentum': params.getfloat('momentum'), 'weight_decay': params.getfloat('weight_decay'), }, criterion=params.get('criterion'), criterion_params={'calibration': params.getfloat('calibration')}, n_epochs=params.getint('n_epochs'), batch_size=params.getint('batch_size'), random_initialization=params.getboolean('random_initialization', fallback=False), update_triple=params.getboolean('update_triple', fallback=False), device=params.get('device'), seed=params.getint('learner_seed')) psd_matrix = learner(X_train, y_train, n_candidate_mins=params.getint('n_candidate_mins', fallback=1)) else: raise Exception('unsupported algorithm') print('<<< psd matrix got') np.savetxt(os.path.join(params.get('results_dir'), 'psd_matrix.txt'), psd_matrix)
print("Method: COV", '\n') cov = Covariance().fit(FSTrainData) TFSTestData = cov.transform(FSTestData) print('Transformation Done', '\n') elif Method == 'ITML': print("Method: ITML", '\n') itml = ITML_Supervised(num_constraints=200, A0=None) x = itml.fit(FSTrainData, TrainLabels) TFSTestData = x.transform(FSTestData) print('Transformation Done', '\n') elif Method == 'LFDA': print("Method: LFDA", '\n') lfda = LFDA(k=4, dim=1) x = lfda.fit(FSTrainData, TrainLabels) TFSTestData = x.transform(FSTestData) print('Transformation Done', '\n') elif Method == 'NCA': print("Method: NCA", '\n') #print('Max', TrainData.max(axis=0)) #print('sssssssss', len(TrainData[0])) #print('sssssssss', len(TrainData.max(axis=0))) #print('Min', TrainData.min(axis=0)) nca = NCA(max_iter=500, learning_rate=0.01) # print('ssssssss', TrainData) x = nca.fit(FSTrainData, TrainLabels) TFSTestData = x.transform(FSTestData)
def test_lfda(self): lfda = LFDA(k=2, num_dims=2) lfda.fit(self.X, self.y) L = lfda.transformer_ assert_array_almost_equal(L.T.dot(L), lfda.get_mahalanobis_matrix())
def test_lfda(self): lfda = LFDA(k=2, n_components=2) lfda.fit(self.X, self.y) L = lfda.components_ assert_array_almost_equal(L.T.dot(L), lfda.get_mahalanobis_matrix())
def test_iris(self): lfda = LFDA(k=2, dim=2) lfda.fit(self.iris_points, self.iris_labels) csep = class_separation(lfda.transform(), self.iris_labels) self.assertLess(csep, 0.15)
pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X_train) print("done in %0.3fs" % (time() - t0)) eigenfaces = pca.components_.reshape((n_components, h, w)) print("Projecting the input data on the eigenfaces orthonormal basis") t0 = time() X_train_pca = pca.transform(X_train) X_test_pca = pca.transform(X_test) print("done in %0.3fs" % (time() - t0)) # Try LMNN here. print("Trying LFDA") param_grid = {''} lfda = LFDA() lfda = lfda.fit(X_train_pca, y_train) X_tr = lfda.transform(X_train_pca) X_te = lfda.transform(X_test_pca) acc, y_pred = classifier.sk_nearest_neighbour(X_tr, y_train, X_te, y_test) print("accuracy = %s", acc) print(classification_report(y_test, y_pred, target_names=target_names)) print(confusion_matrix(y_test, y_pred, labels=range(n_classes))) ############################################################################### # Train a SVM classification model print("Fitting the classifier to the training set") t0 = time() param_grid = { 'C': [1e3, 5e3, 1e4, 5e4, 1e5],
def lfda(data, label, dim): lcda = LFDA(k=2, num_dims=dim) lcda.fit(data, label) result = lcda.transform(data) return result
def test_lfda(self): lfda = LFDA(k=2, num_dims=2) lfda.fit(self.X, self.y) L = lfda.transformer_ assert_array_almost_equal(L.T.dot(L), lfda.metric())
def test_lfda(self): lfda = LFDA(k=2, num_dims=2) lfda.fit(self.X, self.y) L = lfda.transformer() assert_array_almost_equal(L.T.dot(L), lfda.metric())