correct and efficient way to flatten array in numpy in python? [duplicate]

You might need to check out numpy.flatten and numpy.ravel, both return a 1-d array from an n-d array.

Furthermore, if you’re not going to modify the returned 1-d array, I suggest you use numpy.ravel, since it doesn’t make a copy of the array, but just return a view of the array, which is much faster than numpy.flatten.

>>>a = np.arange(10000).reshape((100,100))

>>>%timeit a.flatten()
100000 loops, best of 3: 4.02 µs per loop

>>>%timeit a.ravel()
1000000 loops, best of 3: 412 ns per loop

Also check out this post.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)