Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting list to numpy array

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
like image 281
Priya Narayanan Avatar asked Nov 10 '14 18:11

Priya Narayanan


People also ask

Can we convert list to NumPy array?

The similarity between an array and a list is that the elements of both array and a list can be identified by its index value. In Python lists can be converted to arrays by using two methods from the NumPy library: Using numpy. array()

How do I convert a list to an array in Python?

To convert a list to array in Python, use the np. array() method. The np. array() is a numpy library function that takes a list as an argument and returns an array containing all the list elements.


Video Answer


1 Answers

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.

like image 115
Thom Ives Avatar answered Oct 16 '22 06:10

Thom Ives