1.TSNE理论
t-SNE主要用于可视化操作。
t-SNE 没有唯一最优解,每次结果随机,且不能用于预测。
详细理论推导可看https://blog.csdn.net/sinat_20177327/article/details/80298645
2.代码实现
步骤:
随机产生500个6维数据 。
使用K-means进行聚类,得到5类标签。
对数据进行6维数据进行TSNE可视化。(这一步是因为6维无法在二维图片中查看,我使用TSNE降维处理)
使用TSNE得到的二维数据,K-means得到的标签画图展示。
# coding='utf-8'
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt
import torch
from sklearn.manifold import TSNE
from sklearn.cluster import KMeans
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def get_data(data,label):
n_samples, n_features = data.shape
return data, label, n_samples, n_features
def kmeans(fx, n, metric='cosine'):
if metric == 'cosine':
fn = fx / torch.clamp(torch.norm(fx, dim=1, keepdim=True), min=1e-20)
elif metric == 'euclidean':
fn = fx
else:
raise KeyError
fn = fn.detach().cpu().numpy()
# 初始化KMeans对象
kmeans = KMeans(n_clusters=n, random_state=0)
# 对数据进行拟合和预测
kmeans.fit(fn)
labels = kmeans.predict(fn)
return fn,labels
def main():
data = torch.randn(500, 6)
fn, label = kmeans(data, 5, 'cosine')
data, label, n_samples, n_features = get_data(fn, label)
print('data.shape',data.shape)
print('label',label)
print('label中数字有',len(set(label)),'个不同的数字')
print('data有',n_samples,'个样本')
print('每个样本',n_features,'维数据')
print('Computing t-SNE embedding')
tsne = TSNE(n_components=2, init='pca', random_state=0)
#归一化操作
X_tsne = tsne.fit_transform(data)
print('result.shape',X_tsne.shape)
scaler = MinMaxScaler()
X_tsne = scaler.fit_transform(X_tsne)
plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=label, cmap='viridis')
plt.show()
if __name__ == '__main__':
main()
图中5个颜色代表聚类为5类,共500个点。