Reputation: 55
I have a numpy 2D-array of string with shape (3,2)
:
ar_2d = array([['123', '456'],
['789', '0ab'],
['cde', 'fgh']],
dtype='<U3')
To make it easier, I am sure the length of each string is equal.
And I have a function, i.e. namely split()
, to make string '123'
to python list ['1','2','3']
Now I would like to produce a 3D-array with '123'
to an array array(['1', '2', '3'])
and finally I can get a 3D-array with shape (3,2,3)
:
ar_3d = array([[['1', '2', '3'],
['4', '5', '6']],
[['7', '8', '9'],
['0', 'a', 'b']],
[['c', 'd', 'e'],
['f', 'g', 'h']]],
dtype='<U1')
I have an idea that splitting the string to list first and write to file with numpy's format. Then, I shall read the array from the file.
If the elements are integer numbers, would it be easier? i.e. number 123
to list [1,2,3]
So here is the problem, is there an elegant method to do the convertion?
Thanks in advance!
Upvotes: 2
Views: 814
Reputation: 221664
Use ndarray.view
to view as U1
and then reshape into 3D
-
In [15]: a
Out[15]:
array([['123', '456'],
['789', '0ab'],
['cde', 'fgh']],
dtype='<U3')
In [16]: a.view('U1').reshape(a.shape + (-1,))
Out[16]:
array([[['1', '2', '3'],
['4', '5', '6']],
[['7', '8', '9'],
['0', 'a', 'b']],
[['c', 'd', 'e'],
['f', 'g', 'h']]],
dtype='<U1')
The output would simply be a view of the input and hence this would be memory-efficient. As such, the runtime would be constant (irrespective of array shape) -
In [20]: %timeit a.view('U1').reshape(a.shape + (-1,))
1000000 loops, best of 3: 828 ns per loop
In [21]: a_big = np.tile(a,10000)
In [22]: %timeit a_big.view('U1').reshape(a_big.shape + (-1,))
1000000 loops, best of 3: 851 ns per loop
Upvotes: 2