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

时间:2017-07-19 13:19:19

标签: python-3.x matplotlib plot scikit-learn

我有一个简单的问题:我想绘制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'。我做错了什么?

1 个答案:

答案 0 :(得分:3)

x仍然是一个数据帧,你应该将输出从拟合保存到变量

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

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