如何绘制 scikit 的 t-sne 输出数组?

Posted

技术标签:

【中文标题】如何绘制 scikit 的 t-sne 输出数组?【英文标题】:How to plot scikit's t-sne output array? 【发布时间】:2017-12-24 19:16:21 【问题描述】:

我有一个简单的问题:我想绘制 scikit 的TSNE 的结果。这是我的工作:

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE

x = df
model = TSNE(n_components=2, random_state=0)
model.fit_transform(x)

也就是说,我有一个相当大的数据框df。我运行TSNE。结果是一个数组,格式为

array([[  76.29642863,  -37.2316455 ],
       [  83.28206784,  -35.19983576],
       [ -10.93624436, -109.37550438],
       [  -3.39073195, -107.29288942],
       [  67.66224215,   -2.2439687 ],
       [  30.82205017,  -19.28584524],
       [  47.35280231,  -27.03922442],
       [  81.63500728,  -14.94290102],
       [  36.48278883,  -76.13490133],
       [  51.12458861,  -12.63904649],
       [  27.37677179,  -63.35607228],
       [  57.70966133,  -22.75333134],
       [ -60.32088017,   76.30353357],
       [  33.80070371,   -5.59486463],
       [  27.80748222,  -43.02255416],
       [   8.96419645,   -5.12084324],
       [ 100.61407581,  -50.03230332],
       [  41.32789364,   73.4504622 ],
       [  20.51559019,   -2.0397622 ],
       [   8.72757049,    9.42719579],
       [ -97.09646231,    6.75980242],
       [ -76.37504347,  -67.92449062],
       [  40.8922133 ,  -54.64473336],
       [  21.07407628,   88.54593568],
       [  46.75201811,  -43.85971949],
       [ -62.77960662,  -73.14171491],
       [  83.01345537,  -52.70083606],
       [  84.660757  ,   31.84526227],
       [ -45.30560344,  -80.44506703],
       [  11.42866734, -121.98241383],
       [  45.33793383,   11.43223043],
       [  48.66860826,   22.27729015],
       [ -36.32341645,   89.51767685],
       [  48.29651789,   40.73176577],
       [   6.83643714,   70.83466828],
       [  31.13233995,   63.47771401],
       [  34.78917733,   22.13881353],
       [  43.99847623,   31.40394748],
       [ -76.43334995,  -46.1576235 ],
       [  16.35274296,   24.83566003],
       [  63.76553398,   43.75524517],
       [  17.8211855 ,   66.45561224],
       [  24.03955628,   33.26653325],
       [  34.70078871,   42.87820685],
       [  55.27398051,   63.73362412],
       [  59.86821001,   19.59977048],
       [   3.18558424,   51.5108358 ],
       [  21.17605571,   44.4258268 ],
       [  70.05979974,   58.17860968],
       [  13.11104526,   51.09243066],
       [   4.38454865,   40.35813416],
       [ -46.6602056 ,   32.57412335],
       [  64.7773806 ,  -58.23294757],
       [  -9.77563246,   22.91711485],
       [  -4.63795974,   34.74267219],
       [ -21.0280986 ,   42.4962899 ],
       [ -15.06925112,   33.66875649],
       [ -23.38102294,   52.59088233],
       [ -11.50369006,   56.07444623],
       [ -23.69948462,  -81.30270807],
       [ -27.78705573,   62.36384916],
       [ -36.11364478,   38.5400532 ],
       [ -34.22060036,   47.86420995],
       [-130.95539878,    7.47698021],
       [ -99.13256617,  -10.0366437 ],
       [ -59.63287577,   31.69736952],
       [  -5.51708328,  -68.92901655],
       [ -64.35945061,   -0.92522887],
       [ -42.98021829,    1.35631152],
       [ -51.40872526,   45.27740173],
       [ -53.20299633,    3.93563808],
       [ -33.41025132,    0.61349718],
       [ -64.20144157,    9.83423394],
       [ -43.76759426,  -11.21050429],
       [   2.95926436,  -27.24926603],
       [   4.92291235,  -40.27121135],
       [ -32.32069564,  -14.38969796],
       [ -21.41289045,   -8.75338496],
       [  42.71465077,   56.97575024],
       [ -77.00615794,   36.85072343],
       [ -22.37961976,  -60.39178939],
       [  -2.91171663,  -50.77959613],
       [ -50.60028744,  -22.71077905],
       [ -80.14729221,    9.56335332],
       [ -62.70963932,  -22.9475924 ],
       [ -62.51993937,  -36.2723845 ],
       [ -71.86583485,   21.0174738 ],
       [ -38.72063372,  -27.05935043],
       [ -47.74223044,  -34.38562981],
       [  53.9395216 ,   51.75186133],
       [ -23.80825858,  -43.32942045],
       [ -16.85950018,  -50.83328698],
       [  -9.79065232,  -29.92011604],
       [ -19.76104305,  -33.2619343 ],
       [ -16.9120824 ,  -21.3528993 ],
       [  65.35647493,  -13.17005252],
       [  42.60852749,  -17.24403937],
       [  30.16514905,   53.77173972],
       [  21.99950701,   10.54130299],
       [  44.00495851,   88.05582053],
       [  82.9521465 ,   14.17920421],
       [   6.0245289 ,   86.73697853],
       [  97.38831046,   59.19700621],
       [  70.64054431,   29.54171715],
       [  57.39849223,   32.87309563],
       [  34.04147517,   32.74829322],
       [  12.81861228,   36.26916722],
       [ -10.63982828,   44.92478561],
       [ -26.58140799,   31.14874014],
       [ -43.6592727 ,   57.07683942],
       [ -39.30343574,   23.39181145],
       [ -55.93416401,   17.19611905],
       [ -45.80009451,   12.10167266],
       [ -54.36943413,   -6.95559746],
       [  26.28194642,  -30.60665791],
       [ -63.04425748,   51.18032057],
       [ -77.67041924,   -1.10858794],
       [  -5.38753713,  -15.08877141],
       [ -85.79812772,   23.3840777 ],
       [ -42.34964347,  -42.59061027],
       [ -32.65764819,  -47.82663035],
       [ -27.87113561,  -26.01927025],
       [ -28.94307267,   10.93519372],
       [ -35.42888905,  -58.36504075],
       [ -21.44710197,   86.52932663],
       [ -11.41769981,  -40.65429075],
       [ -47.33535922,  -53.86342049],
       [ -31.07016357,  -36.23505085]])

我现在正在尝试这样绘制数组:

plt.scatter(x[50:,] , x[:,50])

但是,我抛出了错误unhashable type: 'slice'。我做错了什么?

【问题讨论】:

你没有使用fit_transform()返回的TSNE值。它不会就地转换变量x 【参考方案1】:

x 仍然是一个数据框,您应该将拟合的输出保存到变量中

res = model.fit_transform(x)
plt.scatter(res[50:,0] , res[50:,1])

然后你可以绘制 numpy 数组而不是数据框

【讨论】:

酷,我知道这是怎么回事!但是,我遇到了另一个错误:IndexError: index 50 is out of bounds for axis 1 with size 2- 我想我切错了。抱歉新手问题:( 我解决了!我只是将数组转换为熊猫数据框。 pd_res = pd.DataFrame(res[0:, 0:,]) 谢谢提醒!

以上是关于如何绘制 scikit 的 t-sne 输出数组?的主要内容,如果未能解决你的问题,请参考以下文章

IndexError:使用 scikit-learn 绘制 ROC 曲线时数组索引过多?

如何将 Scikit Learn OneVsRestClassifier 预测方法输出转换为谷歌云 ML 的密集数组?

Scikit-learn---2.降维

Scikit Learn - 如何绘制概率

如何将 scikit learn 的预测概率输出转换为 sigmoid

如何在用 scikit-learn / matplotlib 绘制的混淆矩阵中格式化 xticklabels?