基于sklearn.manifold的 T-SNE 的简单使用(介绍关系数据和图像数据)+ matplotlib的简单使用

0. 写作目的

好记性不如烂笔头。

1. 针对关系数据(表格类型)的使用

1.1 将关系数据降维二维

dataNumpy为numpy.array类型的数据。详细见参考[1].

from sklearn.manifold import TSNE
import numpy as np


## this parameters are default parameters
## data_tsne is:  array, shape (n_samples, n_components)
             #     Embedding of the training data in low-dimensional space.

data_tsne = TSNE(n_components=2, perplexity=30.0, early_exaggeration=12.0, learning_rate=200.0, n_iter=1000, n_iter_without_progress=300, min_grad_norm=1e-07, metric=’euclidean’, init=’random’, verbose=0, random_state=None, method=’barnes_hut’, angle=0.5).fit_transform( dataNumpy )

各参数的意义参考官方给出的解释:

重要的参数:

n_components : int, optional (default: 2)

               Dimension of the embedded space.

perplexity : float, optional (default: 30)

            The perplexity is related to the number of nearest neighbors that is used in 
            other manifold learning algorithms. Larger datasets usually require a larger 
            perplexity. Consider selecting a value between 5 and 50. The choice is not 
            extremely critical since t-SNE is quite insensitive to this parameter.

n_iter : int, optional (default: 1000)

         Maximum number of iterations for the optimization. Should be at least 250.

learning_rate : float, optional (default: 200.0)

                The learning rate for t-SNE is usually in the range [10.0, 1000.0]. If 
                the learning rate is too high, the data may look like a ‘ball’ with any 
                point approximately equidistant from its nearest neighbours. If the 
                learning rate is too low, most points may l
`t-SNE` (t-Distributed Stochastic Neighbor Embedding) 是一种用于降维的非线性技术,特别适合于高维度数据可视化。通过将高维特征映射到低维空间(通常是二维或三维),它可以保留样本之间的局部结构信息,使得相似的样本在低维空间中仍然靠近。 以下是关于如何使用 `sklearn.manifold.TSNE` 的简单介绍及示例代码: --- ### 主要功能与特点 - **适用于高维数据分析**:能够揭示复杂的模式簇群分布; - **强调局部邻近关系**:相较于 PCA 等传统方法,它能更好地反映数据间的距离比例; - **局限性注意事项**: - 计算成本较高,不适合处理超大数据集; - 参数调节敏感度较大,需谨慎设置学习率、困惑度等关键选项; ### 常见参数解析 | 参数名称 | 描述 | |----------------|------------------------------------------------------------------------------------------| | n_components | 输出嵌入向量的空间维度,默认值为2 | | perplexity | 表征平衡点附近的有效邻居数目大小的一个重要指标 | | early_exaggeration | 控制初始阶段优化过程的程度 | | learning_rate | 学习速率 | --- ### 示例代码展示 下面提供一段简单的例子,演示加载手写数字图片数据MNIST并通过TSNE将其转换成平面坐标绘制散点图的过程: ```python from sklearn.datasets import load_digits from sklearn.manifold import TSNE import matplotlib.pyplot as plt # 加载digits数据集 digits = load_digits() X, y = digits.data, digits.target # 应用 t-SNE 进行降维操作 tsne = TSNE(n_components=2, random_state=42) X_embedded = tsne.fit_transform(X) # 绘制结果 plt.figure(figsize=(10, 8)) for i in range(len(digits.target_names)): idx = y == i plt.scatter(X_embedded[idx, 0], X_embedded[idx, 1], label=str(i)) plt.legend(bbox_to_anchor=[1, 1]) plt.xlabel('Dimension 1') plt.ylabel('Dimension 2') plt.title('Handwritten Digits Visualized with t-SNE') plt.show() ``` 上述脚本首先导入必要模块然后利用scikit提供的内置小规模图像数据库作为测试输入源材料,最后呈现出来的图形会把各个类别分开显示出来方便观察区分效果好坏程度. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值