Skip to content

Commit 916b65a

Browse files
committed
DOC: Minor typos and improvement in explanations
1 parent 89c6446 commit 916b65a

File tree

2 files changed

+67
-38
lines changed

2 files changed

+67
-38
lines changed

doc/source/user/basics.copies.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ do not reflect on the original array. Making a copy is slower and
3939
memory-consuming but sometimes necessary. A copy can be forced by using
4040
:meth:`.ndarray.copy`.
4141

42+
.. _indexing-operations:
43+
4244
Indexing operations
4345
===================
4446

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

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ How to index :class:`ndarrays <.ndarray>`
1111
This page tackles common examples. For an in-depth look into indexing, refer
1212
to :ref:`basics.indexing`.
1313

14-
Access specific/arbitary rows and columns
15-
=========================================
14+
Access specific/arbitrary rows and columns
15+
==========================================
1616

1717
Use :ref:`basic-indexing` features like :ref:`slicing-and-striding`, and
1818
:ref:`dimensional-indexing-tools`.
@@ -31,7 +31,7 @@ Use :ref:`basic-indexing` features like :ref:`slicing-and-striding`, and
3131
>>> a[0, :, 3]
3232
array([ 3, 8, 13])
3333

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
3535
original object. To preserve the original dimensions after indexing, you can
3636
use :func:`newaxis`. To use other such tools, refer to
3737
:ref:`dimensional-indexing-tools`.
@@ -52,29 +52,27 @@ Variables can also be used to index::
5252
Refer to :ref:`dealing-with-variable-indices` to see how to use
5353
:term:`python:slice` and :py:data:`Ellipsis` in your index variables.
5454

55-
Index along a specific axis
56-
---------------------------
55+
Index columns
56+
-------------
5757

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::
6060

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]],
7166
<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]])
7673

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::
7876

7977
>>> arr = np.arange(3*4).reshape(3, 4)
8078
>>> arr
@@ -98,6 +96,34 @@ indexing::
9896
[ 4, 6],
9997
[10, 10]])
10098

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+
101127
Create subsets of larger matrices
102128
=================================
103129

@@ -130,10 +156,11 @@ Use :ref:`slicing-and-striding` to access chunks of a large array::
130156
[30, 31, 32, 33, 34],
131157
[40, 41, 42, 43, 44]])
132158

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>`::
135162

136-
>>> a[np.arange(5)[:,None], np.arange(5)[None,:]]
163+
>>> a[np.arange(5)[:, None], np.arange(5)[None, :]]
137164
array([[ 0, 1, 2, 3, 4],
138165
[10, 11, 12, 13, 14],
139166
[20, 21, 22, 23, 24],
@@ -166,22 +193,22 @@ Non-zero elements
166193
Use :meth:`nonzero` to get a tuple of array indices of non-zero elements
167194
corresponding to every dimension::
168195

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]))
176203

177204
Use :meth:`flatnonzero` to fetch indices of elements that are non-zero in
178205
the flattened version of the ndarray::
179206

180207
>>> np.flatnonzero(z)
181-
array([0, 4, 8])
208+
array([0, 1, 2, 6, 7, 8, 9])
182209

183-
Arbitary conditions
184-
-------------------
210+
Arbitrary conditions
211+
--------------------
185212

186213
Use :meth:`where` to generate indices based on conditions and then
187214
use :ref:`advanced-indexing`.
@@ -287,9 +314,9 @@ Index the same ndarray multiple times efficiently
287314
=================================================
288315

289316
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.
293320

294321
Further reading
295322
===============

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