Update 5057, ENH: Add a block function for creating stacked block arrays.#7768
Update 5057, ENH: Add a block function for creating stacked block arrays.#7768charris wants to merge 1 commit intonumpy:masterfrom
Conversation
numpy/core/shape_base.py
Outdated
|
This function is very nice. However, it only appears to work in 2D. I realize that making it work in ND will require a lot more work, but it does seem like a good thing to have. Perhaps a recursive solution would work? |
numpy/core/shape_base.py
Outdated
There was a problem hiding this comment.
This should probably be result.append(block(row)) to support ND. If it is that simple, the docs, examples and tests need to be updated.
There was a problem hiding this comment.
Just wanted to bump this implementation suggetsion.
|
One test that I think is missing is for mismatched dtypes. Looping over all the possible combinations is perhaps overkill, but at least a check that |
numpy/core/shape_base.py
Outdated
There was a problem hiding this comment.
I've decided to omit it ;)
|
Besides the lack of support for >2D, I like this PR. |
|
☔ The latest upstream changes (presumably #7268) made this pull request unmergeable. Please resolve the merge conflicts. |
Add a block function to the current stacking functions vstack, hstack, stack. It is similar to Matlab's square bracket stacking functionality for block matrices.
|
I've made a couple of changes. Thinking about it, I don't think we should allow |
|
Keeping the outer brackets also makes it look more like where Compare that to So the last two lines of each case look more alike. I'll also admit that |
|
Sorry to be late to this: my sense would be to, initially at least, limit the possible ways to do the same thing. I think visually it is much clearer if any times arrays are placed next to each other, they are in fact put next to each other in a row in the final array, never below each other in a column. So, I would Here, I like @ahaldane's analogy with the |
|
By the way, the previous mailing list discussion is at http://thread.gmane.org/gmane.comp.python.numeric.general/58748/ There it still looks up in the air whether to include the outer brackets, but if we keep them it would mean |
|
☔ The latest upstream changes (presumably #7985) made this pull request unmergeable. Please resolve the merge conflicts. |
|
One more request I would like to propose (possibly for another PR), is to add this to masked arrays as well. |
|
Will branch 1.12.x soon, so pushing this off to 1.13. |
| dstack : Stack arrays in sequence depth wise (along third dimension). | ||
| concatenate : Join a sequence of arrays along an existing axis. | ||
| vsplit : Split array into a list of multiple sub-arrays vertically. | ||
| block : Assemble arrays from blocks. |
There was a problem hiding this comment.
I think that this phrasing of the description is by far the best one. "Create block arrays" does not really make much sense without going into a lot more detail.
| dstack : Stack arrays in sequence depth wise (along third axis). | ||
| concatenate : Join a sequence of arrays along an existing axis. | ||
| hsplit : Split array along second axis. | ||
| block : Create block arrays. |
There was a problem hiding this comment.
Change to Assemble arrays from blocks.
| -------- | ||
| concatenate : Join a sequence of arrays along an existing axis. | ||
| split : Split array into a list of multiple sub-arrays of equal size. | ||
| block : Create block arrays. |
There was a problem hiding this comment.
Change to Assemble arrays from blocks.
|
|
||
| def block(*arrays): | ||
| """ | ||
| Create a block array consisting of other arrays. |
There was a problem hiding this comment.
Change to Assemble arrays from blocks consisting of other arrays. Without further explanation, the term "block array" does not have an unambiguous meaning.
| Returns | ||
| ------- | ||
| blocked : ndarray | ||
| The 2-D array assembled from the given blocks. |
There was a problem hiding this comment.
Hopefully this can be extended to N-D.
|
Seems like this should be done in two passes, so as to pre-allocate the destination array, rather than allocating again for each dimension |
All these points are addressed in #8886 |
|
Sorry to interrupt the discussion here, but am curious to see if this might handle a use case I have. A snippet of an example of where I might like to use DetailsIn [1]: a
Out[1]:
array([[-10, -2, 1],
[ 4, -7, 6]])
In [2]: b
Out[2]:
array([[ 5, -2, 3],
[-3, -8, 2],
[-7, 1, -2],
[-9, -2, -3]])
In [3]: c
Out[3]:
array([[-10, 8, 4, -6, -1],
[ 1, -1, -3, -2, 6]])
In [4]: d
Out[4]:
array([[ 1, -2, 0, 6, 1],
[ 9, -1, 9, 5, -7],
[-7, -7, 8, -1, 0],
[-8, 0, 7, 3, 9]])
In [5]: np.concatenate([np.concatenate([a, c], axis=1), np.concatenate([b, d], axis=1)], axis=0)
Out[5]:
array([[-10, -2, 1, -10, 8, 4, -6, -1],
[ 4, -7, 6, 1, -1, -3, -2, 6],
[ 5, -2, 3, 1, -2, 0, 6, 1],
[ -3, -8, 2, 9, -1, 9, 5, -7],
[ -7, 1, -2, -7, -7, 8, -1, 0],
[ -9, -2, -3, -8, 0, 7, 3, 9]])
In[6]: np.block([[a, c], [b, d]])
Out[6]:
array([[-10, -2, 1, -10, 8, 4, -6, -1],
[ 4, -7, 6, 1, -1, -3, -2, 6],
[ 5, -2, 3, 1, -2, 0, 6, 1],
[ -3, -8, 2, 9, -1, 9, 5, -7],
[ -7, 1, -2, -7, -7, 8, -1, 0],
[ -9, -2, -3, -8, 0, 7, 3, 9]]) |
|
@jakirkham: I'm afraid you're out of luck there - dimensions are concatenated from right to left, so Of course, if you want to concatenate from left to right, you can just transpose: You could also write it as, without much benefit over what you already have: |
|
Oops, sorry, I think I had the order flipped in my example. |
|
Yes, what you now have would work just fine |
|
Awesome, thanks. Sorry for the confusion. |
|
@eric-wieser I should close this, right? |
As requested in numpy#7768
Based on feedback in numpy#7768
Updates #5057.
Add a block function to the current stacking functions vstack, hstack,
stack. It is similar to Matlab's square bracket stacking functionality
for block matrices.