t-SNE

t-SNE 可视化降维方法简介 - Pand`s Blog (pandint.github.io)
详解可视化利器 t-SNE 算法:数无形时少直觉 | 机器之心 (jiqizhixin.com)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from sklearn.cluster import SpectralClustering
from sklearn.manifold import TSNE
import seaborn as sns

tsne = TSNE(
n_components=2,
perplexity=30, # 控制局部和全局结构之间的平衡,默认值通常在5到50之间
learning_rate='auto',
init='pca',
random_state=1024 # 设置随机种子确保结果可复现
)

# 运行t-SNE算法进行降维可视化
embedded_data = tsne.fit_transform(X_reduced)

fig, axs = plt.subplots(1,4, sharex=True, sharey=True, figsize=(15,4))

for i, ax in enumerate(axs.flat):
spec = SpectralClustering(n_clusters=i+3)
spec.fit(X_reduced)

# kmeans = KMeans(n_clusters=i+3, n_init='auto')
# kmeans.fit(X_reduced)

# sns.set_context({'figure.figsize':[10, 10]})
palette = sns.color_palette("bright", i+3)
sns.scatterplot(x=embedded_data.T[0], y=embedded_data.T[1], hue=spec.labels_ ,palette=palette, ax=ax)
ax.set_title(f"n_clusters={i+3}")
# print(i, calinski_harabasz_score(X_reduced, spec.labels_))

plt.show()

umap

机器学习算法:UMAP 深入理解 - 知乎 (zhihu.com)
数据处理降维方法UMAP(Uniform Manifold Approximation and Projection)学习跟练_空间代谢组学数据umap降维代码-CSDN博客