def test_mlkr(self):
    mlkr = MLKR(n_components=2)
    mlkr.fit(self.X, self.y)
    res_1 = mlkr.transform(self.X)

    mlkr = MLKR(n_components=2)
    res_2 = mlkr.fit_transform(self.X, self.y)

    assert_array_almost_equal(res_1, res_2)
예제 #2
0
def NW_MLTrain( g, i, dataset, data, cv = False ):
    '''
	Training Code for the Nadarya-Watson method with a learnt metric NW(ML)
    Parameters
    ----------
    g : object
        Object of class My_Globals in globals.py that contains all the global variables
        
    i : int
        Index of the dataset in datasetList.
        
    dataset : string
        Name of the dataset, for example DD1(Jun).csv
        
    data : dataframe
        Training data.
        
    cv : bool, optional
        Used for cross-validation. The default is False.

    Returns
    -------
    model_ox : model for O3 calibration
        
    model_no2 : model for NO2 calibration
        
    X : Training features
    
    y_ox : reference O3 levels
    
    y_no2 : reference NO2 levels
    '''
    maxTrain = 3000
    maxCV = 1000
    n = data.shape[0]
    if cv:
        nTrain = min( n, maxCV )
    else:
        nTrain = min( n, maxTrain )
        
    X = data.iloc[:nTrain, 2:]
    y_ox = data.iloc[:nTrain, 0]
    y_no2 = data.iloc[:nTrain, 1]
    
    # For O3
    mlkr_ox = MLKR()
    temp = mlkr_ox.fit( X, y_ox )
    model_ox = temp.components_
    
    # For NO2
    mlkr_no2 = MLKR()
    temp = mlkr_no2.fit( X, y_no2 )
    model_no2 = temp.components_
    
    return ( model_ox, model_no2, X, y_ox, y_no2 )
예제 #3
0
def train(X_train, Y_train, is_verbose=False):
    """
    Trains the metric learning model on the input training sample.
    Inputs - 
    X_train - nx4 numpy array of site parameters
    Y_train - nx4 numpy array of activation barriers of sites
    is_verbose - boolean, whether to print training progress (i.e. cost as a function of training steps)

    returns - 
    M - 4x4 numpy array of the Mahalanobis distance matrix
    model_predicted_barriers - barriers of sites predicted by the metric learning. 
                               The barriers of the training set are predicted by excluding itself
    residuals - numpy array of true barrier - predicted barrier 
    """
    mlkr = MLKR(verbose=is_verbose)
    fit_final = mlkr.fit(X_train, Y_train)
    M = fit_final.metric()
    model_predicted_barriers = []
    Y_train =  [Decimal(Y_train[i]) for i in range(0,len(Y_train))]
    for i in range(0,len(Y_train)):
        test = X_train[i]
        temp = np.delete(X_train,i,axis=0)
        delta = temp-test
        dist_mat = np.diagonal(np.matmul(np.matmul(delta,M),np.transpose(delta)))
        dist_mat = np.transpose([Decimal(dist_mat[i]) for i in range(len(dist_mat))])
        k_mat = np.exp(-1*dist_mat)
        temp2 = np.delete(Y_train,i,axis=0)
        model_predicted_barriers.append(np.sum(np.multiply(temp2,k_mat))/(np.sum(k_mat)))
    model_predicted_barriers  = np.asarray(model_predicted_barriers)
    residuals = Y_train - model_predicted_barriers 
    return M, model_predicted_barriers, residuals
def runMLKR(X_train, X_test, y_train, y_test):
    transformer = MLKR(verbose=True)
    transformer.fit(X_train, y_train)
    X_train_proj = transformer.transform(X_train)
    X_test_proj = transformer.transform(X_test)
    np.save('X_train_MLKR', X_train_proj)
    np.save('X_test_MLKR', X_test_proj)
    return X_train_proj, X_test_proj
예제 #5
0
def get_embedding(args, rescaled_domain_lst, domain_name_lst, eval_lst):
    if (args.embedding == "origin") or (args.embedding == "mds" and args.embedding_distance == "heuristic"):
        return rescaled_domain_lst
    elif (args.embedding == "bleu") or (args.embedding == "mds" and args.embedding_distance == "bleudif"):
        return [[e] for e in eval_lst]
    elif (args.embedding == "ml"):
        mlkr = MLKR()
        x = np.array(rescaled_domain_lst)
        y = np.array(eval_lst)
        mlkr.fit(x, y)
        return mlkr.transform(x)
예제 #6
0
    def train_metric_learner_supervised(self, landmark_fin_ok,
                                        landmark_fin_bad, path):

        landmarks = np.concatenate((landmark_fin_ok, landmark_fin_bad))
        landmarks = landmarks.reshape(landmarks.shape[0],
                                      landmarks.shape[1] * landmarks.shape[2])
        labels = []

        for i in range(len(landmark_fin_ok)):
            labels.append(1)
        for i in range(len(landmark_fin_bad)):
            labels.append(-1)

        self._learner = MLKR(preprocessor=landmarks)
        self._learner.fit(landmarks, labels)
        self.save_learner(path)
예제 #7
0
  def test_finite_differences(self):
    """Test gradient of loss function

    Assert that the gradient is almost equal to its finite differences
    approximation.
    """
    # Initialize the transformation `M`, as well as `X`, and `y` and `MLKR`
    X, y = make_regression(n_features=4, random_state=1, n_samples=20)
    X, y = check_X_y(X, y)
    M = np.random.randn(2, X.shape[1])
    mlkr = MLKR()
    mlkr.n_iter_ = 0

    def fun(M):
      return mlkr._loss(M, X, y)[0]

    def grad_fn(M):
      return mlkr._loss(M, X, y)[1].ravel()

    # compute relative error
    rel_diff = check_grad(fun, grad_fn, M.ravel()) / np.linalg.norm(grad_fn(M))
    np.testing.assert_almost_equal(rel_diff, 0.)
예제 #8
0
    def train_metric_learner_supervised_all_landmarks(self, landmark_init_ok,
                                                      landmark_fin_ok,
                                                      landmark_fin_bad, path):
        landmarks = np.concatenate(
            (landmark_init_ok, landmark_fin_ok, landmark_fin_bad))
        landmarks = landmarks.reshape(landmarks.shape[0],
                                      landmarks.shape[1] * landmarks.shape[2])
        labels = []

        for i in range(len(landmark_init_ok)):
            labels.append(0)
        for i in range(len(landmark_fin_ok)):
            labels.append(1)
        for i in range(len(landmark_fin_bad)):
            labels.append(-1)

        # Utilizzo anche i video corretti degli altri esercizi
        for categoria in os.listdir("Exercises"):
            cat_path = Path("Exercises/" + categoria)
            if cat_path.is_dir():
                for name in os.listdir(cat_path):
                    ex_path = Path("Exercises/" + categoria + "/" + name)
                    if ex_path.is_dir() and ex_path != Path(path):
                        pathCorrect = Path("Exercises/" + categoria + "/" +
                                           name + "/Correct")
                        for file in os.listdir(pathCorrect):
                            if file.endswith(".npy"):
                                pathFile = str(pathCorrect) + "\\" + file
                                landmark = np.load(
                                    pathFile,
                                    allow_pickle=True)[38].reshape(1, 68 * 2)
                                landmarks = np.concatenate(
                                    (landmarks, landmark))
                                labels.append(-1)

        self._learner = MLKR(preprocessor=landmarks)
        self._learner.fit(landmarks, labels)
        self.save_learner(path)
 def test_mlkr(self):
   mlkr = MLKR(n_components=2)
   mlkr.fit(self.X, self.y)
   L = mlkr.components_
   assert_array_almost_equal(L.T.dot(L), mlkr.get_mahalanobis_matrix())
예제 #10
0
 def test_iris(self):
     mlkr = MLKR()
     mlkr.fit(self.iris_points, self.iris_labels)
     csep = class_separation(mlkr.transform(self.iris_points),
                             self.iris_labels)
     self.assertLess(csep, 0.25)
예제 #11
0
 def test_mlkr(self):
     check_estimator(MLKR())
예제 #12
0
    map(lambda x: x.__class__.__name__,
        [learner for (learner, _) in pairs_learners]))

classifiers = [(Covariance(), build_classification),
               (LFDA(), build_classification), (LMNN(), build_classification),
               (NCA(), build_classification), (RCA(), build_classification),
               (ITML_Supervised(max_iter=5), build_classification),
               (LSML_Supervised(), build_classification),
               (MMC_Supervised(max_iter=5), build_classification),
               (RCA_Supervised(num_chunks=10), build_classification),
               (SDML_Supervised(), build_classification)]
ids_classifiers = list(
    map(lambda x: x.__class__.__name__,
        [learner for (learner, _) in classifiers]))

regressors = [(MLKR(), build_regression)]
ids_regressors = list(
    map(lambda x: x.__class__.__name__,
        [learner for (learner, _) in regressors]))

WeaklySupervisedClasses = (_PairsClassifierMixin, _QuadrupletsClassifierMixin)

tuples_learners = pairs_learners + quadruplets_learners
ids_tuples_learners = ids_pairs_learners + ids_quadruplets_learners

supervised_learners = classifiers + regressors
ids_supervised_learners = ids_classifiers + ids_regressors

metric_learners = tuples_learners + supervised_learners
ids_metric_learners = ids_tuples_learners + ids_supervised_learners
예제 #13
0
        [learner for (learner, _) in pairs_learners]))

classifiers = [(Covariance(), build_classification),
               (LFDA(), build_classification), (LMNN(), build_classification),
               (NCA(), build_classification), (RCA(), build_classification),
               (ITML_Supervised(max_iter=5), build_classification),
               (LSML_Supervised(), build_classification),
               (MMC_Supervised(max_iter=5), build_classification),
               (RCA_Supervised(num_chunks=5), build_classification),
               (SDML_Supervised(prior='identity',
                                balance_param=1e-5), build_classification)]
ids_classifiers = list(
    map(lambda x: x.__class__.__name__,
        [learner for (learner, _) in classifiers]))

regressors = [(MLKR(init='pca'), build_regression)]
ids_regressors = list(
    map(lambda x: x.__class__.__name__,
        [learner for (learner, _) in regressors]))

WeaklySupervisedClasses = (_PairsClassifierMixin, _QuadrupletsClassifierMixin)

tuples_learners = pairs_learners + quadruplets_learners
ids_tuples_learners = ids_pairs_learners + ids_quadruplets_learners

supervised_learners = classifiers + regressors
ids_supervised_learners = ids_classifiers + ids_regressors

metric_learners = tuples_learners + supervised_learners
ids_metric_learners = ids_tuples_learners + ids_supervised_learners
 def test_mlkr(self):
   mlkr = MLKR(num_dims=2)
   mlkr.fit(self.X, self.y)
   L = mlkr.transformer_
   assert_array_almost_equal(L.T.dot(L), mlkr.get_mahalanobis_matrix())
 def test_mlkr(self):
   mlkr = MLKR(num_dims=2)
   mlkr.fit(self.X, self.y)
   L = mlkr.transformer_
   assert_array_almost_equal(L.T.dot(L), mlkr.metric())
예제 #16
0
 def mlkr(data, label, dim):
     mlkr = MLKR(num_dims=dim)
     mlkr.fit(data, label)
     result = mlkr.transform(data)
     return result