114

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
9
  • 6
    You have a list of lists, where not all lists have the same amount of entries. Commented Nov 10, 2014 at 18:28
  • ok. What is the solution? New to python so please bear with me Commented Nov 10, 2014 at 18:59
  • Check the type after your np.array(x) is successful. If it is float64 which is the default - then you are simply trying to assign a type which is not suitable for your list (or list of lists). Commented Nov 10, 2014 at 19:00
  • possible duplicate of ValueError: setting an array element with a sequence Commented Nov 10, 2014 at 19:01
  • The dtype of np.array(x) uint8. I think it is a list of lists Commented Nov 10, 2014 at 19:02

2 Answers 2

163

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.

Sign up to request clarification or add additional context in comments.

Comments

8

If you're converting a list into an array, you'll need to make a new copy anyway, so

arr = np.array(my_list, dtype='float32')

also works.

One use case is when my_list is not actually a list but some other list-like object; in which case explicitly casting to list beforehand might be helpful.

arr = np.array(list(my_list), dtype='float32')

For example,

my_list = pd.Series([[1], [2], [3]]).values

np.array(my_list)             # jagged array; not OK
np.array(list(my_list))       # OK
np.array(my_list.tolist())    # OK


my_list = {'a': [1], 'b': [2]}.values()
np.array(my_list)             # jagged array; not OK
np.array(list(my_list))       # OK

To cast nested list into an array, the shapes of the sublists must match. If they don't maybe you want to concatenate the sublists along some axis. Try np.concatenate/np.r_/np.c_ etc. instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.