Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
2  
You have a list of lists, where not all lists have the same amount of entries. –  Daniel Nov 10 '14 at 18:28
    
ok. What is the solution? New to python so please bear with me –  Priya Narayanan Nov 10 '14 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). –  ha9u63ar Nov 10 '14 at 19:00
    
possible duplicate of ValueError: setting an array element with a sequence –  ha9u63ar Nov 10 '14 at 19:01
    
The dtype of np.array(x) uint8. I think it is a list of lists –  Priya Narayanan Nov 10 '14 at 19:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.