Source From Here
Question
Are there good ways to "expand" a numpy ndarray? Say I have an ndarray like this:
And I want each row to contains more elements by filling zeros:
I know there must be some brute-force ways to do so (
say construct a bigger array with zeros then copy elements from old smaller arrays), just wondering are there graceful ways to do so. Tried numpy.reshape but didn't work:
With output as:
How-To
There are the index tricks r_ and c_.
If this is performance critical code, you might prefer to use the equivalent np.concatenate rather than the index tricks.
There are also np.resize and np.ndarray.resize, but they have some limitations (due to the way numpy lays out data in memory) so read the docstring on those ones. You will probably find that simply concatenating is better.
Question
Are there good ways to "expand" a numpy ndarray? Say I have an ndarray like this:
- [[1 2]
- [3 4]]
- [[1 2 0 0 0]
- [3 4 0 0 0]]
- import numpy as np
- a = np.array([[1, 2], [3, 4]])
- np.reshape(a, (2, 5))
How-To
There are the index tricks r_ and c_.
If this is performance critical code, you might prefer to use the equivalent np.concatenate rather than the index tricks.
There are also np.resize and np.ndarray.resize, but they have some limitations (due to the way numpy lays out data in memory) so read the docstring on those ones. You will probably find that simply concatenating is better.
沒有留言:
張貼留言