采用tsne画数据集的分布图
from sklearn.manifold import TSNE
import numpy as np
import time
import matplotlib.pyplot as plt
import pandas as pd
# fig = plt.figure()
# ax = fig.add_subplot(1, 1, 1, projection='3d')
# for i in range(X.shape[0]):
# ax.text(X[i, 0], X[i, 1], X[i, 2], str(digits.target[i]),
# color=plt.cm.Set1(y[i] / 10.),
# fontdict={'weight': 'bold', 'size': 9})
# if title is not None:
# plt.title(title)
name = ['adenocarcinoma']
for j in range(len(name)):
dataset = pd.read_csv('C:/Users/' + name[j] + '.csv', header=None)
print(name[j], dataset.shape)
shape = dataset.shape[1] - 1
shape1 = dataset.shape[0]
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
tsne = TSNE(n_components=2, init='pca', random_state=0)
t0 = time.time()
result = tsne.fit_transform(x)
print(result.shape)
x_min, x_max = np.min(result, 0), np.max(result, 0) # 每列中的最小值和最大值
data = (result - x_min) / (x_max - x_min) # 归一化
# print(x_min)
# print(x_max)
print('----')
# print(data)
# 3d图像
# fig = plt.figure()
# ax = fig.add_subplot(1, 1, 1, projection='3d')
# for i in range(data.shape[0]):
# ax.text(data[i, 0], data[i, 1], data[i, 2], str(y[i]),
# color=plt.cm.Set1(y[i] / 10.),
# fontdict={'weight': 'bold', 'size': 9})
# 2d图像
fig = plt.figure(figsize=(10, 7))
markers = ["+", "o", "<", ">", "*", ".", ",", "p", "3"]
for i in range(data.shape[0]):
plt.text(data[i, 0], data[i, 1], str(y[i]), color=plt.cm.Set1(y[i]),
fontdict={'weight': 'bold', 'size': 9})
plt.xlim()
plt.ylim()
plt.title(name[j])
plt.savefig('C:/Users/tsne_' + name[j] + '.png', dpi=500)