本文整理汇总了Python中sklearn.manifold.MDS.fit方法的典型用法代码示例。如果您正苦于以下问题:Python MDS.fit方法的具体用法?Python MDS.fit怎么用?Python MDS.fit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.manifold.MDS
的用法示例。
在下文中一共展示了MDS.fit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: plot_cities
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def plot_cities():
#distance_matrix = get_distances()
cities = 'BOS CHI DC DEN LA MIA NY SEA SF'.split()
distance_matrix = np.array([
[0 , 963 , 429 , 1949, 2979, 1504, 206 , 2976, 3095],
[963 , 0 , 671 , 996 , 2054, 1329, 802 , 2013, 2142],
[429 , 671 , 0 , 1616, 2631, 1075, 233 , 2684, 2799],
[1949, 996 , 1616, 0 , 1059, 2037, 1771, 1307, 1235],
[2979, 2054, 2631, 1059, 0 , 2687, 2786, 1131, 379],
[1504, 1329, 1075, 2037, 2687, 0 , 1308, 3273, 3053],
[206 , 802 , 233 , 1771, 2786, 1308, 0 , 2815, 2934],
[2976, 2013, 2684, 1307, 1131, 3273, 2815, 0 , 808],
[3095, 2142, 2799, 1235, 379 , 3053, 2934, 808 , 0]
])
# assert symmetric
for (i, j) in [(i, j) for i in range(0, 8) for j in range(0, 8)]:
try:
assert(distance_matrix[i][j] == distance_matrix[j][i])
except AssertionError:
print((i, j))
print(distance_matrix)
mds = MDS(dissimilarity='precomputed')
mds.fit(distance_matrix)
print(mds.embedding_)
for idx, points in enumerate(mds.embedding_):
plt.plot(points[0], points[1], 'r.')
plt.text(points[0], points[1], cities[idx])
plt.show()
return
示例2: non_param_multi_dim_scaling
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def non_param_multi_dim_scaling(dists, n_dims=3, n_threads=None, metric=True):
mds = MDS(n_components=n_dims, metric=metric, n_jobs=n_threads,
dissimilarity='precomputed')
mds.fit(squareform(dists))
projs = mds.embedding_
res = {'stress': mds.stress_,
'projections': projs}
return res
示例3: md_scaling
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def md_scaling(co_matrix, is_distance_matrix=False):
if not is_distance_matrix:
distance_matrix = -np.log(co_matrix.matrix)
else:
distance_matrix = co_matrix
mds = MDS(dissimilarity='precomputed')
mds.fit(distance_matrix)
return mds.embedding_
示例4: plotMap
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def plotMap(maparr, freq, nest, seqs, dbfile, map2d, outfile, plotm='T'):
#mutli-dimensional scaling
similarities = euclidean_distances(np.matrix(maparr))
mds = MDS(n_components=2, max_iter=3000, eps=1e-9, random_state=np.random.RandomState(seed=3), dissimilarity="precomputed", n_jobs=1)
pos = mds.fit(similarities).embedding_
#plot attributes
N = len(pos)
#size = [20*n for n in freq]
size = 8000
color = np.array(range(N))
if str(plotm) == 'T':
#plot MDS
fig, ax = plt.subplots(figsize=(10,10))
warnings.filterwarnings("ignore")
scatter = ax.scatter(np.array(pos[:,0]), np.array(pos[:,1]), c=color, s=size, alpha=0.3, cmap=plt.cm.viridis, marker='s')
plt.xlabel('Dimension 1', fontsize=20, labelpad=20)
plt.ylabel('Dimension 2', fontsize=20, labelpad=20)
#plt.axis([xmin, xmax, ymin, ymax])
plt.tick_params(labelsize=15, length=14, direction='out', pad=15, top='off', right='off')
#save figures
fig.savefig(outfile + '.png', bbox_inches='tight', format='png')
fig.savefig(outfile + '.pdf', bbox_inches='tight', format='pdf')
plt.close(fig)
warnings.resetwarnings()
#write csv file
writePlotMDS(freq, nest, seqs, dbfile, pos, maparr, map2d, outfile)
return pos
示例5: project_in_2D
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def project_in_2D(distance_mat, method='mds'):
"""
Project SDRs onto a 2D space using manifold learning algorithms
:param distance_mat: A square matrix with pairwise distances
:param method: Select method from 'mds' and 'tSNE'
:return: an array with dimension (numSDRs, 2). It contains the 2D projections
of each SDR
"""
seed = np.random.RandomState(seed=3)
if method == 'mds':
mds = MDS(n_components=2, max_iter=3000, eps=1e-9,
random_state=seed,
dissimilarity="precomputed", n_jobs=1)
pos = mds.fit(distance_mat).embedding_
nmds = MDS(n_components=2, metric=False, max_iter=3000, eps=1e-12,
dissimilarity="precomputed", random_state=seed,
n_jobs=1, n_init=1)
pos = nmds.fit_transform(distance_mat, init=pos)
elif method == 'tSNE':
tsne = TSNE(n_components=2, init='pca', random_state=0)
pos = tsne.fit_transform(distance_mat)
else:
raise NotImplementedError
return pos
示例6: main
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def main():
# load sample data
data = np.loadtxt("distmat799.txt", delimiter=",")
dists = data / np.amax(data)
# load images
img_files = [img for img in os.listdir("799_patch") if re.search(r"\.png", img)]
# mds
mds = MDS(n_components=2, dissimilarity="precomputed")
results = mds.fit(dists)
# plot
fig, ax = plt.subplots()
for i, img_file in enumerate(img_files):
img_file = os.path.join("799_patch", img_file)
img = read_png(img_file)
imagebox = OffsetImage(img, zoom=2.0)
coords = results.embedding_[i, :]
xy = tuple(coords)
ab = AnnotationBbox(imagebox, xy)
ax.add_artist(ab)
ax.set_xlim(-1.0, 1.0)
ax.set_ylim(-1.0, 1.0)
plt.show()
示例7: labtest_MDS
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def labtest_MDS(PID):
data = [patients[pid]['tests'] for pid in PID]
X = pp.scale(data)
mds = MDS(n_components = 2, metric = True, n_init = 4, max_iter = 300, verbose = 0, eps = 0.001, n_jobs = 1, dissimilarity = 'euclidean')
pos = mds.fit(X).embedding_
return pos
示例8: cluster
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def cluster(D, k=3, verbose=False):
"""Cluster LDS's via Multi-Dimensional Scaling and KMeans.
Strategy:
1. Build NxN matrix of pairwise similarities
2. Run MDS to embed data in R^2
3. Run KMeans with k cluster centers
4. Find samples closest to the k centers
Paramters:
----------
D: numpy.ndarray, shape = (N, N)
Precomputed distance matrix.
k: int (default: 3)
Number of desired cluster centers.
verbose: boolean
Enable verbose output.
Returns:
--------
eData: numpy.ndarray, shape (N, k)
N d-dimensional samples embedded in R^d.
ids: numpy.ndarray, shape = (k,)
List of indices identifying the k representatives.
"""
assert D.shape[0] == D.shape[1], "OOps (distance matrix not square)!"
# build MDS for precomputed similarity matrix
mds = MDS(metric=True, n_components=2, verbose=True,
dissimilarity="precomputed")
def __symmetrize(A):
return A + A.T - np.diag(A.diagonal())
# run MDS on symmetrized similarity matrix
eData = mds.fit(__symmetrize(D)).embedding_
kmObj = KMeans(k)
kmObj.fit_predict(eData)
ids = np.zeros((k,), dtype=np.int)
for i in range(k):
# sanity check
cDat = eData[np.where(kmObj.labels_ == i)[0],:]
assert len(cDat) > 0, "Oops, empty cluster ..."
kCen = kmObj.cluster_centers_[i,:]
x = euclidean_distances(eData, kCen)
ids[i] = int(np.argsort(x.ravel())[0])
# return distance matrix and ID's of representative LDS's
return (eData, ids)
示例9: timeline_scatter_plot
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def timeline_scatter_plot(X, time_index, method='MDS', metric='cosine', **kwargs):
if not isinstance(time_index, pd.DatetimeIndex):
time_index = pd.DatetimeIndex(time_index)
dm = pairwise_distances(X, metric=metric)
if method.upper() == 'MDS':
decomposer = MDS(n_components=2, dissimilarity='precomputed', verbose=1, **kwargs)
decomposer.fit(dm)
elif method.upper() == 'TSNE':
decomposer = TSNE(n_components=2, metric='precomputed', verbose=1, **kwargs)
decomposer.fit(dm)
else:
raise ValueError("Method %s is not supported..." % method)
X, Y = decomposer.embedding_[:,0], decomposer.embedding_[:,1]
unique_index = time_index.unique().order()
colormap = {time_stamp: color for time_stamp, color in zip(
unique_index, sns.cubehelix_palette(unique_index.shape[0]))}
colors = [colormap[time_stamp] for time_stamp in time_index]
sns.plt.scatter(X, Y, s=40, color=colors, alpha=0.7)
sns.plt.axis('off')
示例10: get_mds
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def get_mds(similarities):
seed = np.random.RandomState(seed=3)
print(np.amax(similarities))
print(np.amin(similarities))
nmds = MDS(n_components=2, metric=False, max_iter=3000, eps=1e-12,
dissimilarity="precomputed", random_state=seed, n_jobs=1,
n_init=1)
pos = nmds.fit(similarities).embedding_
X=np.array(pos)
return X
示例11: embedDistanceMatrix
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def embedDistanceMatrix(dmatDf, method='kpca', n_components=2, **kwargs):
"""Two-dimensional embedding of sequence distances in dmatDf,
returning Nx2 x,y-coords: tsne, isomap, pca, mds, kpca, sklearn-tsne"""
if isinstance(dmatDf, pd.DataFrame):
dmat = dmatDf.values
else:
dmat = dmatDf
if method == 'tsne':
xy = tsne.run_tsne(dmat, no_dims=n_components, perplexity=kwargs['perplexity'])
elif method == 'isomap':
isoObj = Isomap(n_neighbors=10, n_components=n_components)
xy = isoObj.fit_transform(dmat)
elif method == 'mds':
mds = MDS(n_components=n_components,
max_iter=3000,
eps=1e-9,
random_state=15,
dissimilarity="precomputed",
n_jobs=1)
xy = mds.fit(dmat).embedding_
rot = PCA(n_components=n_components)
xy = rot.fit_transform(xy)
elif method == 'pca':
pcaObj = PCA(n_components=None)
xy = pcaObj.fit_transform(dmat)[:, :n_components]
elif method == 'kpca':
pcaObj = KernelPCA(n_components=dmat.shape[0], kernel='precomputed', eigen_solver='dense')
try:
gram = dist2kernel(dmat)
except:
print('Could not convert dmat to kernel for KernelPCA; using 1 - dmat/dmat.max() instead')
gram = 1 - dmat / dmat.max()
xy = pcaObj.fit_transform(gram)[:, :n_components]
elif method == 'lle':
lle = manifold.LocallyLinearEmbedding(n_neighbors=30, n_components=n_components, method='standard')
xy = lle.fit_transform(dist)
elif method == 'sklearn-tsne':
tsneObj = TSNE(n_components=n_components, metric='precomputed', random_state=0, perplexity=kwargs['perplexity'])
xy = tsneObj.fit_transform(dmat)
elif method == 'umap':
umapObj = umap.UMAP(n_components=n_components, metric='precomputed', **kwargs)
xy = umapObj.fit_transform(dmat)
else:
print('Method unknown: %s' % method)
return
assert xy.shape[0] == dmatDf.shape[0]
xyDf = pd.DataFrame(xy[:, :n_components], index=dmatDf.index, columns=np.arange(n_components))
if method == 'kpca':
"""Not sure how negative eigenvalues should be handled here, but they are usually
small so it shouldn't make a big difference"""
setattr(xyDf, 'explained_variance_', pcaObj.lambdas_[:n_components]/pcaObj.lambdas_[pcaObj.lambdas_>0].sum())
return xyDf
示例12: make_mds_image
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def make_mds_image(m, filename, labels=None, colour=None):
"""Given a matrix of distances, project into 2D space using
multi-dimensional scaling and produce an image."""
mds_data_filename = filename + ".dat"
try:
# if we've previously computed, load it
p = np.genfromtxt(mds_data_filename)
except:
# else, compute it now (and save)
# Construct MDS object with various defaults including 2d
mds = MDS(dissimilarity="precomputed")
# Fit
try:
f = mds.fit(m)
except ValueError as e:
print("Can't run MDS for " + filename + ": " + str(e))
return
# Get the embedding in 2d space
p = f.embedding_
# save
np.savetxt(mds_data_filename, p)
# Make an image
fig, ax = plt.subplots(figsize=(5, 5))
# x- and y-coordinates
ax.set_aspect('equal')
ax.scatter(p[:,0], p[:,1], edgecolors='none')
if labels != None:
print filename
# hard-coded for GP depth-2
indices = [0, 2, 50, 52]
for i in indices:
print labels[i], p[i,0], p[i,1]
# can print some labels directly on the graph as follows,
# but maybe it's better done manually, after printing
# their locations to terminal?
# plt.text(p[i,0], p[i,1], labels[i], style='italic',
# bbox={'facecolor':'red', 'alpha':0.5, 'pad':10})
fig.savefig(filename + ".pdf")
fig.savefig(filename + ".eps")
fig.savefig(filename + ".png")
plt.close(fig)
示例13: plot_mds
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def plot_mds(points, genres, n_points=500):
'''
Plots a set of documents in MDS space
Args:
points: dense array with coordinates of each document
genres: list of genres for each entry in points
Returns:
None
'''
genres = np.array(genres)
genre_sel = np.not_equal(genres, None)
X, y = points[genre_sel], genres[genre_sel]
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, train_size=n_points)
distances = cosine_distances(X_train, X_train)
mds = MDS(n_components=2, dissimilarity='precomputed')
mds.fit(distances)
plot_embedding(mds.embedding_, y_train)
示例14: reduction
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def reduction(simMat,N=2):
#change similarity matrix into dissimilarity matrix
dis = map(lambda x: map(lambda y: 1-y, x), simMat)
#dis = dist(simMat)
#dis = simMat
#configure MDS to run 10 times. Also specify that data will be a dissimilarity matrix
mds = MDS(n_components=N, n_init=10,max_iter=3000, metric=True, dissimilarity="precomputed")
mat = np.array(dis)
#Run MDS
fit = mds.fit(mat)
print "Approximate Stress:", fit.stress_
print "Stress:", stress(dis, fit.embedding_)
return fit.embedding_
示例15: mult_scl
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit [as 别名]
def mult_scl(X, labels):
print('labels:')
for i, label in zip(range(1, len(labels) + 1), labels):
print('{}: {}'.format(i, label))
isomap = Isomap()
points = isomap.fit(np.nan_to_num(X)).embedding_
f, (ax1, ax2, ax3) = plt.subplots(1, 3)
plot_location(labels, ax3)
ax1.scatter(points[:, 0], points[:, 1], s=20, c='r')
ax1.set_title('Isomap')
add_labels(labels, points, ax1)
mds = MDS()
points = mds.fit(np.nan_to_num(X)).embedding_
ax2.scatter(points[:, 0], points[:, 1], s=20, c='g')
ax2.set_title('MDS')
add_labels(labels, points, ax2)
plt.show()