3

I'm performing data mining on images. Each pixel is treated as a data point. The image is read as follows:

im=Image.open('lena.bmp')
im=numpy.array(im)
print im.shape

Depending on whether the image is color or grayscale, im.shape is either (10,10, 3) or (10,10,1)

After that, the image is flattened to a feature matrix as follows:

if (10,10,3), then --->(100,3)

if (10,10,1), then --->(100,1)

How do I write a polymorphic function for this? My current approach is:

obs=reshape(im,(im.shape[0]*im.shape[1],1, im.size/(im.shape[0]*im.shape[1])))
2
  • 1
    Don't you have an extra 1 inside your shape tuple? Commented Feb 13, 2013 at 21:52
  • that's true. I'm having obs=reshape(im,(im.shape[0]*im.shape[1], im.size/(im.shape[0]*im.shape[1]))) Commented Feb 13, 2013 at 23:49

1 Answer 1

3

You can do:

obs = np.reshape(im, (-1, im.shape[-1]))
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please elaborate this code? It works perfectly. shape[-1] is the number of dimensions, how about -1 before it?
@DzungNguyen -1 is replaced internally by numpy to whatever value is needed to fit the size of the array.

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.