Convert list to numpy array

I was able to upload images to a folder using the sklearn command line: load_sample_images()

Now I would like to convert it to numpy.ndarray format with float32 datatype

I managed 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 an error:

 ValueError: setting an array element with a sequence. 

Is there any way 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 
+6
source share
1 answer

If you have a list of lists, you only need to use ...

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

for LINK in the scipy / numpy documentation. You just needed to define the dtype inside the asarray call.

+3
source

Source: https://habr.com/ru/post/977990/


All Articles