NumPy Fancy Array Indexing: Difference between revisions
Jump to navigation
Jump to search
(3 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
Fancy indexing is a term adopted by NumPy to describe indexing using integer arrays. | Fancy indexing is a term adopted by NumPy to describe indexing using integer arrays. | ||
In case of a bi-dimensional array, passing an unidimensional list containing row indices returns a bi-dimensional array where only the rows whose indices were provided are present, in the order in which the indices were specified in the selection array. | |||
<syntaxhighlight lang='py'> | |||
a = np.array([[10, 10], [20, 20], [30, 30], [40, 40]]) | |||
</syntaxhighlight> | |||
<font size=-2> | |||
array([[10, 10], | |||
[20, 20], | |||
[30, 30], | |||
[40, 40]]) | |||
</font> | |||
<syntaxhighlight lang='py'> | |||
a[[3, 0]] | |||
</syntaxhighlight> | |||
<font size=-2> | |||
array([[40, 40], | |||
[10, 10]]) | |||
</font> | |||
Using negative indices selects rows from the end. | |||
Fancy indexing, unlike slicing, copies the data into a new array when assigning the result to a new variable. | |||
<font color=darkkhaki>TODO: https://learning.oreilly.com/library/view/python-for-data/9781098104023/ch04.html#:-:text=Fancy%20Indexing</font> | <font color=darkkhaki>TODO: https://learning.oreilly.com/library/view/python-for-data/9781098104023/ch04.html#:-:text=Fancy%20Indexing</font> |
Latest revision as of 17:17, 21 May 2024
Internal
Overview
Fancy indexing is a term adopted by NumPy to describe indexing using integer arrays.
In case of a bi-dimensional array, passing an unidimensional list containing row indices returns a bi-dimensional array where only the rows whose indices were provided are present, in the order in which the indices were specified in the selection array.
a = np.array([[10, 10], [20, 20], [30, 30], [40, 40]])
array([[10, 10], [20, 20], [30, 30], [40, 40]])
a[[3, 0]]
array([[40, 40], [10, 10]])
Using negative indices selects rows from the end.
Fancy indexing, unlike slicing, copies the data into a new array when assigning the result to a new variable.