I have a numpy array of numpy arrays, and I want to convert it to a 2d numpy array. For example
#Init Sub Arrays
a = np.array([1,2])
b = np.array([3,4,5])
c = np.array([2,1])
d = np.array([5,4,3])
#Combine Sub Arrays
e = np.array([[a,b],[c,d]])
#Sample Sub Arrays
f = e[:,0]
#Attempt to convert sub np arrays to 2d np array
g = np.array(f)
expected = np.array([[1,2],[2,1]])
print("Actual 1: ",f)
print("Actual 2: ",g)
print("Expected:", expected)
print("Actual 1: ",np.ravel(f))
print("Actual 2: ",np.ravel(g))
print("Expected: ",np.ravel(expected))
Output:
Actual 1: [array([1, 2]) array([2, 1])]
Actual 2: [array([1, 2]) array([2, 1])]
Expected: [[1 2]
[2 1]]
Actual 1: [array([1, 2]) array([2, 1])]
Actual 2: [array([1, 2]) array([2, 1])]
Expected: [1 2 2 1]
I understand that the array is initialized the way it is because numpy doesn't support arrays of different length on the same dimension, but I want to know how I can convert a valid sample of a "hack" numpy array to a "valid" numpy array