|
4 | 4 | import numpy as np
|
5 | 5 |
|
6 | 6 | try:
|
7 |
| - from numpy import broadcast_to, stack, nanprod, nancumsum, nancumprod |
| 7 | + from numpy import nancumsum, nancumprod |
8 | 8 | except ImportError: # pragma: no cover
|
9 |
| - # Code copied from newer versions of NumPy (v1.10 to v1.12). |
| 9 | + # Code copied from newer versions of NumPy (v1.12). |
10 | 10 | # Used under the terms of NumPy's license, see licenses/NUMPY_LICENSE.
|
11 | 11 |
|
12 |
| - def _maybe_view_as_subclass(original_array, new_array): |
13 |
| - if type(original_array) is not type(new_array): |
14 |
| - # if input was an ndarray subclass and subclasses were OK, |
15 |
| - # then view the result as that subclass. |
16 |
| - new_array = new_array.view(type=type(original_array)) |
17 |
| - # Since we have done something akin to a view from original_array, we |
18 |
| - # should let the subclass finalize (if it has it implemented, i.e., is |
19 |
| - # not None). |
20 |
| - if new_array.__array_finalize__: |
21 |
| - new_array.__array_finalize__(original_array) |
22 |
| - return new_array |
23 |
| - |
24 |
| - def _broadcast_to(array, shape, subok, readonly): |
25 |
| - shape = tuple(shape) if np.iterable(shape) else (shape,) |
26 |
| - array = np.array(array, copy=False, subok=subok) |
27 |
| - if not shape and array.shape: |
28 |
| - raise ValueError('cannot broadcast a non-scalar to a scalar array') |
29 |
| - if any(size < 0 for size in shape): |
30 |
| - raise ValueError('all elements of broadcast shape must be non-' |
31 |
| - 'negative') |
32 |
| - broadcast = np.nditer( |
33 |
| - (array,), flags=['multi_index', 'zerosize_ok', 'refs_ok'], |
34 |
| - op_flags=['readonly'], itershape=shape, order='C').itviews[0] |
35 |
| - result = _maybe_view_as_subclass(array, broadcast) |
36 |
| - if not readonly and array.flags.writeable: |
37 |
| - result.flags.writeable = True |
38 |
| - return result |
39 |
| - |
40 |
| - def broadcast_to(array, shape, subok=False): |
41 |
| - """Broadcast an array to a new shape. |
42 |
| -
|
43 |
| - Parameters |
44 |
| - ---------- |
45 |
| - array : array_like |
46 |
| - The array to broadcast. |
47 |
| - shape : tuple |
48 |
| - The shape of the desired array. |
49 |
| - subok : bool, optional |
50 |
| - If True, then sub-classes will be passed-through, otherwise |
51 |
| - the returned array will be forced to be a base-class array (default). |
52 |
| -
|
53 |
| - Returns |
54 |
| - ------- |
55 |
| - broadcast : array |
56 |
| - A readonly view on the original array with the given shape. It is |
57 |
| - typically not contiguous. Furthermore, more than one element of a |
58 |
| - broadcasted array may refer to a single memory location. |
59 |
| -
|
60 |
| - Raises |
61 |
| - ------ |
62 |
| - ValueError |
63 |
| - If the array is not compatible with the new shape according to NumPy's |
64 |
| - broadcasting rules. |
65 |
| -
|
66 |
| - Examples |
67 |
| - -------- |
68 |
| - >>> x = np.array([1, 2, 3]) |
69 |
| - >>> np.broadcast_to(x, (3, 3)) |
70 |
| - array([[1, 2, 3], |
71 |
| - [1, 2, 3], |
72 |
| - [1, 2, 3]]) |
73 |
| - """ |
74 |
| - return _broadcast_to(array, shape, subok=subok, readonly=True) |
75 |
| - |
76 |
| - def stack(arrays, axis=0): |
77 |
| - """ |
78 |
| - Join a sequence of arrays along a new axis. |
79 |
| -
|
80 |
| - .. versionadded:: 1.10.0 |
81 |
| -
|
82 |
| - Parameters |
83 |
| - ---------- |
84 |
| - arrays : sequence of ndarrays |
85 |
| - Each array must have the same shape. |
86 |
| - axis : int, optional |
87 |
| - The axis along which the arrays will be stacked. |
88 |
| -
|
89 |
| - Returns |
90 |
| - ------- |
91 |
| - stacked : ndarray |
92 |
| - The stacked array has one more dimension than the input arrays. |
93 |
| - See Also |
94 |
| - -------- |
95 |
| - concatenate : Join a sequence of arrays along an existing axis. |
96 |
| - split : Split array into a list of multiple sub-arrays of equal size. |
97 |
| -
|
98 |
| - Examples |
99 |
| - -------- |
100 |
| - >>> arrays = [np.random.randn(3, 4) for _ in range(10)] |
101 |
| - >>> np.stack(arrays, axis=0).shape |
102 |
| - (10, 3, 4) |
103 |
| -
|
104 |
| - >>> np.stack(arrays, axis=1).shape |
105 |
| - (3, 10, 4) |
106 |
| -
|
107 |
| - >>> np.stack(arrays, axis=2).shape |
108 |
| - (3, 4, 10) |
109 |
| -
|
110 |
| - >>> a = np.array([1, 2, 3]) |
111 |
| - >>> b = np.array([2, 3, 4]) |
112 |
| - >>> np.stack((a, b)) |
113 |
| - array([[1, 2, 3], |
114 |
| - [2, 3, 4]]) |
115 |
| -
|
116 |
| - >>> np.stack((a, b), axis=-1) |
117 |
| - array([[1, 2], |
118 |
| - [2, 3], |
119 |
| - [3, 4]]) |
120 |
| - """ |
121 |
| - arrays = [np.asanyarray(arr) for arr in arrays] |
122 |
| - if not arrays: |
123 |
| - raise ValueError('need at least one array to stack') |
124 |
| - |
125 |
| - shapes = set(arr.shape for arr in arrays) |
126 |
| - if len(shapes) != 1: |
127 |
| - raise ValueError('all input arrays must have the same shape') |
128 |
| - |
129 |
| - result_ndim = arrays[0].ndim + 1 |
130 |
| - if not -result_ndim <= axis < result_ndim: |
131 |
| - msg = 'axis {0} out of bounds [-{1}, {1})'.format(axis, result_ndim) |
132 |
| - raise IndexError(msg) |
133 |
| - if axis < 0: |
134 |
| - axis += result_ndim |
135 |
| - |
136 |
| - sl = (slice(None),) * axis + (np.newaxis,) |
137 |
| - expanded_arrays = [arr[sl] for arr in arrays] |
138 |
| - return np.concatenate(expanded_arrays, axis=axis) |
139 |
| - |
140 | 12 | def _replace_nan(a, val):
|
141 | 13 | """
|
142 | 14 | If `a` is of inexact type, make a copy of `a`, replace NaNs with
|
@@ -178,75 +50,6 @@ def _replace_nan(a, val):
|
178 | 50 | np.copyto(a, val, where=mask)
|
179 | 51 | return a, mask
|
180 | 52 |
|
181 |
| - def nanprod(a, axis=None, dtype=None, out=None, keepdims=0): |
182 |
| - """ |
183 |
| - Return the product of array elements over a given axis treating Not a |
184 |
| - Numbers (NaNs) as zero. |
185 |
| -
|
186 |
| - One is returned for slices that are all-NaN or empty. |
187 |
| -
|
188 |
| - .. versionadded:: 1.10.0 |
189 |
| -
|
190 |
| - Parameters |
191 |
| - ---------- |
192 |
| - a : array_like |
193 |
| - Array containing numbers whose sum is desired. If `a` is not an |
194 |
| - array, a conversion is attempted. |
195 |
| - axis : int, optional |
196 |
| - Axis along which the product is computed. The default is to compute |
197 |
| - the product of the flattened array. |
198 |
| - dtype : data-type, optional |
199 |
| - The type of the returned array and of the accumulator in which the |
200 |
| - elements are summed. By default, the dtype of `a` is used. An |
201 |
| - exception is when `a` has an integer type with less precision than |
202 |
| - the platform (u)intp. In that case, the default will be either |
203 |
| - (u)int32 or (u)int64 depending on whether the platform is 32 or 64 |
204 |
| - bits. For inexact inputs, dtype must be inexact. |
205 |
| - out : ndarray, optional |
206 |
| - Alternate output array in which to place the result. The default |
207 |
| - is ``None``. If provided, it must have the same shape as the |
208 |
| - expected output, but the type will be cast if necessary. See |
209 |
| - `doc.ufuncs` for details. The casting of NaN to integer can yield |
210 |
| - unexpected results. |
211 |
| - keepdims : bool, optional |
212 |
| - If True, the axes which are reduced are left in the result as |
213 |
| - dimensions with size one. With this option, the result will |
214 |
| - broadcast correctly against the original `arr`. |
215 |
| -
|
216 |
| - Returns |
217 |
| - ------- |
218 |
| - y : ndarray or numpy scalar |
219 |
| -
|
220 |
| - See Also |
221 |
| - -------- |
222 |
| - numpy.prod : Product across array propagating NaNs. |
223 |
| - isnan : Show which elements are NaN. |
224 |
| -
|
225 |
| - Notes |
226 |
| - ----- |
227 |
| - Numpy integer arithmetic is modular. If the size of a product exceeds |
228 |
| - the size of an integer accumulator, its value will wrap around and the |
229 |
| - result will be incorrect. Specifying ``dtype=double`` can alleviate |
230 |
| - that problem. |
231 |
| -
|
232 |
| - Examples |
233 |
| - -------- |
234 |
| - >>> np.nanprod(1) |
235 |
| - 1 |
236 |
| - >>> np.nanprod([1]) |
237 |
| - 1 |
238 |
| - >>> np.nanprod([1, np.nan]) |
239 |
| - 1.0 |
240 |
| - >>> a = np.array([[1, 2], [3, np.nan]]) |
241 |
| - >>> np.nanprod(a) |
242 |
| - 6.0 |
243 |
| - >>> np.nanprod(a, axis=0) |
244 |
| - array([ 3., 2.]) |
245 |
| -
|
246 |
| - """ |
247 |
| - a, mask = _replace_nan(a, 1) |
248 |
| - return np.prod(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims) |
249 |
| - |
250 | 53 | def nancumsum(a, axis=None, dtype=None, out=None):
|
251 | 54 | """
|
252 | 55 | Return the cumulative sum of array elements over a given axis treating
|
|
0 commit comments