python中TSNE的使用

1.TSNE理论

t-SNE主要用于可视化操作。

t-SNE 没有唯一最优解,每次结果随机,且不能用于预测。 

详细理论推导可看https://blog.csdn.net/sinat_20177327/article/details/80298645

2.代码实现 

步骤:

随机产生500个6维数据 。

使用K-means进行聚类,得到5类标签。

对数据进行6维数据进行TSNE可视化。(这一步是因为6维无法在二维图片中查看,我使用TSNE降维处理)

使用TSNE得到的二维数据,K-means得到的标签画图展示。

# coding='utf-8'
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import torch
from sklearn.manifold import TSNE
from sklearn.cluster import KMeans
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def get_data(data,label):
    n_samples, n_features = data.shape
    return data, label, n_samples, n_features

def kmeans(fx, n, metric='cosine'):
    if metric == 'cosine':
        fn = fx / torch.clamp(torch.norm(fx, dim=1, keepdim=True), min=1e-20)
    elif metric == 'euclidean':
        fn = fx
    else:
        raise KeyError
    fn = fn.detach().cpu().numpy()
    # 初始化KMeans对象
    kmeans = KMeans(n_clusters=n, random_state=0)
    # 对数据进行拟合和预测
    kmeans.fit(fn)
    labels = kmeans.predict(fn)

    return fn,labels

def main():
    data = torch.randn(500, 6)
    fn, label = kmeans(data, 5, 'cosine')

    data, label, n_samples, n_features = get_data(fn, label)
    print('data.shape',data.shape)
    print('label',label)
    print('label中数字有',len(set(label)),'个不同的数字')
    print('data有',n_samples,'个样本')
    print('每个样本',n_features,'维数据')
    print('Computing t-SNE embedding')
    tsne = TSNE(n_components=2, init='pca', random_state=0)
    #归一化操作
    X_tsne = tsne.fit_transform(data)
    print('result.shape',X_tsne.shape)
    scaler = MinMaxScaler()
    X_tsne = scaler.fit_transform(X_tsne)
    plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=label, cmap='viridis')
    plt.show()


if __name__ == '__main__':
    main()

图中5个颜色代表聚类为5类,共500个点。 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值