@@ -66,15 +66,28 @@ def take(a, indices, axis=None, out=None, mode='raise'):
66
66
"""
67
67
Take elements from an array along an axis.
68
68
69
- This function does the same thing as "fancy" indexing (indexing arrays
70
- using arrays); however, it can be easier to use if you need elements
71
- along a given axis.
69
+ When axis is not None, this function does the same thing as "fancy"
70
+ indexing (indexing arrays using arrays); however, it can be easier to use
71
+ if you need elements along a given axis. A call such as
72
+ ``np.take(arr, indices, axis=3)`` is equivalent to
73
+ ``arr[:,:,:,indices,...]``.
74
+
75
+ Explained without fancy indexing, this is equivalent to the following use
76
+ of `ndindex`, which sets each of ``ii``, ``jj``, and ``kk`` to a tuple of
77
+ indices::
78
+
79
+ Ni, Nk = a.shape[:axis], a.shape[axis+1:]
80
+ Nj = indices.shape
81
+ for ii in ndindex(Ni):
82
+ for jj in ndindex(Nj):
83
+ for kk in ndindex(Nk):
84
+ out[ii + jj + kk] = a[ii + (indices[jj],) + kk]
72
85
73
86
Parameters
74
87
----------
75
- a : array_like
88
+ a : array_like (Ni..., M, Nk...)
76
89
The source array.
77
- indices : array_like
90
+ indices : array_like (Nj...)
78
91
The indices of the values to extract.
79
92
80
93
.. versionadded:: 1.8.0
@@ -83,7 +96,7 @@ def take(a, indices, axis=None, out=None, mode='raise'):
83
96
axis : int, optional
84
97
The axis over which to select values. By default, the flattened
85
98
input array is used.
86
- out : ndarray, optional
99
+ out : ndarray, optional (Ni..., Nj..., Nk...)
87
100
If provided, the result will be placed in this array. It should
88
101
be of the appropriate shape and dtype.
89
102
mode : {'raise', 'wrap', 'clip'}, optional
@@ -99,14 +112,31 @@ def take(a, indices, axis=None, out=None, mode='raise'):
99
112
100
113
Returns
101
114
-------
102
- subarray : ndarray
115
+ out : ndarray (Ni..., Nj..., Nk...)
103
116
The returned array has the same type as `a`.
104
117
105
118
See Also
106
119
--------
107
120
compress : Take elements using a boolean mask
108
121
ndarray.take : equivalent method
109
122
123
+ Notes
124
+ -----
125
+
126
+ By eliminating the inner loop in the description above, and using `s_` to
127
+ build simple slice objects, `take` can be expressed in terms of applying
128
+ fancy indexing to each 1-d slice::
129
+
130
+ Ni, Nk = a.shape[:axis], a.shape[axis+1:]
131
+ for ii in ndindex(Ni):
132
+ for kk in ndindex(Nj):
133
+ out[ii + s_[...,] + kk] = a[ii + s_[:,] + kk][indices]
134
+
135
+ For this reason, it is equivalent to (but faster than) the following use
136
+ of `apply_along_axis`::
137
+
138
+ out = np.apply_along_axis(lambda a_1d: a_1d[indices], axis, a)
139
+
110
140
Examples
111
141
--------
112
142
>>> a = [4, 3, 5, 7, 6, 8]
0 commit comments