Reputation: 105
Currently, the array I got is
arr = array([array([ 2, 7, 8, 12, 14]), array([ 3, 4, 5, 6, 9, 10]),
array([0, 1]), array([11, 13])], dtype=object)
How can I convert it into array([[ 2, 7, 8, 12, 14], [ 3, 4, 5, 6, 9, 10], [0, 1], [11, 13]])
?
I tried arr.astype(np.int)
, but failed
Upvotes: 1
Views: 8384
Reputation: 164623
The dtype
for an array of arrays will always be object
. This is unavoidable because with NumPy only non-jagged n-dimensional arrays can be held in a contiguous memory block.
Notice your constituent arrays are already of int
dtype:
arr[0].dtype # dtype('int32')
Notice also your logic will work for a non-jagged array of arrays:
arr = np.array([np.array([ 2, 7, 8]),
np.array([ 3, 4, 5])], dtype=object)
arr = arr.astype(int)
arr.dtype # dtype('int32')
In fact, in this case, the array of arrays is collapsed into a single array:
print(arr)
array([[2, 7, 8],
[3, 4, 5]])
For computations with a jagged array of arrays you may see some performance advantages relative to a list of lists, but the benefit may be limited. See also How do I stack vectors of different lengths in NumPy?
Upvotes: 2