Dzung Nguyen
Dzung Nguyen

Reputation: 3944

Flattening of numpy array

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])))

Upvotes: 3

Views: 1854

Answers (1)

Jaime
Jaime

Reputation: 67417

You can do:

obs = np.reshape(im, (-1, im.shape[-1]))

Upvotes: 3

Related Questions