def embedf(dist_mat): embedding, stress = manifold.smacof(dist_mat, metric=False, n_components=3, max_iter=500) return embedding
def __init__(self, df_avg, df_seg0, icells, edges, init=None, seed=137, xfac=1.0): map0, rmap0 = mk_maps(df_avg, icells) edges_0 = rmap0[edges] self.e0 = edges_0[:, 0] self.e1 = edges_0[:, 1] X = df_seg0.loc[icells, :].values dist = euclidean_distances(X) if init is None: np.random.seed(seed) #n_init should not be 1 for random seed case ux2, stress, n_iter = smacof(dist, init=init, n_init=1, return_n_iter=True) ux2[:, 0] = xfac * ux2[:, 0] segs = ux2[edges_0] segs0 = np.mean(segs, axis=1, keepdims=True) dsegs = segs - segs0 segs = .95 * dsegs + segs0 self.df_avg = df_avg self.ux2 = ux2 self.segs = segs self.edges_0 = edges_0 self.map0 = map0 #really same as icells self.rmap0 = rmap0
np.fill_diagonal(X, 0) for x in l_d: iCity0 = d_city[x[0]] iCity1 = d_city[x[1]] dist = x[2] print (iCity0, iCity1, dist) X[iCity0, iCity1] = dist X[iCity1, iCity0] = dist res = smacof(X, n_components=2, random_state=1, metric = True, verbose = 1, n_init = 10, eps=1e-12, max_iter=3000) cities = towns coords = res # dist(df, 'skien', 'oslo', d_city) df = pd.DataFrame({ 'x': coords[0][:, 0], 'y': coords[0][:, 1], 'group': cities }) def dist (df, t0, t1, d_city):
def smacof( D, n_components=2, metric=True, init=None, random_state=None, verbose=0, max_iter=3000, eps=1e-6, n_jobs=1, ): """Metric and non-metric MDS using SMACOF Parameters ---------- D : array-like, shape=[n_samples, n_samples] pairwise distances n_components : int, optional (default: 2) number of dimensions in which to embed `D` metric : bool, optional (default: True) Use metric MDS. If False, uses non-metric MDS init : array-like or None, optional (default: None) Initialization state random_state : int, RandomState or None, optional (default: None) numpy random state verbose : int or bool, optional (default: 0) verbosity max_iter : int, optional (default: 3000) maximum iterations eps : float, optional (default: 1e-6) stopping criterion Returns ------- Y : array-like, shape=[n_samples, n_components] embedded data """ _logger.debug( "Performing non-metric MDS on " "{} of shape {}...".format(type(D), D.shape) ) # Metric MDS from sklearn Y, _ = manifold.smacof( D, n_components=n_components, metric=metric, max_iter=max_iter, eps=eps, random_state=random_state, n_jobs=n_jobs, n_init=1, init=init, verbose=verbose, ) return Y
def SMACOF(D, num_iter, eps): X, stress = manifold.smacof(D, metric=False,n_components=2,verbose=2,\ max_iter=num_iter, eps=eps, n_jobs=8) return X
def embed_MDS(X, ndim=2, how='metric', distance_metric='euclidean', n_jobs=1, seed=None, verbose=0): """Performs classic, metric, and non-metric MDS Metric MDS is initialized using classic MDS, non-metric MDS is initialized using metric MDS. Parameters ---------- X: ndarray [n_samples, n_samples] 2 dimensional input data array with n_samples embed_MDS does not check for matrix squareness, but this is necessary for PHATE n_dim : int, optional, default: 2 number of dimensions in which the data will be embedded how : string, optional, default: 'classic' choose from ['classic', 'metric', 'nonmetric'] which MDS algorithm is used for dimensionality reduction distance_metric : string, optional, default: 'euclidean' choose from ['cosine', 'euclidean'] distance metric for MDS n_jobs : integer, optional, default: 1 The number of jobs to use for the computation. If -1 all CPUs are used. If 1 is given, no parallel computing code is used at all, which is useful for debugging. For n_jobs below -1, (n_cpus + 1 + n_jobs) are used. Thus for n_jobs = -2, all CPUs but one are used seed: integer or numpy.RandomState, optional The generator used to initialize SMACOF (metric, nonmetric) MDS If an integer is given, it fixes the seed Defaults to the global numpy random number generator Returns ------- Y : ndarray [n_samples, n_dim] low dimensional embedding of X using MDS """ if how not in ['classic', 'metric', 'nonmetric']: raise ValueError("Allowable 'how' values for MDS: 'classic', " "'metric', or 'nonmetric'. " "'{}' was passed.".format(how)) # MDS embeddings, each gives a different output. X_dist = squareform(pdist(X, distance_metric)) # initialize all by CMDS Y = cmdscale_fast(X_dist, ndim) if how in ['metric', 'nonmetric']: log_debug("Performing metric MDS on " "{} of shape {}...".format(type(X_dist), X_dist.shape)) # Metric MDS from sklearn Y, _ = smacof(X_dist, n_components=ndim, metric=True, max_iter=3000, eps=1e-6, random_state=seed, n_jobs=n_jobs, n_init=1, init=Y, verbose=verbose) if how == 'nonmetric': log_debug("Performing non-metric MDS on " "{} of shape {}...".format(type(X_dist), X_dist.shape)) # Nonmetric MDS from sklearn using metric MDS as an initialization Y, _ = smacof(X_dist, n_components=ndim, metric=True, max_iter=3000, eps=1e-6, random_state=seed, n_jobs=n_jobs, n_init=1, init=Y, verbose=verbose) return Y
def __init__(self,df_avg,df_seg0,icells,edges,init=None,seed=137,xfac=1.0,wfac=1.0): map0,rmap0 = mk_maps(df_avg,icells) edges_0 = rmap0[edges] print("wfac",wfac) self.e0 = edges_0[:,0] self.e1 = edges_0[:,1] X = df_seg0.loc[icells,:].values dist = euclidean_distances(X) edist = dist[self.e0,self.e1] print("edist",edist[:5]) weights = np.ones(dist.shape) if wfac != 1.0: print("weights") weights[self.e0,self.e1] = wfac weights[self.e1,self.e0] = wfac if init is None: np.random.seed(seed) #n_init should not be 1 for random seed case if wfac == 1.0: ux2,stress,n_iter = smacof(dist,init=init,n_init=1,return_n_iter=True) else: ux2,stress,xdist,n_iter = smacof2(dist,init=init,n_init=1,return_n_iter=True,weights=weights) #ux2,stress,xdist,n_iter = smacof2(dist,init=init,n_init=1,return_n_iter=True,weights=None) print("n_iter",n_iter) """ exdist = xdist[self.e0,self.e1] eratio = exdist/edist print(np.amax(eratio), np.amin(eratio)) plt.hist(eratio,bins=50,log=True) plt.show() """ #ux2,stress,n_iter = smacof2(dist,return_n_iter=True,weights=weights) #here init is None ux2[:,0] = xfac*ux2[:,0] segs = ux2[edges_0] segs0 = np.mean(segs,axis=1,keepdims=True) dsegs = segs - segs0 segs = .95*dsegs + segs0 self.df_avg = df_avg self.ux2 = ux2 self.segs = segs self.edges_0 = edges_0 self.map0 = map0 #really same as icells self.rmap0 = rmap0
def fit_transform(self, data): dist_matrix = squareform(pdist(data, metric=self.metric)) return smacof(dist_matrix)
scores_img = rsa.compare_to(model, distance="spearmanr", n_perms=200) # Compute just the distance with the model pval, true_score, random_score = mantel_test(avg_rdm, model, "spearmanr", 1000) # MDS seed = np.random.RandomState(seed=3) mds = manifold.MDS(n_components=2, max_iter=3000, eps=1e-9, random_state=seed, dissimilarity="precomputed", n_jobs=1) pos_mds = mds.fit(check_rdm(avg_rdm)).embedding_ pos_smacof, _ = manifold.smacof(check_rdm(avg_rdm), n_components=2, random_state=seed) # ****************************************************************************** # ***** Figure ***************************************************************** # ****************************************************************************** fig, axes = plt.subplots(2, 3, figsize=(12, 10)) # The average RDM plot_rdm(avg_rdm, "Average RDM", triangle="lower", ax=axes[0, 0], cblabel="Euclidean distance") # Plot plot_position(pos_mds,