Skip to content

Commit 02753aa

Browse files
committed
DOC: Added and modified examples
Also fixed minor typos and styling errors
1 parent ff8113d commit 02753aa

File tree

3 files changed

+89
-40
lines changed

3 files changed

+89
-40
lines changed

doc/source/user/basics.indexing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ For this reason, it is possible to use the output from the
855855
:meth:`np.nonzero() <ndarray.nonzero>` function directly as an index since
856856
it always returns a tuple of index arrays.
857857

858-
Because the special treatment of tuples, they are not automatically
858+
Because of the special treatment of tuples, they are not automatically
859859
converted to an array as a list would be. As an example: ::
860860

861861
>>> z[[1, 1, 1, 1]] # produces a large array

doc/source/user/how-to-index.rst

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ Variables can also be used to index::
4949
>>> a[y, :, y+3]
5050
array([ 3, 8, 13])
5151

52+
Refer to :ref:`dealing-with-variable-indices` to see how to use
53+
:term:`python:slice` and :py:data:`Ellipsis` in your index variables.
54+
5255
Index along a specific axis
5356
---------------------------
5457

@@ -81,63 +84,109 @@ Use :ref:`dimensional-indexing-tools` to avoid shape mismatches::
8184
>>> column_indices = [[1, 3], [0, 2], [2, 2]]
8285
>>> np.arange(arr.shape[0])
8386
array([0, 1, 2])
84-
85-
Adding the second dimension to index the second dimension of the array::
86-
87-
>>> shape_indices = np.arange(arr.shape[0])[:, np.newaxis]
88-
>>> shape_indices
87+
>>> row_indices = np.arange(arr.shape[0])[:, np.newaxis]
88+
>>> row_indices
8989
array([[0],
9090
[1],
9191
[2]])
9292

93-
Using the ``shape_indices`` and ``column_indices`` for advanced
93+
Use the ``row_indices`` and ``column_indices`` for advanced
9494
indexing::
9595

96-
>>> arr[shape_indices, column_indices]
96+
>>> arr[row_indices, column_indices]
9797
array([[ 1, 3],
9898
[ 4, 6],
9999
[10, 10]])
100100

101101
Create subsets of larger matrices
102102
=================================
103103

104-
Use :ref:`slicing-and-striding` to access chunks of an array.
105-
But if you want to access multiple scattered elements to create
106-
complicated subsets, you have to use :ref:`advanced-indexing`. Use
107-
:func:`ix_` to quickly contruct index arrays::
104+
Use :ref:`slicing-and-striding` to access chunks of a large array::
108105

109-
>>> indices = np.ix_([0, 1], [0, 2], [2, 4])
110-
>>> indices
111-
(array([[[0]],
112-
<BLANKLINE>
113-
[[1]]]),
114-
array([[[0],
115-
[2]]]),
116-
array([[[2, 4]]]))
117-
>>> a[indices]
118-
array([[[ 2, 4],
119-
[12, 14]],
120-
<BLANKLINE>
121-
[[17, 19],
122-
[27, 29]]])
123-
>>> indices = np.ix_([0, 1], [0, 1])
106+
>>> a = np.arange(100).reshape(10, 10)
107+
>>> a
108+
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
109+
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
110+
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
111+
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
112+
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
113+
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
114+
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
115+
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
116+
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
117+
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
118+
>>> a[2:5, 2:5]
119+
array([[22, 23, 24],
120+
[32, 33, 34],
121+
[42, 43, 44]])
122+
>>> a[2:5, 1:3]
123+
array([[21, 22],
124+
[31, 32],
125+
[41, 42]])
126+
>>> a[:5, :5]
127+
array([[ 0, 1, 2, 3, 4],
128+
[10, 11, 12, 13, 14],
129+
[20, 21, 22, 23, 24],
130+
[30, 31, 32, 33, 34],
131+
[40, 41, 42, 43, 44]])
132+
133+
The same thing can be done with advanced indexing in a slightly complex
134+
way. Remember that advanced indexing creates a copy::
135+
136+
>>> a[np.arange(5)[:,None], np.arange(5)[None,:]]
137+
array([[ 0, 1, 2, 3, 4],
138+
[10, 11, 12, 13, 14],
139+
[20, 21, 22, 23, 24],
140+
[30, 31, 32, 33, 34],
141+
[40, 41, 42, 43, 44]])
142+
143+
You can also use :meth:`mgrid` to generate indices::
144+
145+
>>> indices = np.mgrid[0:6:2]
124146
>>> indices
125-
(array([[0],
126-
[1]]),
127-
array([[0, 1]]))
128-
>>> a[indices]
129-
array([[[ 0, 1, 2, 3, 4],
130-
[ 5, 6, 7, 8, 9]],
131-
<BLANKLINE>
132-
[[15, 16, 17, 18, 19],
133-
[20, 21, 22, 23, 24]]])
147+
array([0, 2, 4])
148+
>>> a[:, indices]
149+
array([[ 0, 2, 4],
150+
[10, 12, 14],
151+
[20, 22, 24],
152+
[30, 32, 34],
153+
[40, 42, 44],
154+
[50, 52, 54],
155+
[60, 62, 64],
156+
[70, 72, 74],
157+
[80, 82, 84],
158+
[90, 92, 94]])
134159

135160
Filter values
136161
=============
137162

163+
Non-zero elements
164+
-----------------
165+
166+
Use :meth:`nonzero` to get a tuple of array indices of non-zero elements
167+
corresponding to every dimension::
168+
169+
>>> z = np.eye(3)
170+
>>> z
171+
array([[1., 0., 0.],
172+
[0., 1., 0.],
173+
[0., 0., 1.]])
174+
>>> np.nonzero(z)
175+
(array([0, 1, 2]), array([0, 1, 2]))
176+
177+
Use :meth:`flatnonzero` to fetch indices of elements that are non-zero in
178+
the flattened version of the ndarray::
179+
180+
>>> np.flatnonzero(z)
181+
array([0, 4, 8])
182+
183+
Arbitary conditions
184+
-------------------
185+
138186
Use :meth:`where` to generate indices based on conditions and then
139187
use :ref:`advanced-indexing`.
140188

189+
>>> a = np.arange(30).reshape(2, 3, 5)
141190
>>> indices = np.where(a % 2 == 0)
142191
>>> indices
143192
(array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1]),
@@ -159,8 +208,8 @@ Or, use :ref:`boolean-indexing`::
159208
>>> a[a > 14]
160209
array([15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
161210

162-
Replace values
163-
--------------
211+
Replace values after filtering
212+
------------------------------
164213

165214
Use assignment with filtering to replace desired values::
166215

numpy/lib/index_tricks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,13 @@ class MGridClass(nd_grid):
227227
228228
See Also
229229
--------
230-
numpy.lib.index_tricks.nd_grid : class of `ogrid` and `mgrid` objects
230+
lib.index_tricks.nd_grid : class of `ogrid` and `mgrid` objects
231231
ogrid : like mgrid but returns open (not fleshed out) mesh grids
232232
r_ : array concatenator
233233
234234
Examples
235235
--------
236-
>>> np.mgrid[0:5,0:5]
236+
>>> np.mgrid[0:5, 0:5]
237237
array([[[0, 0, 0, 0, 0],
238238
[1, 1, 1, 1, 1],
239239
[2, 2, 2, 2, 2],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy