简体   繁体   English

在numpy文件中存储和提取数组时出现问题

[英]issues storing and extracting arrays in numpy file

Trying to store an array in numpy file however, while trying to extract it, and use it, getting an error message as trying to apply array to a sequence. 但是,在尝试提取数组并使用它时,尝试将其存储在numpy文件中,并收到一条错误消息,即尝试将数组应用于序列。

These are the two arrays, unsure which one is causing the issue. 这是两个数组,不确定是哪个引起了问题。

X = [[1,2,3],[4,5,6],[7,8,9]]
y = [0,1,2,3,4,5,6....]

while trying to retrieve it and use it getting the values as: 尝试检索它并使用它获取值时:

X: array(list[1,2,3],list[4,5,6],list[7,8,9])
y = array([0,1,2,3,4,5...])

Here is the code: 这是代码:

vectors = np.array(X)
labels = np.array(y)

While retrieving working on t-sne 在检索t-sne的工作时

visualisations = TSNE(n_components=2).fit_transform(X,y)

I get the following error: 我收到以下错误:

ValueError                                Traceback (most recent call last)
<ipython-input-11-244f99341167> in <module>()
----> 1 visualisations = TSNE(n_components=2).fit_transform(X,y)

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\manifold\t_sne.py in fit_transform(self, X, y)
    856             Embedding of the training data in low-dimensional space.
    857         """
--> 858         embedding = self._fit(X)
    859         self.embedding_ = embedding
    860         return self.embedding_

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\manifold\t_sne.py in _fit(self, X, skip_num_points)
    658         else:
    659             X = check_array(X, accept_sparse=['csr', 'csc', 'coo'],
--> 660                             dtype=[np.float32, np.float64])
    661         if self.method == 'barnes_hut' and self.n_components > 3:
    662             raise ValueError("'n_components' should be inferior to 4 for the "

C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    431                                       force_all_finite)
    432     else:
--> 433         array = np.array(array, dtype=dtype, order=order, copy=copy)
    434 
    435         if ensure_2d:

ValueError: setting an array element with a sequence.

Assuming I understand you correctly you need to package the first group in a list; 假设我对您的理解正确,则需要将第一组包装在列表中。 something like this: 像这样的东西:

import numpy as np

#X = [[1,2,3],[4,5,6],[7,8,9]]
#y = [0,1,2,3,4,5,6, 7, 8, 9]

X = np.array([[1,2,3],[4,5,6],[7,8,9]])
y = np.array([0,1,2,3,4,5, 6, 7, 8, 9])
array(list[1,2,3],list[4,5,6],list[7,8,9])

is a 1d object dtype array. 是一维对象dtype数组。 To get that from 从那得到

[[1,2,3],[4,5,6],[7,8,9]]

requires more than np.array([[1,2,3],[4,5,6],[7,8,9]]) ; 需要超过np.array([[1,2,3],[4,5,6],[7,8,9]]) ; either the list elements have to vary in size, or you have to initialize an object array and copy the list values to it. 列表元素的大小必须有所不同,或者您必须初始化对象数组并将列表值复制到其中。

In any case fit_transform cannot handle that kind of array. 无论如何, fit_transform无法处理这种数组。 It expects a 2d numeric dtype. 它需要一个二维数字dtype。 Notice the parameters to the check_array function. 注意check_array函数的参数。

If all the list elements of X are the same size, then 如果X所有列表元素大小都相同,则

X = np.stack(X)

should turn it into a 2d numeric array. 应该把它变成一个二维数字数组。

I suspect X was that 1d object array type before saving. 我怀疑X是保存之前的1d对象数组类型。 By itself save/load should not turn a 2d numeric array into an object one. save/load本身不应将2维数字数组转换为对象1。

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

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