一种用于数据降维可视化算法t-SNE,Python实现,欢迎批评指正。

1.t-SNE集成在机器学习库scikit-learn中

2.本文中的数据仅有三个特征(3波段,bgr)

3.样本数据存储在csv中,由遥感图像及标签数据转换得到

4.类别3

5.样本数量3000

# -*- coding: utf-8 -*-
'''
  @Time    : 2022/9/10 20:47
  @Author  : lutingyu
  @FileName: t-SNE.py.py
  @Software: PyCharm
  @descrbtion:降维可视化算法t-SNE
'''
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.manifold import TSNE
import pandas as pd
import random
from random import randint
from random import sample

def tsne_data_visual(csvFile='./tsne_data.csv'):
    data = pd.read_csv(csvFile)
    features = data.iloc[:, 0:3].values  # 波段b g r
    label = data.iloc[:, 3:].values  # 标签
    label = np.squeeze(label, axis=1)
    print(features)
    print(label.shape)
    model = TSNE(n_components=2, init='pca', random_state=501)  # 映射到2维
    # #
    transformed = model.fit_transform(features)
    print(transformed.shape)
    x_axis = transformed[:, 0]
    print(x_axis.shape[0])
    y_axis = transformed[:, 1]
    print(y_axis.shape[0])
    #
    plt.xticks([])
    plt.yticks([])
    #
    colors = ["#FFA0A0", "#00FF00", "#FFFF00"]  #颜色字典
    markers = ["o", "^", "s"]                   #样式字典
    edgecolors='#646464'                        #边框颜色
    for i in range(x_axis.shape[0]):
        plt.scatter(x_axis[i], y_axis[i], s=50, color=colors[label[i] - 1], marker=markers[label[i] - 1],linewidths=1,edgecolors='#646464')
    plt.show()

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值