简体   繁体   English

fit_transform(image)TSNE方法的数字数组格式

[英]Numpy array format for fit_transform(image) TSNE method

I'm using Scikit-learn for tSNE to interrogate around 1000 scatterplots, but I appear to require a 2D numpy array to access the fit_transform method. 我正在使用Scikit-learn进行tSNE询问大约1000个散点图,但是我似乎需要2D numpy数组才能访问fit_transform方法。 I'm new to Python. 我是Python的新手。

My code, 我的代码

from sklearn.manifold import TSNE
import numpy as np
import cv2
mypath='/Path/to/files/scatterplots/'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
photos = np.empty(len(onlyfiles), dtype=object)
for n in range(0, len(onlyfiles)):
  photos[n] = cv2.imread( join(mypath,onlyfiles[n]) )

fig, axes = plt.subplots(2, 2, figsize=(10,10), subplot_kw={'xticks':(), 'yticks':()})
for ax, img in zip(axes.ravel(), photos):
   ax.imshow(img)

output 输出 在此处输入图片说明

Problem code 问题代码

tsne = TSNE(random_state=50)
digits_tsne = tsne.fit_transform (photos.data)

Error 错误

ValueError Traceback (most recent call last) in ValueError追溯(最近一次通话最近)在

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/manifold/t_sne.py in fit_transform(self, X, y) fit_transform(self,X,y)中的/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sklearn/manifold/t_sne.py

892             Embedding of the training data in low-dimensional space.
893         """

--> 894 embedding = self._fit(X) -> 894嵌入= self._fit(X)

  • 3 further lines of error output within t_sne.py t_sne.py中输出的其他3行错误

I believe the fit_transform method requires a 2D numpy array, eg 我相信fit_transform方法需要2D numpy数组,例如

'target': array([0, 1, 2, 3])

where 0-3 refer to the different data (parasites) behind each of the scatterplots 1-4. 其中0-3表示每个散点图1-4后面的不同数据(寄生物)。

Request How do I combine the target array into image numpy array so fit_transform can see it and process it? 请求 如何将目标数组合并为图像numpy数组,以便fit_transform可以查看和处理它?

Please check documentation for t-SNE : 请检查t-SNE的文档:

X : array, shape (n_samples, n_features) X:数组,形状(n_samples,n_features)

For your case to work, you need to cast images to 1d array and assemble a matrix out of them. 为了使您的案例正常工作,您需要将图像投射到一维数组并从中组装出一个矩阵。

Codewise, the following snippet should do the job of 2-dimensional t-SNE clustering: 在代码方面,以下代码片段应完成二维t-SNE聚类的工作:

arr = [cv2.imread( join(mypath,onlyfiles[n])).ravel() for n in range(0, len(onlyfiles))]
X = np.vstack[arr]
tsne = TSNE(n_components=2).fit_transform(X)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM