def test_MDS(): sim = np.array([[0, 5, 3, 4], [5, 0, 2, 2], [3, 2, 0, 1], [4, 2, 1, 0]]) mds_clf = mds.MDS(metric=False, n_jobs=3, dissimilarity="precomputed") mds_clf.fit(sim)
def isomap_own(X, no_of_dim, neighbour): n = len(X) adj = 1e10 * np.ones([n, n]) for i in range(len(adj)): adj[i][i] = 0 for i in range(len(X)): ndrs = NearestNeighbors(n_neighbors=neighbour, algorithm='ball_tree', metric='euclidean').fit(X) dis, ind = ndrs.kneighbors([[X[i][0], X[i][1], X[i][2]]]) print(dis, ind) print('**', len(dis)) for j in range(len(ind[0])): adj[i][ind[0][j]] = dis[0][j] adj[ind[0][j]][i] = dis[0][j] print('****') print(len(adj[0])) print(adj[0]) dis = copy.deepcopy(adj) n = len(adj) for k in range(n): for i in range(n): for j in range(n): if dis[i][j] > dis[i][k] + dis[k][j]: dis[i][j] = dis[i][k] + dis[k][j] dr = mds.MDS(n_components=2, dissimilarity="precomputed") print(dr.fit_transform(dis)) y = dr.fit_transform(dis) #return y return mds_own(dis, no_of_dim)
def sklearn_mds(self): mds_clf = mds.MDS(metric=True, n_jobs=3, dissimilarity="precomputed") self.distanceMatrix = np.array(self.distanceMatrix) #print self.distanceMatrix res = mds_clf.fit_transform(self.distanceMatrix) #print mds_clf #print res self.assign(res)
def test_MDS(): sim = np.array([[0, 5, 3, 4], [5, 0, 2, 2], [3, 2, 0, 1], [4, 2, 1, 0]]) mds_clf = mds.MDS(metric=False, n_jobs=3) mds_clf.fit(sim)
dis = np.zeros([n_points, n_points]) X, color = datasets.samples_generator.make_s_curve(n_points, random_state=0) print(X) fig = plt.figure(figsize=(15, 8)) ax = fig.add_subplot(111, projection='3d') ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=plt.cm.Spectral) ax.view_init(12, -72) plt.show() for i in range(n_points): for j in range(n_points): dis[i][j] = euclidean(X[i], X[j]) nx = mds_own(dis) dr = mds.MDS(n_components=2) nx1 = dr.fit_transform(X) #nx=dr.fit_transform(X) #print(nx)''' '''dis=np.eye(len(X)) for i in range(len(X)): for j in range(len(X)): dis[i][j]=euclidean(X[i],X[j]) nx=mds_own(dis,2)''' fig = plt.figure(figsize=(15, 8)) ax = fig.add_subplot(111) ax.scatter(nx[:, 0], nx[:, 1], c=color, cmap=plt.cm.Spectral) plt.show()