一种用于数据降维可视化算法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()

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

 

你可能感兴趣的:(python,算法,开发语言)