@@ -11,8 +11,8 @@ How to index :class:`ndarrays <.ndarray>`
11
11
This page tackles common examples. For an in-depth look into indexing, refer
12
12
to :ref: `basics.indexing `.
13
13
14
- Access specific/arbitary rows and columns
15
- =========================================
14
+ Access specific/arbitrary rows and columns
15
+ ==========================================
16
16
17
17
Use :ref: `basic-indexing ` features like :ref: `slicing-and-striding `, and
18
18
:ref: `dimensional-indexing-tools `.
@@ -31,7 +31,7 @@ Use :ref:`basic-indexing` features like :ref:`slicing-and-striding`, and
31
31
>>> a[0 , :, 3 ]
32
32
array([ 3, 8, 13])
33
33
34
- Note that the output from indexing operations can have different shape as the
34
+ Note that the output from indexing operations can have different shape from the
35
35
original object. To preserve the original dimensions after indexing, you can
36
36
use :func: `newaxis `. To use other such tools, refer to
37
37
:ref: `dimensional-indexing-tools `.
@@ -52,29 +52,27 @@ Variables can also be used to index::
52
52
Refer to :ref: `dealing-with-variable-indices ` to see how to use
53
53
:term: `python:slice ` and :py:data: `Ellipsis ` in your index variables.
54
54
55
- Index along a specific axis
56
- ---------------------------
55
+ Index columns
56
+ -------------
57
57
58
- Use :meth: ` take `. See also :meth: ` take_along_axis ` and
59
- :meth: ` put_along_axis `.
58
+ To index columns, you have to index the last axis. Use
59
+ :ref: ` dimensional-indexing-tools ` to get the desired number of dimensions::
60
60
61
- >>> np.take(a, [2 , 3 ], axis = 2 )
62
- array([[[ 2, 3],
63
- [ 7, 8],
64
- [12, 13]],
65
- <BLANKLINE>
66
- [[17, 18],
67
- [22, 23],
68
- [27, 28]]])
69
- >>> np.take(a, [2 ], axis = 1 )
70
- array([[[10, 11, 12, 13, 14]],
61
+ >>> a = np.arange(24).reshape(2, 3, 4)
62
+ >>> a
63
+ array([[[ 0, 1, 2, 3],
64
+ [ 4, 5, 6, 7],
65
+ [ 8, 9, 10, 11]],
71
66
<BLANKLINE>
72
- [[25, 26, 27, 28, 29]]])
73
-
74
- Index columns
75
- -------------
67
+ [[12, 13, 14, 15],
68
+ [16, 17, 18, 19],
69
+ [20, 21, 22, 23]]])
70
+ >>> a[..., 3]
71
+ array([[ 3, 7, 11],
72
+ [15, 19, 23]])
76
73
77
- Use :ref: `dimensional-indexing-tools ` to avoid shape mismatches::
74
+ To index specific elements in each column, make use of :ref: `advanced-indexing `
75
+ as below::
78
76
79
77
>>> arr = np.arange(3*4).reshape(3, 4)
80
78
>>> arr
@@ -98,6 +96,34 @@ indexing::
98
96
[ 4, 6],
99
97
[10, 10]])
100
98
99
+ Index along a specific axis
100
+ ---------------------------
101
+
102
+ Use :meth: `take `. See also :meth: `take_along_axis ` and
103
+ :meth: `put_along_axis `.
104
+
105
+ >>> a = np.arange(30 ).reshape(2 , 3 , 5 )
106
+ >>> a
107
+ array([[[ 0, 1, 2, 3, 4],
108
+ [ 5, 6, 7, 8, 9],
109
+ [10, 11, 12, 13, 14]],
110
+ <BLANKLINE>
111
+ [[15, 16, 17, 18, 19],
112
+ [20, 21, 22, 23, 24],
113
+ [25, 26, 27, 28, 29]]])
114
+ >>> np.take(a, [2 , 3 ], axis = 2 )
115
+ array([[[ 2, 3],
116
+ [ 7, 8],
117
+ [12, 13]],
118
+ <BLANKLINE>
119
+ [[17, 18],
120
+ [22, 23],
121
+ [27, 28]]])
122
+ >>> np.take(a, [2 ], axis = 1 )
123
+ array([[[10, 11, 12, 13, 14]],
124
+ <BLANKLINE>
125
+ [[25, 26, 27, 28, 29]]])
126
+
101
127
Create subsets of larger matrices
102
128
=================================
103
129
@@ -130,10 +156,11 @@ Use :ref:`slicing-and-striding` to access chunks of a large array::
130
156
[30, 31, 32, 33, 34],
131
157
[40, 41, 42, 43, 44]])
132
158
133
- The same thing can be done with advanced indexing in a slightly complex
134
- way. Remember that advanced indexing creates a copy::
159
+ The same thing can be done with advanced indexing in a slightly more complex
160
+ way. Remember that
161
+ :ref: `advanced indexing creates a copy <indexing-operations >`::
135
162
136
- >>> a[np.arange(5)[:,None], np.arange(5)[None,:]]
163
+ >>> a[np.arange(5)[:, None], np.arange(5)[None, :]]
137
164
array([[ 0, 1, 2, 3, 4],
138
165
[10, 11, 12, 13, 14],
139
166
[20, 21, 22, 23, 24],
@@ -166,22 +193,22 @@ Non-zero elements
166
193
Use :meth: `nonzero ` to get a tuple of array indices of non-zero elements
167
194
corresponding to every dimension::
168
195
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]))
196
+ >>> z = np.array([[1, 2, 3, 0], [0, 0, 5, 3], [4, 6, 0, 0]] )
197
+ >>> z
198
+ array([[1, 2, 3, 0 ],
199
+ [0, 0, 5, 3 ],
200
+ [4, 6, 0, 0 ]])
201
+ >>> np.nonzero(z)
202
+ (array([0, 0, 0, 1, 1, 2, 2 ]), array([0, 1, 2, 2, 3, 0, 1 ]))
176
203
177
204
Use :meth: `flatnonzero ` to fetch indices of elements that are non-zero in
178
205
the flattened version of the ndarray::
179
206
180
207
>>> np.flatnonzero(z)
181
- array([0, 4, 8 ])
208
+ array([0, 1, 2, 6, 7, 8, 9 ])
182
209
183
- Arbitary conditions
184
- -------------------
210
+ Arbitrary conditions
211
+ --------------------
185
212
186
213
Use :meth: `where ` to generate indices based on conditions and then
187
214
use :ref: `advanced-indexing `.
@@ -287,9 +314,9 @@ Index the same ndarray multiple times efficiently
287
314
=================================================
288
315
289
316
It must be kept in mind that basic indexing produces :term: `views <view> `
290
- and advanced indexing produces :term: `copies <copy> `, which takes
291
- more time . Hence, you should take care to use basic indexing wherever
292
- possible instead of advanced indexing.
317
+ and advanced indexing produces :term: `copies <copy> `, which are
318
+ computationally less efficient . Hence, you should take care to use basic
319
+ indexing wherever possible instead of advanced indexing.
293
320
294
321
Further reading
295
322
===============
0 commit comments