1
1
from __future__ import division , absolute_import , print_function
2
2
3
- __all__ = ['atleast_1d' , 'atleast_2d' , 'atleast_3d' , 'block ' , 'hstack ' ,
4
- 'stack' , 'vstack' ]
3
+ __all__ = ['atleast_1d' , 'atleast_2d' , 'atleast_3d' , 'atleast_nd ' , 'block ' ,
4
+ 'hstack' , ' stack' , 'vstack' ]
5
5
6
6
7
7
from . import numeric as _nx
8
8
from .numeric import array , asanyarray , newaxis
9
9
from .multiarray import normalize_axis_index
10
10
from ._internal import recursive
11
11
12
+
12
13
def atleast_1d (* arys ):
13
14
"""
14
15
Convert inputs to arrays with at least one dimension.
@@ -29,7 +30,7 @@ def atleast_1d(*arys):
29
30
30
31
F438
See Also
31
32
--------
32
- atleast_2d, atleast_3d
33
+ atleast_2d, atleast_3d, atleast_nd
33
34
34
35
Examples
35
36
--------
@@ -48,18 +49,10 @@ def atleast_1d(*arys):
48
49
[array([1]), array([3, 4])]
49
50
50
51
"""
51
- res = []
52
- for ary in arys :
53
- ary = asanyarray (ary )
54
- if ary .ndim == 0 :
55
- result = ary .reshape (1 )
56
- else :
57
- result = ary
58
- res .append (result )
59
- if len (res ) == 1 :
60
- return res [0 ]
61
- else :
62
- return res
52
+ if len (arys ) == 1 :
53
+ return atleast_nd (arys [0 ], 1 )
54
+ return [atleast_nd (x , 1 ) for x in arys ]
55
+
63
56
64
57
def atleast_2d (* arys ):
65
58
"""
@@ -68,9 +61,9 @@ def atleast_2d(*arys):
68
61
Parameters
69
62
----------
70
63
arys1, arys2, ... : array_like
71
- One or more array-like sequences. Non-array inputs are converted
72
- to arrays. Arrays that already have two or more dimensions are
73
- preserved.
64
+ One or more array-like sequences. Non-array inputs are
65
+ converted to arrays. Arrays that already have two or more
66
+ dimensions are preserved.
74
67
75
68
Returns
76
69
-------
@@ -81,7 +74,7 @@ def atleast_2d(*arys):
81
74
82
75
See Also
83
76
--------
84
- atleast_1d, atleast_3d
77
+ atleast_1d, atleast_3d, atleast_nd
85
78
86
79
Examples
87
80
--------
@@ -98,20 +91,10 @@ def atleast_2d(*arys):
98
91
[array([[1]]), array([[1, 2]]), array([[1, 2]])]
99
92
100
93
"""
101
- res = []
102
- for ary in arys :
103
- ary = asanyarray (ary )
104
- if ary .ndim == 0 :
105
- result = ary .reshape (1 , 1 )
106
- elif ary .ndim == 1 :
107
- result = ary [newaxis ,:]
108
- else :
109
- result = ary
110
- res .append (result )
111
- if len (res ) == 1 :
112
- return res [0 ]
113
- else :
114
- return res
94
+ if len (arys ) == 1 :
95
+ return atleast_nd (arys [0 ], 2 )
96
+ return [atleast_nd (x , 2 ) for x in arys ]
97
+
115
98
116
99
def atleast_3d (* arys ):
117
100
"""
@@ -120,22 +103,31 @@ def atleast_3d(*arys):
120
103
Parameters
121
104
----------
122
105
arys1, arys2, ... : array_like
123
- One or more array-like sequences. Non-array inputs are converted to
124
- arrays. Arrays that already have three or more dimensions are
125
- preserved.
106
+ One or more array-like sequences. Non-array inputs are
107
+ converted to arrays. Arrays that already have three or more
108
+ dimensions are preserved.
126
109
127
110
Returns
128
111
-------
129
112
res1, res2, ... : ndarray
130
- An array, or list of arrays, each with ``a.ndim >= 3``. Copies are
131
- avoided where possible, and views with three or more dimensions are
132
- returned. For example, a 1-D array of shape ``(N,)`` becomes a view
133
- of shape ``(1, N, 1 )``, and a 2-D array of shape ``(M , N)`` becomes a
134
- view of shape ``(M, N, 1)``.
113
+ An array, or list of arrays, each with ``a.ndim >= 3``. Copies
114
+ are avoided where possible, and views with three or more
115
+ dimensions are returned. For example, a 1-D array of shape
116
+ ``(N, )`` becomes a view of shape ``(1 , N, 1 )``, and a 2-D array
117
+ of shape ``(M, N)`` becomes a view of shape ``(M, N, 1)``.
135
118
136
119
See Also
137
120
--------
138
- atleast_1d, atleast_2d
121
+ atleast_1d, atleast_2d, atleast_nd
122
+
123
+ Notes
124
+ -----
125
+ As mentioned in the `Returns` section, the results of this fuction
126
+ are not compatible with any of the other `atleast*` functions.
127
+ `atleast_2d` prepends the unit dimension to a 1D array while
128
+ `atleast_3d` appends it to a 2D array. The 1D array case both
129
+ appends and prepends a dimension, while `atleast_nd` can only add
130
+ dimensions to one end at a time.
139
131
140
132
Examples
141
133
--------
@@ -168,9 +160,9 @@ def atleast_3d(*arys):
168
160
if ary .ndim == 0 :
169
161
result = ary .reshape (1 , 1 , 1 )
170
162
elif ary .ndim == 1 :
171
- result = ary [newaxis ,:, newaxis ]
163
+ result = ary [newaxis , :, newaxis ]
172
164
elif ary .ndim == 2 :
173
- result = ary [:,:, newaxis ]
165
+ result = ary [:, :, newaxis ]
174
166
else :
175
167
result = ary
176
168
res .append (result )
@@ -180,6 +172,100 @@ def atleast_3d(*arys):
180
172
return res
181
173
182
174
175
+ def atleast_nd (ary , ndim , pos = 0 ):
176
+ """
177
+ View input as array with at least `ndim` dimensions.
178
+
179
+ New unit dimensions are inserted at the index given by `pos` if
180
+ necessary.
181
+
182
+ Parameters
183
+ ----------
184
+ ary : array_like
185
+ The input array. Non-array inputs are converted to arrays.
186
+ Arrays that already have `ndim` or more dimensions are
187
+ preserved.
188
+ ndim : scalar
189
+ The minimum number of dimensions required.
190
+ pos : int, optional
191
+ The index to insert the new dimensions. May range from
192
+ ``-ary.ndim - 1`` to ``+ary.ndim`` (inclusive). Non-negative
193
+ indices indicate locations before the corresponding axis:
194
+ ``pos=0`` means to insert at the very beginning. Negative
195
+ indices indicate locations after the corresponding axis:
196
+ ``pos=-1`` means to insert at the very end. 0 and -1 are always
197
+ guaranteed to work. Any other number will depend on the
198
+ dimensions of the existing array. Default is 0.
199
+
200
+ Returns
201
+ -------
202
+ res : ndarray
203
+ An array with ``res.ndim >= ndim``. A view is returned for array
204
+ inputs. Dimensions are prepended if `pos` is 0, so for example,
205
+ a 1-D array of shape ``(N,)`` with ``ndim=4`` becomes a view of
206
+ shape ``(1, 1, 1, N)``. Dimensions are appended if `pos` is -1,
207
+ so for example a 2-D array of shape ``(M, N)`` becomes a view of
208
+ shape ``(M, N, 1, 1)`` when ``ndim=4``.
209
+
210
+ See Also
211
+ --------
212
+ atleast_1d, atleast_2d, atleast_3d
213
+
214
+ Notes
215
+ -----
216
+ This function does not follow the convention of the other atleast_*d
217
+ functions in numpy in that it only accepts a single array argument.
218
+ To process multiple arrays, use a comprehension or loop around the
219
+ function call. See examples below.
220
+
221
+ Setting ``pos=0`` is equivalent to how the array would be
222
+ interpreted by numpy's broadcasting rules. There is no need to call
223
+ this function for simple broadcasting. This is also roughly
224
+ (but not exactly) equivalent to
225
+ ``np.array(ary, copy=False, subok=True, ndmin=ndim)``.
226
+
227
+ It is easy to create functions for specific dimensions similar to
228
+ the other atleast_*d functions using Python's `functools.partial`
229
+ function. An example is shown below.
230
+
231
+ Examples
232
+ --------
233
+ >>> np.atleast_nd(3.0, 4)
234
+ array([[[[ 3.]]]])
235
+
236
+ >>> x = np.arange(3.0)
237
+ >>> np.atleast_nd(x, 2).shape
238
+ (1, 3)
239
+
240
+ >>> x = np.arange(12.0).reshape(4, 3)
241
+ >>> np.atleast_nd(x, 5).shape
242
+ (1, 1, 1, 4, 3)
243
+ >>> np.atleast_nd(x, 5).base is x.base
244
+ True
245
+
246
+ >>> [np.atleast_nd(x) for x in ((1, 2), [[1, 2]], [[[1, 2]]])]:
247
+ [array([[1, 2]]), array([[1, 2]]), array([[[1, 2]]])]
248
+
249
+ >>> np.atleast_nd((1, 2), 5, pos=0).shape
250
+ (1, 1, 1, 1, 2)
251
+ >>> np.atleast_nd((1, 2), 5, pos=-1).shape
252
+ (2, 1, 1, 1, 1)
253
+
254
+ >>> from functools import partial
255
+ >>> atleast_4d = partial(np.atleast_nd, ndim=4)
256
+ >>> atleast_4d([1, 2, 3])
257
+ [[[[1, 2, 3]]]]
258
+ """
259
+ ary = array (ary , copy = False , subok = True )
260
+ if ary .ndim :
261
+ pos = normalize_axis_index (pos , ary .ndim + 1 )
262
+ extra = ndim - ary .ndim
263
+ if extra > 0 :
264
+ ind = pos * (slice (None ),) + extra * (None ,) + (Ellipsis ,)
265
+ ary = ary [ind ]
266
+ return ary
267
+
268
+
183
269
def vstack (tup ):
184
270
"""
185
271
Stack arrays in sequence vertically (row wise).
0 commit comments