import time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.python.keras.utils import np_utils
from sklearn.manifold import TSNE
from sklearn.preprocessing import LabelEncoder
df = pd.read_excel(r'C:/Users/1/Desktop/TSNE/data.xlsx', header=None) # 载入数据(4500, 2049),最后一列为标签
X = np.expand_dims(df.values[:, 0:2048].astype(float), axis=2) # (4500, 2048, 1) float64
# print(X.shape, X.dtype)
Y = df.values[:, 2048] # (4500,) float64
# print(Y.shape, Y.dtype)
# time.sleep(10)
encoder = LabelEncoder() # 把标签做成one_hot encoding的形式
Y_encoded = encoder.fit_transform(Y)
Y_onehot = np_utils.to_categorical(Y_encoded) # (4500, 10) float32
# print(Y_onehot.shape, Y_onehot.dtype)
# time.sleep(10)
X = X.reshape(4500, 2048) # 根据文件大小自行调试,4500是行数,2048是列数,此列数比文件中少一列
# print(X.shape, X.dtype)
# 加载数据
def get_data():
"""
:return: 数据集、标签、样本数量、特征数量
"""
data = X
label = Y
n_samples = 4500 # 对应reshape中的行数
n_features = 2048 # 对应reshape中的列数
return data, label, n_samples, n_features
# 对样本进行预处理并画图
def plot_embedding(data, label, title):
"""
:param data:数据集
:param label:样本标签
:param title:图像标题
:return:图像
"""
x_min, x_max = np.min(data, 0), np.max(data, 0)
data = (data - x_min) / (x_max - x_min) # 对数据进行归一化处理
fig = plt.figure(figsize=(5.5, 4)) # Set the figure size in inches figsize=(7.3/2.54, 4.7/2.54)
ax = plt.subplot(111) # 创建子图,经过验证111正合适,尽量不要修改
# 遍历所有样本
for i in range(data.shape[0]):
# 在图中为每个数据点画出标签
if label[i] == 0:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#C25c63', markeredgecolor='#C25c63', markersize=4)
elif label[i] == 1:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#357fae', markeredgecolor='#357fae', markersize=4)
elif label[i] == 2:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#FFDC91CC', markeredgecolor='#FFDC91CC', markersize=4)
elif label[i] == 3:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#FFC0CB', markeredgecolor='#FFC0CB', markersize=4)
elif label[i] == 4:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#FF00FF', markeredgecolor='#FF00FF', markersize=4)
elif label[i] == 5:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#87CEFA', markeredgecolor='#87CEFA', markersize=4)
elif label[i] == 6:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#FAEBD7', markeredgecolor='#FAEBD7', markersize=4)
elif label[i] == 7:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#98FB98', markeredgecolor='#98FB98', markersize=4)
elif label[i] == 8:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#FFA500', markeredgecolor='#FFA500', markersize=4)
else:
plt.plot(data[i, 0], data[i, 1], 'o', markerfacecolor='#7876B1FF', markeredgecolor='#7876B1FF', markersize=4)
plt.xlim()
plt.ylim()
plt.title(title, fontsize=15, x=0.5, y=1)
return fig
# 主函数,执行t-SNE降维
def main():
data, label, n_samples, n_features = get_data() # 调用函数,获取数据集信息
print('Starting compute t-SNE Embedding...')
ts = TSNE(n_components=2, init='pca', random_state=0)
reslut = ts.fit_transform(data) # t-SNE降维
fig = plot_embedding(reslut, label, 'Input data visualisation') # 调用函数,绘制图像
plt.savefig("TSNE.png", dpi=600, format="png") # 显示图像
plt.show()
# 主函数
if __name__ == '__main__':
main()
tsne降维python可视化
需积分: 0 163 浏览量
2023-05-11
16:40:57
上传
评论 4
收藏 88.14MB ZIP 举报
hhl7324
- 粉丝: 0
- 资源: 4
最新资源
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
- 五相感应电机矢量控制模型MATLAB
- RGLED (1) (1).circ
- IMG_20240427_215747.jpg
- python下前端WEB学习笔记
- 田间种植行排号自动生成工具
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈