question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IndexError when extracting noncontiguous columns and rows from matrix

See original GitHub issue

Test 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:closed
  • Created 9 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
argriffingcommented, Feb 16, 2015

There’s an ix_ function that does something like this.

>>> import numpy as np
>>> a = np.random.rand(4,4)
>>> a[(0,3),:][:,(0,2,3)]
array([[ 0.84060636,  0.69065912,  0.0026522 ],
       [ 0.61781349,  0.98709272,  0.97869428]])
>>> a[np.ix_((0,3),(0,2,3))]
array([[ 0.84060636,  0.69065912,  0.0026522 ],
       [ 0.61781349,  0.98709272,  0.97869428]])
0reactions
njsmithcommented, Feb 16, 2015

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:

Oh ok, I didn’t know that. Neat. I still feel this is inconsistent with slicing (which uses the same “operator” to extract a submatrix), however there’s absolutely no point in changing anything, and I may be thinking too Matlab-y. Please close this bug, and thanks for your time.

— Reply to this email directly or view it on GitHub https://github.com/numpy/numpy/issues/5574#issuecomment-74527617.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found