A Rob4 A Rob4 - 3 months ago 6
Python Question

Trying to flatten array in numpy

I'm new to numpy and trying to flatten a 1000,1000 array created from a pandas dataframe. the code i've used is:

lidor_array=lidor_df.values
print(lidor_array.shape)
lidor_array.flatten()
print(lidor_array.shape)


the shapes are output as (1000,1000) for both the pre and post flattened array. what am i missing?

many thanks for your help

Answer

flatten is not performed in-place. It returns a copy:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flatten.html

You could either do:

lidor_array.flatten().shape

or

lidor_array_flat = arr.flatten()
print lidor_array_flat.shape