コード例 #1
0
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__()
コード例 #3
0
ファイル: 5-LLE.py プロジェクト: alisure-ml/ml-sklearn
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_)))
コード例 #4
0
	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
コード例 #5
0
ファイル: runtime.py プロジェクト: babakahmadi/Thesis
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
コード例 #6
0
 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
コード例 #7
0
ファイル: Manifold.py プロジェクト: rajivsam/ManifoldLearning
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
コード例 #8
0
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)
コード例 #9
0
 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
コード例 #10
0
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
コード例 #11
0
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_))
コード例 #12
0
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))
コード例 #13
0
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_))
コード例 #14
0
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))
コード例 #15
0
ファイル: dr.py プロジェクト: ajgappmark/master-thesis
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)
コード例 #16
0
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_))
コード例 #17
0
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
コード例 #18
0
ファイル: main.py プロジェクト: hxc123fau/eu_data_analysis
    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
コード例 #19
0
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))
コード例 #20
0
ファイル: embedding.py プロジェクト: szsdk/Dragonfly
    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
コード例 #21
0
ファイル: Transform.py プロジェクト: gitter-badger/MESmerize
    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)
コード例 #22
0
 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)
コード例 #23
0
 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)
コード例 #24
0
ファイル: dim_reduction.py プロジェクト: fraka6/mlboost
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()
コード例 #25
0
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_))
コード例 #26
0
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')
コード例 #27
0
ファイル: test_lle.py プロジェクト: keceli/megaman
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))
コード例 #28
0
ファイル: dim_reduction.py プロジェクト: fraka6/mlboost
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()
コード例 #29
0
ファイル: kmds.py プロジェクト: rubythonode/jamespy_py3
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))
コード例 #30
0
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)