'Numpy flatten a nested array using concatenate

I have a numpy array with subarrays of different shapes. I was trying to use an iterator to flatten them into a 1D array. Below is the code:

import numpy as np
a=np.array([np.random.rand(1,2),np.random.rand(2,2),np.random.rand(1,4)],dtype=object)
b=np.concatenate(x.ravel for x in a)

This returns an error:

TypeError: The first input argument needs to be a sequence

I am not quite sure what I am doing incorrectly. It works fine when I create a for loop with the same logic and keep concatenating my array recursively. Any help appreciated.

The goal is to flatten the array into a 1D array. (Note that hstack doesn't work because the arrays are of different shapes. flatten doesn't work because it is already a 1D array (of arrays).)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source