示例#1
0
def myIsomap(X,y):
    t1 = clock()
    n_neighbors = 30
    clf = manifold.Isomap(n_neighbors, n_components=2)
    clf.fit(X)
    newRep = clf.transform(X)
    t2 = clock()
    return t2-t1
示例#2
0
def dimensionality_reduce(X, y):
    logging.info("Computing Isomap embedding")
    t0 = time()
    X_iso = manifold.Isomap(n_neighbors, n_components=2).fit_transform(X)
    #print("Done.")
    plot_embedding_2d(
        X_iso, y,
        "Isomap projection of the doc2vec (time %.2fs)" % (time() - t0))
示例#3
0
def run_isomap(*data):
    x, y = data
    for n in [4, 3, 2, 1]:
        print("-" * 50)
        isomap = manifold.Isomap(n_components=n)
        print("训练模型")
        isomap.fit(x)
        print("重构误差:", (n, str(isomap.reconstruction_error())))
示例#4
0
def test_pipeline():
    # check that Isomap works fine as a transformer in a Pipeline
    iris = datasets.load_iris()
    clf = pipeline.Pipeline([('isomap', manifold.Isomap()),
                             ('neighbors_clf',
                              neighbors.KNeighborsClassifier())])
    clf.fit(iris.data, iris.target)
    assert_lower(.7, clf.score(iris.data, iris.target))
示例#5
0
    def trainAndSaveIsomap(self, filename="isomap_model"):
        all_xs, all_ys = self.getXY("xs", "ys")

        im = manifold.Isomap(self.knn, self.isomap_dimension, n_jobs=-1)
        im = im.fit(all_xs)
        pickle.dump(im, open(filename, "wb"))

        return im
示例#6
0
def run_all_methods_for_some_dimensions(target_dim, exp_images, exp_labels,
                                        n_samples, initial_dim):

    methods_l = [
        ('MDS (proposed)',
         multidimensional.mds.MDS(target_dim,
                                  point_filter,
                                  radius_update,
                                  starting_radius=starting_radius,
                                  radius_barrier=radius_barrier,
                                  max_turns=max_turns,
                                  explore_dim_percent=explore_dim_percent,
                                  keep_history=False,
                                  history_color=None,
                                  history_path=None,
                                  dissimilarities='euclidean')),
        ('Truncated SVD', decomposition.TruncatedSVD(n_components=target_dim)),
        ('LLE',
         manifold.LocallyLinearEmbedding(method_n_comp,
                                         target_dim,
                                         eigen_solver='auto',
                                         method='standard',
                                         n_jobs=8)),
        ('Isomap', manifold.Isomap(method_n_comp, target_dim)),
        ('ModifiedLLE',
         manifold.LocallyLinearEmbedding(method_n_comp,
                                         target_dim,
                                         eigen_solver='auto',
                                         method='modified',
                                         n_jobs=8)),
        ('SpectralEmbedding',
         manifold.SpectralEmbedding(n_components=target_dim,
                                    n_neighbors=method_n_comp,
                                    n_jobs=8)),

        # ('tSNE',
        # manifold.TSNE(n_components=target_dim,
        #               init='pca', random_state=0)),
        ('LTSA',
         manifold.LocallyLinearEmbedding(method_n_comp,
                                         target_dim,
                                         eigen_solver='auto',
                                         method='ltsa',
                                         n_jobs=8)),
        ('MDS SMACOF',
         manifold.MDS(n_components=target_dim,
                      n_init=1,
                      max_iter=max_turns,
                      verbose=2,
                      dissimilarity='euclidean')),
    ]

    results = {}
    for m_name, m_func in methods_l:
        res = safe_run_experiment(target_dim, exp_images, exp_labels,
                                  n_samples, initial_dim, m_name, m_func)
        results[m_name] = res
    return results
示例#7
0
def isomap(X, **kargs):
    # import matplotlib.cm as cm

    n_neighbors = 10
    n_components = 2

    y = kargs.get('y', None)
    # ulabels = labels = y = kargs.get('y', None)
    # n_labels = X.shape[0]
    # if labels is not None:
    #     ulabels = set(labels)
    #     n_labels = len(ulabels)

    t0 = time()
    X_proj = Y = manifold.Isomap(n_neighbors, n_components).fit_transform(X)
    t1 = time()
    print("Isomap: %.2g sec" % (t1 - t0))

    outputdir = kargs.get('outputdir', os.path.join(os.getcwd(), 'plot'))
    if not os.path.exists(outputdir): os.makedirs(outputdir)  # base directory

    plot_mode = kargs.get('graph_mode', 'seaborn')
    fpath = os.path.join(TestDir, name_image_file(
        descriptor='isomap', **kargs))  # kargs: seq_ptype, d2v_method

    if plot_mode.startswith('s'):
        scatter2(X_proj, y)
        plt.savefig(fpath, dpi=300)
    else:
        # [params] colors
        n_points = X.shape[0]
        colors = itertools.cycle(cm.rainbow(np.linspace(0, 1, n_points)))

        # for i, c in enumerate(itertools.cycle(cm.rainbow(np.linspace(0, 1, n_points)))):
        # for i, c in enumerate(itertools.cycle(["r", "b", "g"])):
        lcmap = {
            0: 'g',
            1: 'b',
            2: 'r',
        }
        colors = (lcmap[l] for l in y)  # loop through y

        fig = plt.figure(figsize=(15, 8))

        ax = fig.add_subplot(257)
        plt.scatter(X_proj[:, 0],
                    X_proj[:, 1],
                    c=next(colors),
                    cmap=plt.cm.Spectral)  #
        plt.title("Isomap (%.2g sec)" % (t1 - t0))
        ax.xaxis.set_major_formatter(NullFormatter())
        ax.yaxis.set_major_formatter(NullFormatter())
        plt.axis('tight')
        plt.savefig(fpath, dpi=300)

    plt.close()

    return X_proj
示例#8
0
def dimensionality_analysis_plot(df_orig,
                                 labels=[],
                                 method="PCA",
                                 n_components=2,
                                 n_neighbors=10,
                                 figsize=(7, 7),
                                 font_scale=2,
                                 return_df=False):
    # Define valid methods
    valid_methods = [
        "PCA",
        "T-SNE",
        "Isomap",
        "MDS",
        "SE",
        "LLE",
        "Modified LLE",
    ]
    if method not in valid_methods:
        print(method, "not a valid method. Available methods:\n",
              valid_methods)

    # Load libraries
    from sklearn import manifold
    from functools import partial
    LLE = partial(manifold.LocallyLinearEmbedding,
                  n_neighbors,
                  n_components,
                  eigen_solver='auto')

    # Extract data from method
    if method == "PCA":
        Y = PCA_analysis(df_orig, n_components)
    elif method == "T-SNE":
        model = manifold.TSNE(n_components=n_components,
                              init='pca',
                              random_state=0)
        Y = model.fit_transform(df_orig)
    elif method == "LLE":
        model = LLE(method='standard')
        Y = model.fit_transform(df_orig)
    elif method == "Modified LLE":
        model = LLE(method='modified')
        Y = model.fit_transform(df_orig)
    elif method == "Isomap":
        model = manifold.Isomap(n_neighbors, n_components)
        Y = model.fit_transform(df_orig)
    elif method == "MDS":
        model = manifold.MDS(n_components, max_iter=100, n_init=1)
        Y = model.fit_transform(df_orig)
    elif method == "SE":
        model = manifold.SpectralEmbedding(n_components=n_components,
                                           n_neighbors=n_neighbors)
        Y = model.fit_transform(df_orig)

    #Plot
    plot_scatter_labels(Y, method, labels)
示例#9
0
 def isomap(self, n_components=2, solver='auto'):
     print('Calculating isomap....\n')
     t = time.time()
     iso = manifold.Isomap(n_neighbors=self.n_neighbors,
                           n_components=n_components,
                           eigen_solver=solver)
     iso_array = iso.fit_transform(self.X)
     #plot2D(iso_array,self.y,'isomap','Isomap: 1078 cells with 10 subtypes',time.time() - t)
     return iso_array
示例#10
0
def isomap_projection():

    print("Computing Isomap projection")
    t0 = time()
    X_iso = manifold.Isomap(n_neighbors, n_components=2).fit_transform(X)
    print("Done.")
    plot_embedding(X_iso,
                   "Isomap projection of the digits (time %.2fs)" %
                   (time() - t0))
示例#11
0
def test_pipeline():
    # check that Isomap works fine as a transformer in a Pipeline
    # only checks that no error is raised.
    # TODO check that it actually does something useful
    X, y = datasets.make_blobs(random_state=0)
    clf = pipeline.Pipeline([('isomap', manifold.Isomap()),
                             ('clf', neighbors.KNeighborsClassifier())])
    clf.fit(X, y)
    assert_less(.9, clf.score(X, y))
示例#12
0
def isomap(input,finaldim):
    from sklearn import manifold
    import numpy
    if isinstance(input,numpy.ndarray)==False:
        input= input.todense()
    im= manifold.Isomap(n_neighbors=max(3,int(input.shape[0]/160)), n_components=finaldim, n_jobs=-1)
    trans=im.fit_transform(input)
    # return trans,np.asarray(np.asmatrix(input)*np.asmatrix(trans))
    return [],trans
示例#13
0
def _make_datasets():
    high_data, color \
        = datasets.samples_generator.make_swiss_roll(n_samples=300,
                                                     random_state=1)

    isomap = manifold.Isomap(n_neighbors=12, n_components=2)
    low_data = isomap.fit_transform(high_data)

    return high_data, low_data
示例#14
0
def test_get_feature_names_out():
    """Check get_feature_names_out for Isomap."""
    X, y = make_blobs(random_state=0, n_features=4)
    n_components = 2

    iso = manifold.Isomap(n_components=n_components)
    iso.fit_transform(X)
    names = iso.get_feature_names_out()
    assert_array_equal([f"isomap{i}" for i in range(n_components)], names)
示例#15
0
def test_isomap_raise_error_when_neighbor_and_radius_both_set():
    # Isomap.fit_transform must raise a ValueError if
    # radius and n_neighbors are provided.

    X, _ = datasets.load_digits(return_X_y=True)
    isomap = manifold.Isomap(n_neighbors=3, radius=5.5)
    msg = "Both n_neighbors and radius are provided"
    with pytest.raises(ValueError, match=msg):
        isomap.fit_transform(X)
def getIsomapGdist(data, num_neigh):

    # need to do this because reticulate stupidly converts integers to floats, and then complains about it
    num_neigh = int(num_neigh)

    embedding = manifold.Isomap(n_neighbors=num_neigh)
    embedding.fit_transform(data)
    gdist = embedding.dist_matrix_

    return gdist
def isomap1(data, n, k):
    X = data.T
    isomap = manifold.Isomap(n_neighbors=k,
                             n_components=n,
                             eigen_solver='auto',
                             path_method='FW',
                             neighbors_algorithm='auto')
    isomap.fit(X)
    X_r = isomap.fit_transform(X)
    return X_r
示例#18
0
 def __init__(self, data, control_points, parent):
     super(ISO, self).__init__(data, control_points, parent)
     self.name = "ISO"
     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:
             iso = manifold.Isomap(n_neighbors=int(num), out_dim=2)
         except:
             iso = manifold.Isomap(n_neighbors=int(num), n_components=2)
         iso.fit(data)
         self.embedding = np.array(iso.transform(data))
     except:
         msg = "It seems like the embedding algorithm did not converge with the given parameter setting"
         QMessageBox.about(parent, "Embedding error", msg)
示例#19
0
def test_sparse_input():
    X = sparse_rand(100, 3, density=0.1, format='csr')

    # Should not error
    for eigen_solver in eigen_solvers:
        for path_method in path_methods:
            clf = manifold.Isomap(n_components=2,
                                  eigen_solver=eigen_solver,
                                  path_method=path_method)
            clf.fit(X)
示例#20
0
def computeEmbedding(weights, embed_type):
    if embed_type == 'mds':
        mds = manifold.MDS(2, max_iter=100, n_init=1)
        Y = mds.fit_transform(weights)
    elif embed_type == 'tsne':
        tsne = manifold.TSNE(n_components=2, init='pca', random_state=0)
        Y = tsne.fit_transform(weights)
    elif embed_type == 'isomap':
        Y = manifold.Isomap(kNeiborSize, 2).fit_transform(weights)
    elif embed_type == 'spectral':
        se = manifold.SpectralEmbedding(n_components=2,
                                        n_neighbors=kNeiborSize)
        Y = se.fit_transform(weights)
    elif embed_type == 'pca':
        Y = PCA(n_components=2).fit_transform(weights)
    else:
        Y = manifold.Isomap(kNeiborSize, 2).fit_transform(weights)
    pos = zip(Y[:, 0], Y[:, 1])
    return pos
示例#21
0
def test_Isomap(*data):
    """
    测试等度量映射降维
    """
    x,y = data
    for n in [4, 3, 2, 1]:
        isomap = manifold.Isomap(n_components = n)
        isomap.fit(x)
        print("reconstruction_error(n_components=%d): %s" %\
                (n, isomap.reconstruction_error()))
示例#22
0
    def compute_geodesic_coordinates(self, euclid_coords):
        """Use the isomap algorithm (Tenenbaum et al., 2000, Science) to convert euclidean coordinates to geodesic.

        :return:
        """
        iso = manifold.Isomap(n_neighbors=4, path_method='FW')
        iso.fit(euclid_coords)
        geo_coords = iso.transform(euclid_coords)
        geo_coords = pd.DataFrame(geo_coords, columns=['GeoX', 'GeoY'])
        return geo_coords
示例#23
0
def isomap(X, dim=2, n_neighbors=30, **kargs):
    '''Isomap projection of the dataset'''
    print("Computing Isomap embedding")
    try:
        isomap = manifold.Isomap(n_neighbors, n_components=dim)
        X_iso = isomap.fit_transform(X)
        print("Done.")
        return isomap, X_iso, "Isomap projection of the features"
    except Exception as e:
        traceback.print_exc()
示例#24
0
def test_isomap_fit_precomputed_radius_graph():
    # Isomap.fit_transform must yield similar result when using
    # a precomputed distance matrix.

    X, y = datasets.make_s_curve(200, random_state=0)
    radius = 10

    g = neighbors.radius_neighbors_graph(X, radius=radius, mode="distance")
    isomap = manifold.Isomap(n_neighbors=None,
                             radius=radius,
                             metric="precomputed")
    isomap.fit(g)
    precomputed_result = isomap.embedding_

    isomap = manifold.Isomap(n_neighbors=None,
                             radius=radius,
                             metric="minkowski")
    result = isomap.fit_transform(X)
    assert_allclose(precomputed_result, result)
示例#25
0
def plot_tsne(epoch, data1, mname):
    if not os.path.isdir('./results/' + mname):
        os.mkdir('./results/' + mname)
    data = []
    for i in range(1500):
        image = data1[i]
        # image = binary.findGoldMask(image)
        data.append(image.flatten())
    class_list = np.load('./data/test_namelist.npy')
    data_binary = np.array(data)
    class_list = np.array(class_list) + 1

    ##Binary

    # starting manifold embedding
    n_neighbors = 25
    n_components = 2
    SNR = 0.05
    colors_jet = ['k', 'r', 'g', 'b', 'y']

    method = 't-SNE'
    t0 = time()
    Y = manifold.TSNE(n_components=n_components).fit_transform(data_binary)
    print(Y.shape)
    np.savetxt('./results/'+ mname + "/epoch_%d_denoise_SNR_%f_images_%s_%dNN_%d_component_eigs" % (epoch, SNR, method, n_neighbors, n_components), Y)
    t1 = time()
    print(" t-SNE: %.2g sec" % (t1 - t0))
    fig = plt.figure(figsize=(15, 15))
    ax = fig.gca()
    for i in range(1, 6):  # because the list id is from 1,five conformations
        index = np.where(class_list == i)[0]
        plt.scatter(Y[index, 0], Y[index, 1], label="state %d" % i, color=colors_jet[i - 1], s=30)
    plt.title(" t-SNE (%.2g sec)" % (t1 - t0))
    plt.savefig(
        "./results/"+ mname + "/epoch_%d_denoise_SNR_%f_images_%s_%dNN_%d_component.png" % (epoch, SNR, method, n_neighbors, n_components))
    plt.close()

    method = 'ISOMAP'
    t0 = time()
    Y = manifold.Isomap(n_neighbors=n_neighbors, n_components=n_components).fit_transform(data_binary)
    print(np.shape(Y))
    np.savetxt('./results/'+ mname + '/epoch_%d_denoise_SNR_%f_images_%s_%dNN_%d_component_eigs' % (epoch, SNR, method, n_neighbors, n_components), Y)
    t1 = time()
    print("Isomap: %.2g sec" % (t1 - t0))
    fig = plt.figure(figsize=(15, 15))
    ax = fig.gca()
    # plt.scatter(Y[:, 0], Y[:, 1], cmap=plt.cm.Spectral)
    for i in range(1, 6):  # because the list id is from 1,five conformations
        index = np.where(class_list == i)[0]
        plt.scatter(Y[index, 0], Y[index, 1], label="state %d" % i, color=colors_jet[i - 1], s=30)
    plt.title("Isomap (%.2g sec)" % (t1 - t0))
    plt.savefig(
        "./results/"+ mname + "/epoch_%d_denoise_SNR_%f_images_%s_%dNN_%d_component.png" % (
        epoch, SNR, method, n_neighbors, n_components))
    plt.close()
示例#26
0
def cross_validation_with_and_without_manifold(X, y, n_neighbors, n_components, k):
    # Split indexes according to Kfold with k = 10
    kf = KFold(n_splits=k)

    # initialize scores lists
    scores = []
    scores2 = []
    for train_index, test_index in kf.split(X):
        kernel = GraphKernel(kernel={"name": "shortest_path", "with_labels": False}, normalize=True)

        # split train and test of K-fold
        X_train, X_test = X[train_index], X[test_index]
        y_train, y_test = y[train_index], y[test_index]

        # Calculate the kernel matrix.
        K_train = kernel.fit_transform(X_train)
        K_test = kernel.transform(X_test)

        # Initialise an SVM and fit.
        clf = svm.SVC(kernel='precomputed', C=4)
        clf.fit(K_train, y_train)

        # Predict and test.
        y_pred = clf.predict(K_test)

        # Calculate accuracy of classification.
        acc = accuracy_score(y_test, y_pred)
        scores.append(acc)

        # Compute distance matrix
        D_train = compute_distance_matrix(K_train)
        D_test = compute_distance_matrix(K_test)

        # Initialize Isomap embedding object, embed train and test data
        embedding = manifold.Isomap(n_neighbors, n_components, metric="precomputed")
        E_train = embedding.fit_transform(D_train)
        E_test = embedding.transform(D_test)

        # initialize second svm (not necessary? search documentation)
        clf2 = svm.SVC(kernel='linear', C=4)
        clf2.fit(E_train, y_train)

        # Predict and test.
        y_pred = clf2.predict(E_test)

        # Calculate accuracy of classification.
        acc = accuracy_score(y_test, y_pred)
        scores2.append(acc)
    for i, _ in enumerate(scores):
        scores[i] = scores[i] * 100

    for i, _ in enumerate(scores2):
        scores2[i] = scores2[i] * 100
    return scores, scores2
示例#27
0
def isomap(data, labels, new_dimension):
    print "isomap..."

    if hasattr(data, "toarray"):
        data = data.toarray()

    start = time.time()
    iso = manifold.Isomap(n_components=new_dimension)
    reduced = iso.fit_transform(data)
    end = time.time()
    return (reduced, end-start)
示例#28
0
def plot_isomap(X, y, n_neighbors):
    """
    Isomap projection of the digits dataset
    """
    print("Computing Isomap embedding")
    t0 = time()
    X_iso = manifold.Isomap(n_neighbors, n_components=2).fit_transform(X)
    print("Done.")
    plot_embedding(X_iso, y,
                   "Isomap projection of the digits (time %.2fs)" %
                   (time() - t0))
示例#29
0
    def plot(self, dim_reduction_strategy='PCA', kernel='linear'):
        """
        Plot the result of the fiting
        """
        whole_space = np.append(self._training_set._ip_feature_array, self._testing_set._ip_feature_array, axis=0)

        whole_target = np.append(self._training_set._target, self._testing_set._target, axis=0)
        if dim_reduction_strategy == 'PCA':
            # change randomizedPCA to PCA
            # dim_reducer = decomposition.RandomizedPCA(n_components=2)
            dim_reducer = decomposition.PCA(n_components=2)
        elif dim_reduction_strategy == 'Isomap':
            n_neighbors = 30
            dim_reducer = manifold.Isomap(n_neighbors, n_components=2)
        elif dim_reduction_strategy == 'MDS':
            dim_reducer = manifold.MDS(n_components=2, n_init=1, max_iter=100)

        dim_reducer.fit(whole_space)
        reduced_train_spc = dim_reducer.transform(self._training_set._ip_feature_array)
        reduced_test_spc = dim_reducer.transform(self._testing_set._ip_feature_array)

        reduced_whole_space = np.append(reduced_train_spc, reduced_test_spc, axis=0)

        clf = svm.SVC(kernel=str(kernel), gamma=10)
        clf.fit(reduced_train_spc, self._training_set._target)

        pl.figure(0)
        pl.clf()
        pl.scatter(reduced_whole_space[:, 0], reduced_whole_space[:, 1], c=whole_target, zorder=10, cmap=pl.cm.Paired)

        # Circle out the test data
        pl.scatter(reduced_test_spc[:, 0], reduced_test_spc[:, 1],
                   s=80, facecolors='none', zorder=10)

        pl.axis('tight')
        x_min = reduced_whole_space[:, 0].min()
        x_max = reduced_whole_space[:, 0].max()
        y_min = reduced_whole_space[:, 1].min()
        y_max = reduced_whole_space[:, 1].max()

        XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j]
        Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()])

        # Put the result into a color plot
        Z = Z.reshape(XX.shape)
        pl.pcolormesh(XX, YY, Z > 0, cmap=pl.cm.Paired)
        pl.contour(XX, YY, Z, colors=['k', 'k', 'k'],
              linestyles=['--', '-', '--'],
              levels=[-.5, 0, .5])

        norm_mode = (self._training_set._normalisation_data and "individual" or "sparse")
        pl.title("Norm: " + norm_mode + ", Kernel:" + kernel + ", Dimension reduced by " + dim_reduction_strategy)

        pl.show()
示例#30
0
def test_pipeline(n_neighbors, radius):
    # check that Isomap works fine as a transformer in a Pipeline
    # only checks that no error is raised.
    # TODO check that it actually does something useful
    X, y = datasets.make_blobs(random_state=0)
    clf = pipeline.Pipeline([
        ("isomap", manifold.Isomap(n_neighbors=n_neighbors, radius=radius)),
        ("clf", neighbors.KNeighborsClassifier()),
    ])
    clf.fit(X, y)
    assert 0.9 < clf.score(X, y)