python高维数据_t-SNE高维数据可视化(python)
t-SNE實(shí)踐——sklearn教程
t-SNE是一種集降維與可視化于一體的技術(shù),它是基于SNE可視化的改進(jìn),解決了SNE在可視化后樣本分布擁擠、邊界不明顯的特點(diǎn),是目前最好的降維可視化手段。
關(guān)于t-SNE的歷史和原理詳見(jiàn)從SNE到t-SNE再到LargeVis。
代碼見(jiàn)下面例一
TSNE的參數(shù)
函數(shù)參數(shù)表:
parameters描述
n_components
嵌入空間的維度
perpexity
混亂度,表示t-SNE優(yōu)化過(guò)程中考慮鄰近點(diǎn)的多少,默認(rèn)為30,建議取值在5到50之間
early_exaggeration
表示嵌入空間簇間距的大小,默認(rèn)為12,該值越大,可視化后的簇間距越大
learning_rate
學(xué)習(xí)率,表示梯度下降的快慢,默認(rèn)為200,建議取值在10到1000之間
n_iter
迭代次數(shù),默認(rèn)為1000,自定義設(shè)置時(shí)應(yīng)保證大于250
min_grad_norm
如果梯度小于該值,則停止優(yōu)化。默認(rèn)為1e-7
metric
表示向量間距離度量的方式,默認(rèn)是歐氏距離。如果是precomputed,則輸入X是計(jì)算好的距離矩陣。也可以是自定義的距離度量函數(shù)。
init
初始化,默認(rèn)為random。取值為random為隨機(jī)初始化,取值為pca為利用PCA進(jìn)行初始化(常用),取值為numpy數(shù)組時(shí)必須shape=(n_samples, n_components)
verbose
是否打印優(yōu)化信息,取值0或1,默認(rèn)為0=>不打印信息。打印的信息為:近鄰點(diǎn)數(shù)量、耗時(shí)、σσ、KL散度、誤差等
random_state
隨機(jī)數(shù)種子,整數(shù)或RandomState對(duì)象
method
兩種優(yōu)化方法:barnets_hut和exact。第一種耗時(shí)O(NlogN),第二種耗時(shí)O(N^2)但是誤差小,同時(shí)第二種方法不能用于百萬(wàn)級(jí)樣本
angle
當(dāng)method=barnets_hut時(shí),該參數(shù)有用,用于均衡效率與誤差,默認(rèn)值為0.5,該值越大,效率越高&誤差越大,否則反之。當(dāng)該值在0.2-0.8之間時(shí),無(wú)變化。
返回對(duì)象的屬性表:
Atrtributes描述
embedding_
嵌入后的向量
kl_divergence_
KL散度
n_iter_
迭代的輪數(shù)
t-distributed Stochastic Neighbor Embedding(t-SNE)
t-SNE可降樣本點(diǎn)間的相似度關(guān)系轉(zhuǎn)化為概率:在原空間(高維空間)中轉(zhuǎn)化為基于高斯分布的概率;在嵌入空間(二維空間)中轉(zhuǎn)化為基于t分布的概率。這使得t-SNE不僅可以關(guān)注局部(SNE只關(guān)注相鄰點(diǎn)之間的相似度映射而忽略了全局之間的相似度映射,使得可視化后的邊界不明顯),還關(guān)注全局,使可視化效果更好(簇內(nèi)不會(huì)過(guò)于集中,簇間邊界明顯)。
目標(biāo)函數(shù):原空間與嵌入空間樣本分布之間的KL散度。
優(yōu)化算法:梯度下降。
注意問(wèn)題:KL散度作目標(biāo)函數(shù)是非凸的,故可能需要多次初始化以防止陷入局部次優(yōu)解。
t-SNE的缺點(diǎn):
計(jì)算量大,耗時(shí)間是PCA的百倍,內(nèi)存占用大。
專(zhuān)用于可視化,即嵌入空間只能是2維或3維。
需要嘗試不同的初始化點(diǎn),以防止局部次優(yōu)解的影響。
t-SNE的優(yōu)化
在優(yōu)化t-SNE方面,有很多技巧。下面5個(gè)參數(shù)會(huì)影響t-SNE的可視化效果:
perplexity 混亂度。混亂度越高,t-SNE將考慮越多的鄰近點(diǎn),更關(guān)注全局。因此,對(duì)于大數(shù)據(jù)應(yīng)該使用較高混亂度,較高混亂度也可以幫助t-SNE拜托噪聲的影響。相對(duì)而言,該參數(shù)對(duì)可視化效果影響不大。
early exaggeration factor 該值表示你期望的簇間距大小,如果太大的話(大于實(shí)際簇的間距),將導(dǎo)致目標(biāo)函數(shù)無(wú)法收斂。相對(duì)而言,該參數(shù)對(duì)可視化效果影響較小,默認(rèn)就行。
learning rate 學(xué)習(xí)率。關(guān)鍵參數(shù),根據(jù)具體問(wèn)題調(diào)節(jié)。
maximum number of iterations 迭代次數(shù)。迭代次數(shù)不能太低,建議1000以上。
angle (not used in exact method) 角度。相對(duì)而言,該參數(shù)對(duì)效果影響不大。
代碼
例一
import numpy as np
import matplotlib.pyplot as plt
from sklearn import manifold, datasets
digits = datasets.load_digits(n_class=6)
X, y = digits.data, digits.target
n_samples, n_features = X.shape
'''顯示原始數(shù)據(jù)'''
n = 20 # 每行20個(gè)數(shù)字,每列20個(gè)數(shù)字
img = np.zeros((10 * n, 10 * n))
for i in range(n):
ix = 10 * i + 1
for j in range(n):
iy = 10 * j + 1
img[ix:ix + 8, iy:iy + 8] = X[i * n + j].reshape((8, 8))
plt.figure(figsize=(8, 8))
plt.imshow(img, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
plt.show()
'''t-SNE'''
tsne = manifold.TSNE(n_components=2, init='pca', random_state=501)
X_tsne = tsne.fit_transform(X)
print("Org data dimension is {}.
Embedded data dimension is {}".format(X.shape[-1], X_tsne.shape[-1]))
'''嵌入空間可視化'''
x_min, x_max = X_tsne.min(0), X_tsne.max(0)
X_norm = (X_tsne - x_min) / (x_max - x_min) # 歸一化
plt.figure(figsize=(8, 8))
for i in range(X_norm.shape[0]):
plt.text(X_norm[i, 0], X_norm[i, 1], str(y[i]), color=plt.cm.Set1(y[i]),
fontdict={'weight': 'bold', 'size': 9})
plt.xticks([])
plt.yticks([])
plt.show()
t-SNE高維數(shù)據(jù)可視化(python)
t-SNE(t-distributedstochastic neighbor embedding?)是目前最為流行的一種高維數(shù)據(jù)降維的算法。在大數(shù)據(jù)的時(shí)代,數(shù)據(jù)不僅越來(lái)越大,而且也變得越來(lái)越復(fù)雜,數(shù)據(jù)維度的轉(zhuǎn)化也在驚人的增加,例如,一組圖像的維度就是該圖像的像素個(gè)數(shù),其范圍從數(shù)千到數(shù)百萬(wàn)。
對(duì)計(jì)算機(jī)而言,處理高維數(shù)據(jù)絕對(duì)沒(méi)問(wèn)題,但是人類(lèi)能感知的確只有三個(gè)維度,因此很有必要將高維數(shù)據(jù)可視化的展現(xiàn)出來(lái)。那么如何將數(shù)據(jù)集從一個(gè)任意維度的降維到二維或三維呢。T-SNE就是一種數(shù)據(jù)降維的算法,其成立的前提是基于這樣的假設(shè):盡管現(xiàn)實(shí)世界中的許多數(shù)據(jù)集是嵌入在高維空間中,但是都具有很低的內(nèi)在維度。也就是說(shuō)高維數(shù)據(jù)經(jīng)過(guò)降維后,在低維狀態(tài)下更能顯示出其本質(zhì)特性。這就是流行學(xué)習(xí)的基本思想,也稱(chēng)為非線性降維。
下面就展示一下如何使用t-SNE算法可視化sklearn庫(kù)中的手寫(xiě)字體數(shù)據(jù)集。
import numpy as np
import sklearn
from sklearn.manifold import TSNE
from sklearn.datasets import load_digits
# Random state.
RS = 20150101
import matplotlib.pyplot as plt
import matplotlib.patheffects as PathEffects
import matplotlib
# We import seaborn to make nice plots.
import seaborn as sns
sns.set_style('darkgrid')
sns.set_palette('muted')
sns.set_context("notebook", font_scale=1.5,
rc={"lines.linewidth": 2.5})
digits = load_digits()
# We first reorder the data points according to the handwritten numbers.
X = np.vstack([digits.data[digits.target==i]
for i in range(10)])
y = np.hstack([digits.target[digits.target==i]
for i in range(10)])
digits_proj = TSNE(random_state=RS).fit_transform(X)
def scatter(x, colors):
# We choose a color palette with seaborn.
palette = np.array(sns.color_palette("hls", 10))
# We create a scatter plot.
f = plt.figure(figsize=(8, 8))
ax = plt.subplot(aspect='equal')
sc = ax.scatter(x[:,0], x[:,1], lw=0, s=40,
c=palette[colors.astype(np.int)])
plt.xlim(-25, 25)
plt.ylim(-25, 25)
ax.axis('off')
ax.axis('tight')
# We add the labels for each digit.
txts = []
for i in range(10):
# Position of each label.
xtext, ytext = np.median(x[colors == i, :], axis=0)
txt = ax.text(xtext, ytext, str(i), fontsize=24)
txt.set_path_effects([
PathEffects.Stroke(linewidth=5, foreground="w"),
PathEffects.Normal()])
txts.append(txt)
return f, ax, sc, txts
scatter(digits_proj, y)
plt.savefig('digits_tsne-generated.png', dpi=120)
plt.show()
可視化結(jié)果如下:
總結(jié)
以上是生活随笔為你收集整理的python高维数据_t-SNE高维数据可视化(python)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Android 6.0 源代码编译实践
- 下一篇: 下列选项中不符合python语言变量命名