简体   繁体   English

将列表转换为 numpy 数组

[英]Converting list to numpy array

I have managed to load images in a folder using the command line sklearn: load_sample_images()我设法使用命令行 sklearn 将图像加载到文件夹中: load_sample_images()

I would now like to convert it to a numpy.ndarray format with float32 datatype我现在想将其转换为具有float32数据类型的numpy.ndarray格式

I was able to convert it to np.ndarray using : np.array(X) , however np.array(X, dtype=np.float32) and np.asarray(X).astype('float32') give me the error:我能够使用: np.array(X)将它转换为np.ndarray ,但是np.array(X, dtype=np.float32)np.asarray(X).astype('float32')给了我错误:

 ValueError: setting an array element with a sequence.

Is there a way to work around this?有没有办法解决这个问题?

from sklearn_theano.datasets import load_sample_images
import numpy as np  

kinect_images = load_sample_images()
X = kinect_images.images

X_new = np.array(X)  # works
X_new = np.array(X[1], dtype=np.float32)  # works

X_new = np.array(X, dtype=np.float32)  # does not work

If you have a list of lists, you only needed to use ...如果你有一个列表列表,你只需要使用 ...

import numpy as np
...
npa = np.asarray(someListOfLists, dtype=np.float32)

per this LINK in the scipy / numpy documentation.根据 scipy / numpy 文档中的此链接 You just needed to define dtype inside the call to asarray.您只需要在对 asarray 的调用中定义 dtype。

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

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