I was doing a little experimentation with 2D lists and numpy arrays. From this, I\'ve raised 3 questions I\'m quite curious to know the answer for.
First, I initiali
Which
__xx__
method has numpy overridden/defined to handle fancy indexing?
__getitem__
for retrieval, __setitem__
for assignment. It'd be __delitem__
for deletion, except that NumPy arrays don't support deletion.
(It's all written in C, though, so what they implemented at C level was mp_subscript
and mp_ass_subscript
, and __getitem__
and __setitem__
wrappers were provided by PyType_Ready
. __delitem__
too, even though deletion is unsupported, because __setitem__
and __delitem__
both map to mp_ass_subscript
at C level.)
Why don't python lists natively support fancy indexing?
Python lists are fundamentally 1-dimensional structures, while NumPy arrays are arbitrary-dimensional. Multidimensional indexing only makes sense for multidimensional data structures.
You can have a list with lists as elements, like [[1, 2], [3, 4]]
, but the list doesn't know or care about the structure of its elements. Making lists support l[:, 2]
indexing would require the list to be aware of multidimensional structure in a way that lists aren't designed to be. It would also add a lot of complexity, a lot of error handling, and a lot of extra design decisions - how deep a copy should l[:, :]
be? What happens if the structure is ragged, or inconsistently nested? Should multidimensional indexing recurse into non-list elements? What would del l[1:3, 1:3]
do?
I've seen the NumPy indexing implementation, and it's longer than the entire implementation of lists. Here's part of it. It's not worth doing that to lists when NumPy arrays satisfy all the really compelling use cases you'd need it for.
Why is numpy's fancy indexing so slow on python2? Is it because I don't have native BLAS support for numpy in this version?
NumPy indexing isn't a BLAS operation, so that's not it. I can't reproduce such dramatic timing differences, and the differences I do see look like minor Python 3 optimizations, maybe slightly more efficient allocation of tuples or slices. What you're seeing is probably due to NumPy version differences.