总结
豆包 AI 助手文章总结

t-SNE算法的原理和Python代码实现详解

WBOY
发布: 2024-01-22 23:48:05
转载
2130人浏览过

t分布随机邻域嵌入(t-sne)算法原理及python代码实现t-sne算法

T分布随机邻域嵌入(t-SNE),是一种用于可视化的无监督机器学习算法,使用非线性降维技术,根据数据点与特征的相似性,试图最小化高维和低维空间中这些条件概率(或相似性)之间的差异,以在低维空间中完美表示数据点。

立即学习Python免费学习笔记(深入)”;

因此,t-SNE擅长在二维或三维的低维空间中嵌入高维数据以进行可视化。需要注意的是,t-SNE使用重尾分布来计算低维空间中两点之间的相似度,而不是高斯分布,这有助于解决拥挤和优化问题。而且离群值不影响t-SNE。

立即学习Python免费学习笔记(深入)”;

t-SNE算法步骤

立即学习Python免费学习笔记(深入)”;

1.找出高维空间中相邻点之间的成对相似性。

立即学习Python免费学习笔记(深入)”;

2.根据高维空间中点的成对相似性,将高维空间中的每个点映射到低维映射。

立即学习Python免费学习笔记(深入)”;

3.使用基于Kullback-Leibler散度(KL散度)的梯度下降找到最小化条件概率分布之间的不匹配的低维数据表示。

立即学习Python免费学习笔记(深入)”;

4.使用Student-t分布计算低维空间中两点之间的相似度。

立即学习Python免费学习笔记(深入)”;

MNIST数据集上实现t-SNE的Python代码

导入模块

# Importing Necessary Modules.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
from sklearn.preprocessing import StandardScaler
登录后复制

读取数据

# Reading the data using pandas
df = pd.read_csv('mnist_train.csv')

# print first five rows of df
print(df.head(4))

# save the labels into a variable l.
l = df['label']

# Drop the label feature and store the pixel data in d.
d = df.drop("label", axis = 1)
登录后复制

数据预处理

# Data-preprocessing: Standardizing the data
from sklearn.preprocessing import StandardScaler

standardized_data = StandardScaler().fit_transform(data)
print(standardized_data.shape)
登录后复制

输出

# TSNE
# Picking the top 1000 points as TSNE
# takes a lot of time for 15K points
data_1000 = standardized_data[0:1000, :]
labels_1000 = labels[0:1000]

model = TSNE(n_components = 2, random_state = 0)
# configuring the parameters
# the number of components = 2
# default perplexity = 30
# default learning rate = 200
# default Maximum number of iterations
# for the optimization = 1000

tsne_data = model.fit_transform(data_1000)

# creating a new data frame which
# help us in plotting the result data
tsne_data = np.vstack((tsne_data.T, labels_1000)).T
tsne_df = pd.DataFrame(data = tsne_data,
columns =("Dim_1", "Dim_2", "label"))

# Plotting the result of tsne
sn.FacetGrid(tsne_df, hue ="label", size = 6).map(
plt.scatter, 'Dim_1', 'Dim_2').add_legend()

plt.show()
登录后复制

立即学习Python免费学习笔记(深入)”;

以上就是t-SNE算法的原理和Python代码实现详解的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
相关标签:
来源:网易伏羲网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号