本文整理汇总了Python中sklearn.manifold.MDS.fit_transform方法的典型用法代码示例。如果您正苦于以下问题:Python MDS.fit_transform方法的具体用法?Python MDS.fit_transform怎么用?Python MDS.fit_transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.manifold.MDS
的用法示例。
在下文中一共展示了MDS.fit_transform方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mds
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def mds(similarity, euclid=False):
if euclid:
model = MDS(max_iter=1000)
result = model.fit_transform(similarity)
else:
model = MDS(max_iter=1000, dissimilarity='precomputed')
result = model.fit_transform(1 - similarity)
return result.T
示例2: project_in_2D
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [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
示例3: embed_two_dimensions
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def embed_two_dimensions(data, vectorizer, size=10, n_components=5, colormap='YlOrRd'):
if hasattr(data, '__iter__'):
iterable = data
else:
raise Exception('ERROR: Input must be iterable')
import itertools
iterable_1, iterable_2 = itertools.tee(iterable)
# get labels
labels = []
for graph in iterable_2:
label = graph.graph.get('id', None)
if label:
labels.append(label)
# transform iterable into sparse vectors
data_matrix = vectorizer.transform(iterable_1)
# embed high dimensional sparse vectors in 2D
from sklearn import metrics
distance_matrix = metrics.pairwise.pairwise_distances(data_matrix)
from sklearn.manifold import MDS
feature_map = MDS(n_components=n_components, dissimilarity='precomputed')
explicit_data_matrix = feature_map.fit_transform(distance_matrix)
from sklearn.decomposition import TruncatedSVD
pca = TruncatedSVD(n_components=2)
low_dimension_data_matrix = pca.fit_transform(explicit_data_matrix)
plt.figure(figsize=(size, size))
embed_dat_matrix_two_dimensions(low_dimension_data_matrix, labels=labels, density_colormap=colormap)
plt.show()
示例4: scale_plot
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def scale_plot(input_data, data_colors=None, cluster_colors=None,
cluster_sizes=None, dissimilarity='euclidean', filey=None):
""" Plot MDS of data and clusters """
if data_colors is None:
data_colors = 'r'
if cluster_colors is None:
cluster_colors='b'
if cluster_sizes is None:
cluster_sizes = 2200
# scale
mds = MDS(dissimilarity=dissimilarity)
mds_out = mds.fit_transform(input_data)
with sns.axes_style('white'):
f=plt.figure(figsize=(14,14))
plt.scatter(mds_out[n_clusters:,0], mds_out[n_clusters:,1],
s=75, color=data_colors)
plt.scatter(mds_out[:n_clusters,0], mds_out[:n_clusters,1],
marker='*', s=cluster_sizes, color=cluster_colors,
edgecolor='black', linewidth=2)
# plot cluster number
offset = .011
font_dict = {'fontsize': 17, 'color':'white'}
for i,(x,y) in enumerate(mds_out[:n_clusters]):
if i<9:
plt.text(x-offset,y-offset,i+1, font_dict)
else:
plt.text(x-offset*2,y-offset,i+1, font_dict)
if filey is not None:
plt.title(path.basename(filey)[:-4], fontsize=20)
save_figure(f, filey)
plt.close()
示例5: main
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def main():
args = docopt(__doc__)
is_mds = args['--mds']
# load datasets
digits = load_digits()
X = digits.data
y = digits.target
labels = digits.target_names
# dimension reduction
if is_mds:
model = MDS(n_components=2)
else:
model = PCA(n_components=2)
X_fit = model.fit_transform(X)
for i in range(labels.shape[0]):
plt.scatter(X_fit[y == i, 0], X_fit[y == i, 1],
color=COLORS[i], label=str(i))
plt.legend(loc='upper left')
plt.autoscale()
plt.grid()
plt.show()
示例6: plotFlatClusterGraph
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def plotFlatClusterGraph(tf_idf_matrix, clusters, headlines_utf):
dist = 1 - cosine_similarity(tf_idf_matrix)
MDS()
mds = MDS(n_components=2, dissimilarity="precomputed", random_state=1)
pos = mds.fit_transform(dist)
xs, ys = pos[:, 0], pos[:, 1]
cluster_colors = {0: '#FE642E', 1: '#B40404', 2: '#D7DF01', 3: '#01DF01', 4: '#00FFBF', 5: '#2E64FE', 6:'#8904B1', 7:'#FA58F4', 8:'#FE2E9A', 9:'#A4A4A4'}
#create data frame that has the result of the MDS plus the cluster numbers and titles
df = pandas.DataFrame(dict(x=xs, y=ys, label=clusters, title=headlines_utf))
groups = df.groupby('label')
# set up plots
fig, ax = plt.subplots(figsize=(17, 9)) # set size
#iterate through groups to layer the plots
for name, group in groups:
ax.plot(group.x, group.y, marker='o', linestyle='', ms=12, color=cluster_colors[name], mec='none')
ax.set_aspect('auto')
ax.tick_params(axis= 'x', which='both', bottom='off', top='off', labelbottom='off')
ax.tick_params(axis= 'y', which='both', left='off', top='off', labelleft='off')
ax.legend(numpoints=1) #show legend with only 1 point
#add label in x,y position with the label as the film title
for t_n in range(len(df)):
ax.text(df.ix[t_n]['x'], df.ix[t_n]['y'], df.ix[t_n]['title'], size=8)
plt.savefig('../plots/flat_clusters.png', dpi=400)
示例7: create_2dprojection
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def create_2dprojection(distmat):
#uses isomap to return a species distance map in 2d based on the topological distmat of all species in tree
print 'map to 3d space'
mapper=MDS(n_components=3, metric=True, n_init=4, max_iter=300, verbose=0, eps=0.001, n_jobs=-1, random_state=0, dissimilarity='precomputed')
projmat =mapper.fit_transform(distmat)
print 'DONE'
return projmat
示例8: reorder_channels_by_xyz_coord
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def reorder_channels_by_xyz_coord(data, channel_names=None):
"""
:param data: 2-d array in the format [n_samples, n_channels]
:param channel_names: names of the EEG channels
:return: data, channel_names permutated accordingly
"""
# work on transposed view, i.e. [channel, samples]
data = data.T
# map channels to 1-d coordinates through MDS
from sklearn.manifold import MDS
distances = compute_electrode_distance_matrix()
mds = MDS(n_components=1, dissimilarity='precomputed')
projection = mds.fit_transform(distances).reshape(data.shape[0])
order = np.argsort(projection)
print mds.stress_
print order
# re-order channels
data = data[order]
# restore initial axes layout
data = data.T
# re-order channel_names
channel_names = reorder_channel_names(channel_names, order)
return data, channel_names
示例9: main
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def main():
digits = load_digits()
X = digits.data
y = digits.target
mds = MDS()
X_mds = mds.fit_transform(X)
plot_embedding(X_mds, y)
示例10: visualize_clusters
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def visualize_clusters(tfidf_matrix, vocabulary, km):
# calcuate the cosine distance between each document
# this will be used for plotting on a euclidean (2-dimensional) plane.
dist = 1 - cosine_similarity(tfidf_matrix)
clusters = km.labels_.tolist()
# convert two components as we are plotting points in a two-dimensional plane
# 'precomputed' because we provide a distance matrix
# we will also specify 'random_state' so the plot is reproducible.
mds = MDS(n_components=2, dissimilarity="precomputed", random_state=1)
pos = mds.fit_transform(dist) # shape (n_components, n_samples)
xs, ys = pos[:, 0], pos[:, 1]
# set up colors per clusters using a dict
cluster_colors = {0: '#1b9e77', 1: '#d95f02', 2: '#7570b3', 3: '#e7298a', 4: '#66a61e', 5: '#99cc00'}
# set up cluster names using a dict (perhaps using the top terms of each cluster)
cluster_names = {0: '0',
1: '1',
2: '2',
3: '3',
4: '4',
5: '5'}
#create data frame that has the result of the MDS plus the cluster numbers and titles
df = pd.DataFrame(dict(x=xs, y=ys, label=clusters))
#group by cluster
groups = df.groupby('label')
# set up plot
fig, ax = plt.subplots(figsize=(17, 9)) # set size
ax.margins(0.05) # Optional, just adds 5% padding to the autoscaling
#iterate through groups to layer the plot
#note that I use the cluster_name and cluster_color dicts with the 'name' lookup to return the appropriate color/label
for name, group in groups:
ax.plot(group.x, group.y, marker='o', linestyle='', ms=12,
label=cluster_names[name], color=cluster_colors[name],
mec='none')
ax.set_aspect('auto')
ax.tick_params(\
axis= 'x', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
bottom='off', # ticks along the bottom edge are off
top='off', # ticks along the top edge are off
labelbottom='off')
ax.tick_params(\
axis= 'y', # changes apply to the y-axis
which='both', # both major and minor ticks are affected
left='off', # ticks along the bottom edge are off
top='off', # ticks along the top edge are off
labelleft='off')
ax.legend(numpoints=1) #show legend with only 1 point
plt.show() #show the plot
示例11: generate_cluster_plot_frame
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def generate_cluster_plot_frame(self):
MDS()
mds = MDS(n_components=2, dissimilarity="precomputed", random_state=1)
dist = 1 - cosine_similarity(self.tfidf_matrix)
pos = mds.fit_transform(dist)
xs, ys = pos[:,0], pos[:,1]
self.cluster_plot_frame = pd.DataFrame(dict(x=xs, y=ys, label=self.clusters, chapter=self.chapter_list, book=self.book_list))
示例12: plot_clusters
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def plot_clusters(num_clusters, feature_matrix,
cluster_data, movie_data,
plot_size=(16,8)):
# generate random color for clusters
def generate_random_color():
color = '#%06x' % random.randint(0, 0xFFFFFF)
return color
# define markers for clusters
markers = ['o', 'v', '^', '<', '>', '8', 's', 'p', '*', 'h', 'H', 'D', 'd']
# build cosine distance matrix
cosine_distance = 1 - cosine_similarity(feature_matrix)
# dimensionality reduction using MDS
mds = MDS(n_components=2, dissimilarity="precomputed",
random_state=1)
# get coordinates of clusters in new low-dimensional space
plot_positions = mds.fit_transform(cosine_distance)
x_pos, y_pos = plot_positions[:, 0], plot_positions[:, 1]
# build cluster plotting data
cluster_color_map = {}
cluster_name_map = {}
for cluster_num, cluster_details in cluster_data.items():
# assign cluster features to unique label
cluster_color_map[cluster_num] = generate_random_color()
cluster_name_map[cluster_num] = ', '.join(cluster_details['key_features'][:5]).strip()
# map each unique cluster label with its coordinates and movies
cluster_plot_frame = pd.DataFrame({'x': x_pos,
'y': y_pos,
'label': movie_data['Cluster'].values.tolist(),
'title': movie_data['Title'].values.tolist()
})
grouped_plot_frame = cluster_plot_frame.groupby('label')
# set plot figure size and axes
fig, ax = plt.subplots(figsize=plot_size)
ax.margins(0.05)
# plot each cluster using co-ordinates and movie titles
for cluster_num, cluster_frame in grouped_plot_frame:
marker = markers[cluster_num] if cluster_num < len(markers) \
else np.random.choice(markers, size=1)[0]
ax.plot(cluster_frame['x'], cluster_frame['y'],
marker=marker, linestyle='', ms=12,
label=cluster_name_map[cluster_num],
color=cluster_color_map[cluster_num], mec='none')
ax.set_aspect('auto')
ax.tick_params(axis= 'x', which='both', bottom='off', top='off',
labelbottom='off')
ax.tick_params(axis= 'y', which='both', left='off', top='off',
labelleft='off')
fontP = FontProperties()
fontP.set_size('small')
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.01), fancybox=True,
shadow=True, ncol=5, numpoints=1, prop=fontP)
#add labels as the film titles
for index in range(len(cluster_plot_frame)):
ax.text(cluster_plot_frame.ix[index]['x'],
cluster_plot_frame.ix[index]['y'],
cluster_plot_frame.ix[index]['title'], size=8)
# show the plot
plt.show()
示例13: compute_2d_mapping
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def compute_2d_mapping(layout):
sphere_coords = layout.sphere_coords()
radius = layout.sphere_radius()
from sklearn.manifold import MDS
distances = compute_electrode_distance_matrix(sphere_coords, radius)
mds = MDS(n_components=2, dissimilarity='precomputed')
projection = mds.fit_transform(distances)
# print projection.shape
return projection
示例14: mds_embed
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def mds_embed(graph):
sorted_node_list = sorted(list(graph.nodes()), key=len)
dmat = nx.floyd_warshall_numpy(graph, nodelist=sorted_node_list)
gmds = MDS(n_jobs=-2, dissimilarity="precomputed")
embed_pts = gmds.fit_transform(dmat)
return (embed_pts, dmat, sorted_node_list)
示例15: convert_matrix_to_coordinates
# 需要导入模块: from sklearn.manifold import MDS [as 别名]
# 或者: from sklearn.manifold.MDS import fit_transform [as 别名]
def convert_matrix_to_coordinates(sym_matrix, components):
"""
:param sym_matrix: array, [n_samples, n_samples]
:param components: int: 2 or 3 for MDS
:return: Output of MDS, xy or xyz coordinates as 2d numpy array
with shape [n_samples, components]
"""
# Create coordinates based on multi dimensional scaling
mds = MDS(n_components=components, dissimilarity="precomputed", random_state=1)
coordinates = mds.fit_transform(sym_matrix)
return coordinates