@@ -49,6 +49,9 @@ Variables can also be used to index::
49
49
>>> a[y, :, y+3]
50
50
array([ 3, 8, 13])
51
51
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
+
52
55
Index along a specific axis
53
56
---------------------------
54
57
@@ -81,63 +84,109 @@ Use :ref:`dimensional-indexing-tools` to avoid shape mismatches::
81
84
>>> column_indices = [[1, 3], [0, 2], [2, 2]]
82
85
>>> np.arange(arr.shape[0])
83
86
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
89
89
array([[0],
90
90
[1],
91
91
[2]])
92
92
93
- Using the ``shape_indices `` and ``column_indices `` for advanced
93
+ Use the ``row_indices `` and ``column_indices `` for advanced
94
94
indexing::
95
95
96
- >>> arr[shape_indices , column_indices]
96
+ >>> arr[row_indices , column_indices]
97
97
array([[ 1, 3],
98
98
[ 4, 6],
99
99
[10, 10]])
100
100
101
101
Create subsets of larger matrices
102
102
=================================
103
103
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::
108
105
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]
124
146
>>> 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]])
134
159
135
160
Filter values
136
161
=============
137
162
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
+
138
186
Use :meth: `where ` to generate indices based on conditions and then
139
187
use :ref: `advanced-indexing `.
140
188
189
+ >>> a = np.arange(30 ).reshape(2 , 3 , 5 )
141
190
>>> indices = np.where(a % 2 == 0 )
142
191
>>> indices
143
192
(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`::
159
208
>>> a[a > 14]
160
209
array([15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29])
161
210
162
- Replace values
163
- --------------
211
+ Replace values after filtering
212
+ ------------------------------
164
213
165
214
Use assignment with filtering to replace desired values::
166
215
0 commit comments