将 list 转换为 numpy 数组

我已经设法加载图像在一个文件夹使用命令行 sklearn: load_sample_images()

我现在想把它转换成 numpy.ndarray格式与 float32数据类型

我能够将它转换为 np.ndarray使用: np.array(X),但是 np.array(X, dtype=np.float32)np.asarray(X).astype('float32')给我的错误:

ValueError: setting an array element with a sequence.

有办法解决这个问题吗?

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
263055 次浏览

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. You just needed to define dtype inside the call to asarray.