def test_pipeline(): # check that LocallyLinearEmbedding works fine as a Pipeline from sklearn import pipeline, datasets iris = datasets.load_iris() clf = pipeline.Pipeline([('filter', manifold.LocallyLinearEmbedding()), ('clf', neighbors.KNeighborsClassifier())]) clf.fit(iris.data, iris.target) assert_lower(.7, clf.score(iris.data, iris.target))
def __init__(self, n_components, n_neighbors, n_jobs=1): self.mfld_obj = manifold.LocallyLinearEmbedding( n_neighbors=n_neighbors, n_components=n_components, method="hessian", eigen_solver="dense", n_jobs=n_jobs) super(HLLE_Extractor, self).__init__()
def run_lle(*data): x, y = data for n in [4, 3, 2, 1]: print("-" * 50) lle = manifold.LocallyLinearEmbedding(n_components=n) print("训练模型") lle.fit(x) print("重构误差:", (n, str(lle.reconstruction_error_)))
def LLE_embedding(self, n_neighbors=5, n_components=2, reg=0.001, eigen_solver='auto', tol=1e-06, max_iter=100, method='standard', hessian_tol=0.0001, modified_tol=1e-12, neighbors_algorithm='auto', random_state=None, n_jobs=1): new_lle = manifold.LocallyLinearEmbedding(n_neighbors, n_components, reg, eigen_solver, tol, max_iter, method, hessian_tol, modified_tol, neighbors_algorithm, random_state, n_jobs) self.LLE_embedding = new_lle.fit_transform(self.distance_matrix) self.feature_matrix_dict['lle'] = n_components
def myLLE(X,y): t1 = clock() n_neighbors = 30 clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='standard') clf.fit(X,y) newRep = clf.transform(X) t2 = clock() return t2-t1
def LLE(method, n_neighbors=10, n_components=2): ''' :param method 'standard', 'hessian', 'modified' or 'ltsa' ''' Y = manifold.LocallyLinearEmbedding(n_neighbors, n_components, eigen_solver='auto', method=method) return Y
def do_LLE(X, Y, mth='standard', n_nbrs=5): from sklearn import manifold X_LLE = manifold.LocallyLinearEmbedding(n_neighbors = n_nbrs,\ n_components=2, method=mth).fit_transform(X) do_plot(X_LLE[:, 0], X_LLE[:, 1], Y) return
def test_get_feature_names_out(): """Check get_feature_names_out for LocallyLinearEmbedding.""" X, y = make_blobs(random_state=0, n_features=4) n_components = 2 iso = manifold.LocallyLinearEmbedding(n_components=n_components) iso.fit(X) names = iso.get_feature_names_out() assert_array_equal( [f"locallylinearembedding{i}" for i in range(n_components)], names)
def MLLE(self, source): min_max_scaler = preprocessing.MinMaxScaler() data_source = min_max_scaler.fit_transform(source) lle = manifold.LocallyLinearEmbedding(n_components=2, n_neighbors=25, method='modified') result = {} result['data'] = lle.fit_transform(data_source) result['params'] = 0 return result
def orderPoints_lle(feature_vector, centroid): lle = manifold.LocallyLinearEmbedding(n_components=1, eigen_solver='auto', method='standard') one_d_proj = lle.fit_transform(feature_vector) feature_vector['one_d_proj'] = one_d_proj feature_vector = feature_vector.sort_values(['one_d_proj'], ascending=['False']) del feature_vector['one_d_proj'] return feature_vector
def test_LocallyLinearEmbedding(*data): """ 测试局部线性嵌入的效果 """ x, y = data for n in [4, 3, 2, 1]: lle = manifold.LocallyLinearEmbedding(n_components=n) lle.fit(x) print("reconstruction_error(n_components=%d):%s" %\ (n, lle.reconstruction_error_))
def test_pipeline(): # check that LocallyLinearEmbedding works fine as a Pipeline # only checks that no error is raised. # TODO check that it actually does something useful from sklearn import pipeline, datasets X, y = datasets.make_blobs(random_state=0) clf = pipeline.Pipeline([('filter', manifold.LocallyLinearEmbedding()), ('clf', neighbors.KNeighborsClassifier())]) clf.fit(X, y) assert_less(.9, clf.score(X, y))
def test_LocallyLinearEmbedding(*data): ''' 测试 LocallyLinearEmbedding 的用法 ''' X, y = data for n in [4, 3, 2, 1]: # 依次考察降维目标为 4维、3维、2维、1维 lle = manifold.LocallyLinearEmbedding(n_components=n) lle.fit(X) print('reconstruction_error(n_components=%d) : %s' % (n, lle.reconstruction_error_))
def ltsa_embedding(): print("Computing LTSA embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='ltsa') t0 = time() X_ltsa = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_ltsa, "Local Tangent Space Alignment of the digits (time %.2fs)" % (time() - t0))
def lle(data, labels, new_dimension): print "lle..." if hasattr(data, "toarray"): data = data.toarray() start = time.time() lle = manifold.LocallyLinearEmbedding(n_components=new_dimension) # n_neighbors= int(new_dimension/2), reduced = lle.fit_transform(data) end = time.time() return (reduced, end-start)
def test_LocallyLinearEmbedding(*data): ''' 测试 LocallyLinearEmbedding 的用法 :param data: 可变参数。它是一个元组,这里要求其元素依次为:训练样本集、训练样本的标记 ''' X, y = data for n in [4, 3, 2, 1]: # 依次考察降维目标为 4维、3维、2维、1维 lle = manifold.LocallyLinearEmbedding(n_components=n) lle.fit(X) print('reconstruction_error(n_components=%d) : %s' % (n, lle.reconstruction_error_))
def Z3_master(Input_object_corpora, time_data_loc, input_type="time_series", is_symbolic=True, with_class_labels=False, partition=None, get_embedding=True, get_classification=False, classification_algorithm="KNN", Classification_algorithm_parameters=None, generate_features=False, num_features=2): ''' Ishanu's description: Master primitive encompassing Z3, Z3-C, Z3-E, Z3-F as well as the packaged versions with ZS-ZQ- preprocessing Input_object_corpora can be of type time_series (continuous valued, or Tn valued), or can be of type X (See definition in Table 2), when ZS, ZQ will be called internally Calls embedding algorithm on the distance_matrix and creates Z3_object to store this information Inputs - Input object corpora (np n x n array): output of data smashing time_data_loc (relative directory path as string): path to input of data smashing code Outputs - Z3_obj (object): Z3 object containing information about input dataset ''' sp.Popen('./bin/embed -f ' + time_data_loc, shell=True).wait() sippl_embed = numpy.loadtxt(fname="outE.txt", dtype=float) sippl_feat = sippl_embed[:, :num_features] n_components, n_neighbors = num_features, 10 se = manifold.SpectralEmbedding(n_components=n_components, n_neighbors=n_neighbors) se_fit = se.fit_transform(Input_object_corpora) mds = manifold.MDS(n_components, max_iter=100, n_init=1) mds_fit = mds.fit_transform(Input_object_corpora) lle = manifold.LocallyLinearEmbedding(n_components=n_components, n_neighbors=n_neighbors) lle_fit = lle.fit_transform(Input_object_corpora) iso = manifold.Isomap(n_components=n_components, n_neighbors=n_neighbors) iso_fit = iso.fit_transform(Input_object_corpora) embed_dict = {"sippl_emb":sippl_embed, "spec_emb":se_fit, "mds_emb":mds_fit, "lle_emb":lle_fit, "iso_emb":iso_fit} features_dict = {"sippl":sippl_feat, "spec":se_fit,"mds":mds_fit, "lle":lle_fit, "iso":iso_fit} kmeans_fit = cluster.KMeans().fit_transform(Input_object_corpora) result_obj = Z3_O(dist_matrix=Input_object_corpora, embed_matrix_dict=embed_dict, feat_matrix_dict=features_dict, clus_map=kmeans_fit) return result_obj
def lle_analysis(self, input_x): ss_x = StandardScaler().fit_transform(input_x) norm_x = normalize(input_x, axis=0) n_neighbors = 5 # LLE降维 n_components = 4 lle_res = manifold.LocallyLinearEmbedding( n_neighbors, n_components).fit_transform(ss_x) # print('lle_res',lle_res) return lle_res
def hessian_lle_embedding(): print("Computing Hessian LLE embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='hessian') t0 = time() X_hlle = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_hlle, "Hessian Locally Linear Embedding of the digits (time %.2fs)" % (time() - t0))
def do_embedding(self, event=None): converted = self.parent.converted if converted is None: #self.conversion.convert_frames() self.parent.converted = np.load( self.parent.output_folder + '/converted.npy') #FIXME For debugging converted = self.parent.converted method_ind = self.method.currentIndex() print('Doing %s' % self.method.currentText()) if method_ind == 0: self.embedder = manifold.SpectralEmbedding(n_components=4, n_jobs=-1) elif method_ind == 1: self.embedder = manifold.Isomap(n_components=4, n_jobs=-1) elif method_ind == 2: self.embedder = manifold.LocallyLinearEmbedding(n_components=4, n_jobs=-1, n_neighbors=20, method='modified') elif method_ind == 3: self.embedder = manifold.LocallyLinearEmbedding( n_components=4, n_jobs=-1, n_neighbors=20, method='hessian', eigen_solver='dense') elif method_ind == 4: self.embedder = manifold.MDS(n_components=4, n_jobs=-1) elif method_ind == 5: self.embedder = manifold.TSNE(n_components=3, init='pca') self.embedder.fit(converted) self.embed = self.embedder.embedding_ self.embed_plot = self.embed self.gen_hist() self.plot_embedding() if not self.embedded: self.add_classes_frame() self.embedded = True
def _get_transform(self, method: str, data: np.ndarray, n_components: int, kwargs: dict): if method == 'Isomap': return manifold.Isomap(n_components=n_components, **kwargs).fit_transform(data) elif method == 'Locally Linear Embedding': return manifold.LocallyLinearEmbedding(n_components=n_components, **kwargs).fit_transform(data) elif method == 'Spectral Embedding': return manifold.SpectralEmbedding(n_components=n_components, **kwargs).fit_transform(data) elif method == 'MDS': return manifold.MDS(n_components=n_components, **kwargs).fit_transform(data)
def __init__(self, data, control_points, parent): super(LLE, self).__init__(data, control_points, parent) self.name = "LLE" try: self.w = PopupSlider( 'Enter number of neighbors to consider (default is 4):') self.w.exec_() num = int(self.w.slider_value) if num == '': num = 4 try: lle = manifold.LocallyLinearEmbedding(n_neighbors=int(num), out_dim=2) except: lle = manifold.LocallyLinearEmbedding(n_neighbors=int(num), n_components=2) lle.fit(data) self.embedding = np.array(lle.transform(data)) except: msg = "It seems like the embedding algorithm did not converge with the given parameter setting" QMessageBox.about(parent, "Embedding error", msg)
def __init__(self, n_components, con: Configuration): if con.mode == 'lle_mlr_sga' or con.mode == 'lle_ap_mlr_sga': self.tsne = manifold.LocallyLinearEmbedding( n_components=n_components, eigen_solver='dense') elif con.mode == 'tsne_mlr_sga' or con.mode == 'tsne_ap_mlr_sga': self.tsne = manifold.TSNE(n_components=n_components, init='pca', method='barnes_hut', angle=0.2, n_iter=1000) elif con.mode == 'isomap_mlr_sga' or con.mode == 'isomap_ap_mlr_sga': self.tsne = manifold.Isomap(n_components=n_components)
def hlle(X, dim=2, n_neighbors=30, **kargs): '''HLLE embedding of the dataset''' print("Computing Hessian LLE embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=dim, method='hessian') try: X_hlle = clf.fit_transform(X) print(("Done. Reconstruction error: %g" % clf.reconstruction_error_)) return clf, X_hlle, "Hessian Locally Linear Embedding of the features" except Exception as e: traceback.print_exc()
def test_LocallyLinearEmbedding(*data): ''' test the LLE method :param data: train_data, train_value :return: None ''' X, y = data for n in [4, 3, 2, 1]: lle = manifold.LocallyLinearEmbedding(n_components=n) lle.fit(X) print('reconstruction_error(n_components=%d) : %s' % (n, lle.reconstruction_error_))
def run_methods(df, h, outpath='dflt', hdr='dflt', hue='name', mets='dflt', shape=None, labels='dflt', scaling=None): """ run a selection of alternate dimension reduction techniques""" method_results = dict() n_neighbors = 15 try: hdr = gt.check_dfltarg(hdr, df.name) except: hdr = df.columns[0].split(':')[0] outpath = gt.check_dfltarg(outpath, gt.dflt_outpath(fldr_name='dim_reduct')) labels = gt.check_dfltarg(labels, h.label) mets = gt.check_dfltarg(mets, ['PCA','ISO','MDS','LLE']) if ':' in df.columns.values[0]: df = df.T if scaling is not None: if scaling == 'maxabs': df = MaxAbsScaler().fit_transform(df) elif scaling == 'robust': df = RobustScaler().fit_transform(df) elif scaling == 'std': df = StandardScaler(with_mean=False).fit_transform(df) else: print('error with scaling') if 'PCA' in mets: # Projection on to the first 2 principal components method_results['PCA'] = decomposition.PCA(n_components=2).fit_transform(df) if 'ISO' in mets: # Isomap projection of the digits dataset method_results['ISO'] = manifold.Isomap(n_neighbors, n_components=2).fit_transform(df) if 'MDS' in mets: # MDS embedding of the digits dataset method_results['MDS'] = manifold.MDS(n_components=2, n_init=1, max_iter=100).fit_transform(df) if 'LLE' in mets: # Locally linear embedding method_results['LLE'] = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='modified').fit_transform(df) for met, dat in method_results.items(): xcol, ycol = f'{met}_x', f'{met}_y' h[xcol] = dat[:, 0] h[ycol] = dat[:, 1] title = hdr + ' ' + met seaborn_scatter(h, title, outpath, hue=hue, x=xcol, y=ycol, labels=labels, shape=shape, legend='brief')
def test_lle_with_sklearn(): N = 10 X, color = datasets.samples_generator.make_s_curve(N, random_state=0) n_components = 2 n_neighbors = 3 knn = NearestNeighbors(n_neighbors + 1).fit(X) G = geom.Geometry() G.set_data_matrix(X) G.set_adjacency_matrix(knn.kneighbors_graph(X, mode='distance')) sk_Y_lle = manifold.LocallyLinearEmbedding( n_neighbors, n_components, method='standard').fit_transform(X) (mm_Y_lle, err) = lle.locally_linear_embedding(G, n_components) assert (_check_with_col_sign_flipping(sk_Y_lle, mm_Y_lle, 0.05))
def ltsa(X, dim=2, n_neighbors=30, **kargs): '''LTSA embedding of the dataset''' print("Computing LTSA embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=dim, method='ltsa') try: X_ltsa = clf.fit_transform(X) print(("Done. Reconstruction error: %g" % clf.reconstruction_error_)) return clf, X_ltsa, "Local Tangent Space Alignment of the features" except Exception as e: traceback.print_exc()
def plot_locally_linear_embedding(X, y, n_neighbors): """ Locally linear embedding of the digits dataset """ print("Computing LLE embedding") clf = manifold.LocallyLinearEmbedding(n_neighbors, n_components=2, method='standard') t0 = time() X_lle = clf.fit_transform(X) print("Done. Reconstruction error: %g" % clf.reconstruction_error_) plot_embedding(X_lle, y, "Locally Linear Embedding of the digits (time %.2fs)" % (time() - t0))
def LLE(array, percent_samples): print "LLE with", percent_samples * 100, "% of training data." print "Features\tTime" array = array[:int(percent_samples * len(array))] for pct in pct_features_list: num_features = int(pct * len(array[0])) start = time() Y = manifold.LocallyLinearEmbedding( n_components=num_features, eigen_solver='auto', method='standard').fit_transform(array) end = time() print num_features, "\t", (end - start)