-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: Generalize split function to split matrix into sub-matrices. #15810
base: main
Are you sure you want to change the base?
Conversation
Closes numpy#15380 Co-authored-by: KoningR <r.m.koning@student.tudelft.nl> Co-authored-by: erwinvanthiel <e.l.vanthiel@student.tudelft.nl>
Thanks for the contribution. This is a good start, but in the context of the "0, 1, infinity rule", I don't think we should take this patch in its current form. If possible, do you think you could implement the suggestion in #15380 (comment)? Modifying one of your existing examples, that would give: >>> xs = np.array_split(x, [3, 3], axis=(0, 1))
>>> xs.shape
(3, 3)
>>> xs
array([[array([[0, 1], [4, 5]),
array([[2], [6]]),
array([[3], [7]])],
[array([[8, 9]]),
array([[10]]),
array([[11]])],
[array([[12, 13]]),
array([[14]]),
array([[15]])]], dtype=object) |
…ze-split-for-matrices
Closes numpy#15380 Co-authored-by: KoningR <r.m.koning@student.tudelft.nl> Co-authored-by: erwinvanthiel <e.l.vanthiel@student.tudelft.nl>
…ate. Co-authored-by: KoningR <r.m.koning@student.tudelft.nl> Co-authored-by: erwinvanthiel <e.l.vanthiel@student.tudelft.nl>
numpy/lib/shape_base.py
Outdated
raise ValueError('Axis is a tuple, so indices_or_sections must be of the same length as axis.') | ||
|
||
# Verify there is an index or section for every axis. | ||
if len(indices_or_sections) is not len(axis): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if len(indices_or_sections) is not len(axis): | |
if len(indices_or_sections) != len(axis): |
Comparing integers with is
is not guaranteed to work
Also needs a release note if it goes in. |
…ze-split-for-matrices
…n operator in N-dimensional split functionality. Co-authored-by: KoningR <r.m.koning@student.tudelft.nl> Co-authored-by: erwinvanthiel <e.l.vanthiel@student.tudelft.nl>
numpy/lib/shape_base.py
Outdated
>>> np.array_split(x, 3) | ||
[array([0., 1., 2.]), array([3., 4.]), array([5., 6.])] | ||
|
||
>>> x = np.reshape(np.arange(16), (4, 4)) | ||
>>> np.array_split(x, [3, 3], (0, 1)) | ||
[array([[0, 1], | ||
[4, 5]]), | ||
array([[2], [6]]), | ||
array([[3], [7]]), | ||
array([[8, 9]]), | ||
array([[10]]), | ||
array([[11]]), | ||
array([[12, 13]]), | ||
array([[14]]), | ||
array([[15]])] | ||
|
||
>>> x = np.reshape(np.arange(8), (2, 2, 2)) | ||
>>> np.array_split(x, [2, 2, 2], (0, 1, 2)) | ||
[array([[[0]]]), | ||
array([[[1]]]), | ||
array([[[2]]]), | ||
array([[[3]]]), | ||
array([[[4]]]), | ||
array([[[5]]]), | ||
array([[[6]]]), | ||
array([[[7]]])] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting is off here:
>>> np.array_split(x, 3) | |
[array([0., 1., 2.]), array([3., 4.]), array([5., 6.])] | |
>>> x = np.reshape(np.arange(16), (4, 4)) | |
>>> np.array_split(x, [3, 3], (0, 1)) | |
[array([[0, 1], | |
[4, 5]]), | |
array([[2], [6]]), | |
array([[3], [7]]), | |
array([[8, 9]]), | |
array([[10]]), | |
array([[11]]), | |
array([[12, 13]]), | |
array([[14]]), | |
array([[15]])] | |
>>> x = np.reshape(np.arange(8), (2, 2, 2)) | |
>>> np.array_split(x, [2, 2, 2], (0, 1, 2)) | |
[array([[[0]]]), | |
array([[[1]]]), | |
array([[[2]]]), | |
array([[[3]]]), | |
array([[[4]]]), | |
array([[[5]]]), | |
array([[[6]]]), | |
array([[[7]]])] | |
>>> np.array_split(x, 3) | |
[array([0., 1., 2.]), array([3., 4.]), array([5., 6.])] | |
>>> x = np.reshape(np.arange(16), (4, 4)) | |
>>> np.array_split(x, [3, 3], (0, 1)) | |
[array([[0, 1], | |
[4, 5]]), | |
array([[2], [6]]), | |
array([[3], [7]]), | |
array([[8, 9]]), | |
array([[10]]), | |
array([[11]]), | |
array([[12, 13]]), | |
array([[14]]), | |
array([[15]])] | |
>>> x = np.reshape(np.arange(8), (2, 2, 2)) | |
>>> np.array_split(x, [2, 2, 2], (0, 1, 2)) | |
[array([[[0]]]), | |
array([[[1]]]), | |
array([[[2]]]), | |
array([[[3]]]), | |
array([[[4]]]), | |
array([[[5]]]), | |
array([[[6]]]), | |
array([[[7]]])] |
numpy/lib/shape_base.py
Outdated
>>> np.split(x, [4, 2], (0, 1)) | ||
[array([[0, 1]]), | ||
array([[2, 3]]), | ||
array([[4, 5]]), | ||
array([[6, 7]]), | ||
array([[8, 9]]), | ||
array([[10, 11]]), | ||
array([[12, 13]]), | ||
array([[14, 15]])] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
>>> np.split(x, [4, 2], (0, 1)) | |
[array([[0, 1]]), | |
array([[2, 3]]), | |
array([[4, 5]]), | |
array([[6, 7]]), | |
array([[8, 9]]), | |
array([[10, 11]]), | |
array([[12, 13]]), | |
array([[14, 15]])] | |
[array([[0, 1]]), | |
array([[2, 3]]), | |
array([[4, 5]]), | |
array([[6, 7]]), | |
array([[8, 9]]), | |
array([[10, 11]]), | |
array([[12, 13]]), | |
array([[14, 15]])] |
numpy/lib/tests/test_shape_base.py
Outdated
[[0, 1], | ||
[4, 5]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[[0, 1], | |
[4, 5]], | |
[[0, 1], | |
[4, 5]], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or
[[0, 1], | |
[4, 5]], | |
[[0, 1], [4, 5]], |
It's my opinion that The advantage would be that things like For compatilibity, you'd only return an object array if a tuple is passed for the number of splits. |
Hi,
In this case, The elements in What would be the best way to convert this to an N-dimensional array? |
Just a tip - use triple backticks for multi-line codeblocks in comments. |
One way to transform that would be: # as you have it now
res = [
np.array([[0, 1], [4, 5]]),
np.array([[2], [6]]),
np.array([[3], [7]]),
np.array([[8, 9]]),
np.array([[10]]),
np.array([[11]]),
np.array([[12, 13]]),
np.array([[14]]),
np.array([[15]]),
]
# convert list to object array.
# It's possible you can assemble it this way in the first place,
# but doesn't really matter if you do it this way
res_arr = np.empty(len(res), dtype=object)
for i, subarr in enumerate(res):
res_arr[i] = subarr
# I assume you already have a way to compute this shape
return res.reshape((3, 3)) |
…ze-split-for-matrices
Co-authored-by: KoningR <r.m.koning@student.tudelft.nl> Co-authored-by: erwinvanthiel <e.l.vanthiel@student.tudelft.nl>
Co-authored-by: KoningR <r.m.koning@student.tudelft.nl> Co-authored-by: erwinvanthiel <e.l.vanthiel@student.tudelft.nl>
…ze-split-for-matrices
Hi, The string representation of the object array:
|
Unfortunately you're running up against #15869. |
Hi. It appears PR #15997 was delayed because it might break doctests already in the code base. However, we are now dependent on this PR. If doctests will be refactored sometime in the future anyhow, can we push our doctests such that they pass the build, and be done with this issue? |
The PR you reference is now merged, so that question is moot :) |
instead of splitting in parts, just like a one-dimensional call to `numpy.split` or `numpy.array_split`. | ||
|
||
To use this new functionality, write ``np.split(array, [4, 2], (0, 1))`` or ``np.array_split(array, [4, 2], (0, 1))`` | ||
to split ``array`` in ``4`` parts over axis ``0`` and then in ``2`` parts over axis ``1``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs some rewrapping to fit into 80 columns
Edit: ... and in the rest of the PR as well
``axis``. Also note that these elements can also be lists themselves, to allow for splitting on indices | ||
instead of splitting in parts, just like a one-dimensional call to `numpy.split` or `numpy.array_split`. | ||
|
||
To use this new functionality, write ``np.split(array, [4, 2], (0, 1))`` or ``np.array_split(array, [4, 2], (0, 1))`` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably clearer to explain this with a kwarg.
To use this new functionality, write ``np.split(array, [4, 2], (0, 1))`` or ``np.array_split(array, [4, 2], (0, 1))`` | |
To use this new functionality, write ``np.split(array, [4, 2], axis=(0, 1))`` or ``np.array_split(array, [4, 2], axis=(0, 1))`` |
…-split-for-matrices � Conflicts: � numpy/lib/shape_base.py
…-split-for-matrices
>>> np.array_split(x, 3) | ||
[array([0., 1., 2.]), array([3., 4.]), array([5., 6.])] | ||
>>> x = np.reshape(np.arange(16), (4, 4)) | ||
>>> np.array_split(x, [3, 3], (0, 1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we should allow
>>> np.array_split(x, [3, 3], (0, 1)) | |
>>> np.array_split(x, 3, (0, 1)) |
to mean the same thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea and have added the functionality. Based on your proposed test_zero_dimensional_split
, I assumed that ret = split(matrix, 3, axis=())
should operate the same as ret = split(matrix, [], axis=())
and therefore do nothing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, "split into three parts on no axes" seems like a legal instruction to do nothing, and an error would just be annoying.
…-split-for-matrices
…-split-for-matrices
Hi. I think this PR is done. Would anyone mind having a look at it? |
Co-authored-by: Eric Wieser <wieser.eric@gmail.com>
We discussed it today, we think this can be pushed forward API wise. We haven't reviewed the implementation though, so we will need to find/prioritize some review time actually push it forward. |
Closes #15380
Co-authored-by: KoningR r.m.koning@student.tudelft.nl
Co-authored-by: erwinvanthiel e.l.vanthiel@student.tudelft.nl