TSNE-降维可视化,函数封装

TSNE-降维可视化

from sklearn.manifold import TSNE
import pandas as pd
import numpy as np
from time import time
import matplotlib.pyplot as plt
import seaborn as sns

data_path='./dataset.xlsx'
train_data=pd.read_excel(data_path)
print(type(train_data))

origin_data=np.array(train_data)
data,label=np.split(origin_data,(6,),axis=1)
#print(label.reshape(-1))
print(label.shape)


def perform_tsne(X_data, y_data, perplexities, n_iter=1000, img_name_prefix='t-sne'):
    for index, perplexity in enumerate(perplexities):
        # perform t-sne
        print('\nperforming tsne with perplexity {} and with {} iterations at max'.format(perplexity, n_iter))
        #自行调节学习率
        X_reduced = TSNE(verbose=2,init='pca',random_state=0, learning_rate=0.0001,perplexity=perplexity).fit_transform(X_data)
        print(X_reduced)
        print('Done..')


        # prepare the data for seaborn
        print('Creating plot for this t-sne visualization..')
        #注意下面label的维数,这里reshape(-1)是为了将(700,1)变为(700)
        df = pd.DataFrame({'x': X_reduced[:, 0], 'y': X_reduced[:, 1], 'label': y_data.reshape(-1)})

        # draw the plot in appropriate place in the grid
        sns.lmplot(data=df, x='x', y='y', hue='label', fit_reg=False, size=8, \
                   palette="Set1", markers=['^', 'v', 's'])
        plt.title("perplexity : {} and max_iter : {}".format(perplexity, n_iter))
        img_name = img_name_prefix + '_perp_{}_iter_{}.png'.format(perplexity, n_iter)
        print('saving this plot as image in present working directory...')
        plt.savefig(img_name)
        plt.show()
        print('Done')

X_pre_tsne = data
y_pre_tsne = label

perform_tsne(X_data = X_pre_tsne,y_data=y_pre_tsne, perplexities =[5,10,50,100],n_iter=1000)

最终结果为如下图所示:
在这里插入图片描述
因为数据集的原因,可能降维效果不是很好。或是可以调整参数优化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值