将列表转换为 numpy 数组 [英] Converting list to numpy array
问题描述
我设法使用命令行 sklearn 加载文件夹中的图像:load_sample_images()
我现在想将其转换为具有 float32
数据类型的 numpy.ndarray
格式
我能够使用:np.array(X)
将它转换为 np.ndarray
,但是 np.array(X, dtype=np.float32)
和 np.asarray(X).astype('float32')
给我错误:
ValueError: 使用序列设置数组元素.
有没有办法解决这个问题?
from sklearn_theano.datasets import load_sample_images将 numpy 导入为 npkinect_images = load_sample_images()X = kinect_images.imagesX_new = np.array(X) # 有效X_new = np.array(X[1], dtype=np.float32) # 有效X_new = np.array(X, dtype=np.float32) # 不起作用
如果你有一个列表列表,你只需要使用...
将 numpy 导入为 np...npa = np.asarray(someListOfLists, dtype=np.float32)
根据这个LINK 在 scipy/numpy 文档中.您只需要在对 asarray 的调用中定义 dtype.
I have managed to load images in a folder using the command line sklearn: load_sample_images()
I would now like to convert it to a numpy.ndarray
format with float32
datatype
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:
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. You just needed to define dtype inside the call to asarray.
这篇关于将列表转换为 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!