Maxxx
Maxxx

Reputation: 3768

Python numpy reshape

I have a variable of z storing features of length and height of image files where z is

z = [length, height]

and i want to change these dimension to just:

z = [area] where area = length * height

I tried using the numpy reshape function as follow:

area = z.shape[0] * z.shape[1] #length * height
z = z.reshape(-1) #was trying to reduce to just z = [area]

but it seemed like I'm not using the reshape function correctly. Could anyone help me out?

Upvotes: 0

Views: 412

Answers (1)

Jurgen Strydom
Jurgen Strydom

Reputation: 3930

Simple example of how to use reshape:

import numpy as np

a = np.random.randint(0,10,(10,10))
b = np.reshape(a, (100,))
print(b)

For your case it will be:

print(a.shape)  # prints (length,height)
b = np.reshape(a, (length * height,))
print(b.shape)  # prints (length * height,)

To perform a reshape in place you can also use:

a.shape = ((100,))

Upvotes: 1

Related Questions