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
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)
def test_mlkr(self): mlkr = MLKR(num_dims=2) mlkr.fit(self.X, self.y) res_1 = mlkr.transform(self.X) mlkr = MLKR(num_dims=2) res_2 = mlkr.fit_transform(self.X, self.y) assert_array_almost_equal(res_1, res_2)
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 )
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)
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 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())
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)
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_iris(self): mlkr = MLKR() mlkr.fit(self.iris_points, self.iris_labels) csep = class_separation(mlkr.transform(), self.iris_labels) self.assertLess(csep, 0.25)
class MetricLearner: def __init__(self): self._learner = None self._trained = ObservableObject(False) 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) 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 get_learner(self): return self._learner def save_learner(self, path): joblib.dump(self._learner, path + "TreanedModel.sav") def load_learner(self, path): self._learner = joblib.load(path + "TreanedModel.sav") def remove_learner(self, path): os.remove(path + ".sav")
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())
def mlkr(data, label, dim): mlkr = MLKR(num_dims=dim) mlkr.fit(data, label) result = mlkr.transform(data) return result
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())