IndexError when extracting noncontiguous columns and rows from matrix
See original GitHub issueTest case:
>>> A=np.random.rand(4,4)
>>> A[(0,3),(0,2,3)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (3,)
However, A[(0,3),(0,2,3)]
is a question with a very definite answer which can be obtained by calling
>>> A[(0,3),:][:,(0,2,3)]
I can’t think of any situation in which this might be ambiguous or indefinite, so if in fact none exists, I propose that numpy should return this result instead of an exception.
Thank you for your work!
Issue Analytics
- State:
- Created 9 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
How to extract non-consecutive rows and columns of a matrix?
For example, in this matrix how do i extract rows 1,2 and 4 with columns 1, 2 and 4? import numpy as np...
Read more >Indexing on ndarrays — NumPy v1.25.dev0 Manual
It results in the construction of a new array where each value of the index array selects one row from the array being...
Read more >First introduction to NumPy — SciPyTutorial 0.0.4 documentation
The term slicing is used for extracting whole rows, columns or other rectangular (hypercubic) subregions from an arbitrary dimensional array.
Read more >How to display two non-consecutive column vectors
Indices can be non-consecutive numbers. Try extracting the first, third, and sixth elements of density. Theme.
Read more >Python IndexError: List Index Out of Range [Easy Fix] - Finxter
A common cause of the error is trying to access indices 1, 2, ..., n instead of using the correct indices 0,1, ...,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There’s an
ix_
function that does something like this.Yes, this also confused me when I started using Numpy. You just have to memorize that “fancy” indexing uses a totally different rule to combine multiple indexes than slicing does. It’s much more powerful this way, granted, and it’s far too late to change now. But it is confusing.
To get what you originally wanted, you can make it so the indices do broadcast broadcast against each other:
arr[ np.reshape([1, 3, 5], (3, 1)), np.reshape([2, 4, 6], (1, 3)) ] On Feb 16, 2015 7:45 AM, “Andrea Azzini” notifications@github.com wrote: