MCPcopy Create free account
hub / github.com/numpy/numpy / atleast_3d

Function atleast_3d

numpy/core/shape_base.py:140–204  ·  view source on GitHub ↗

View inputs as arrays with at least three dimensions. Parameters ---------- arys1, arys2, ... : array_like One or more array-like sequences. Non-array inputs are converted to arrays. Arrays that already have three or more dimensions are preserved. Ret

(*arys)

Source from the content-addressed store, hash-verified

138
139@array_function_dispatch(_atleast_3d_dispatcher)
140def atleast_3d(*arys):
141 """
142 View inputs as arrays with at least three dimensions.
143
144 Parameters
145 ----------
146 arys1, arys2, ... : array_like
147 One or more array-like sequences. Non-array inputs are converted to
148 arrays. Arrays that already have three or more dimensions are
149 preserved.
150
151 Returns
152 -------
153 res1, res2, ... : ndarray
154 An array, or list of arrays, each with ``a.ndim >= 3``. Copies are
155 avoided where possible, and views with three or more dimensions are
156 returned. For example, a 1-D array of shape ``(N,)`` becomes a view
157 of shape ``(1, N, 1)``, and a 2-D array of shape ``(M, N)`` becomes a
158 view of shape ``(M, N, 1)``.
159
160 See Also
161 --------
162 atleast_1d, atleast_2d
163
164 Examples
165 --------
166 >>> np.atleast_3d(3.0)
167 array([[[3.]]])
168
169 >>> x = np.arange(3.0)
170 >>> np.atleast_3d(x).shape
171 (1, 3, 1)
172
173 >>> x = np.arange(12.0).reshape(4,3)
174 >>> np.atleast_3d(x).shape
175 (4, 3, 1)
176 >>> np.atleast_3d(x).base is x.base # x is a reshape, so not base itself
177 True
178
179 >>> for arr in np.atleast_3d([1, 2], [[1, 2]], [[[1, 2]]]):
180 ... print(arr, arr.shape) # doctest: +SKIP
181 ...
182 [[[1]
183 [2]]] (1, 2, 1)
184 [[[1]
185 [2]]] (1, 2, 1)
186 [[[1 2]]] (1, 1, 2)
187
188 """
189 res = []
190 for ary in arys:
191 ary = asanyarray(ary)
192 if ary.ndim == 0:
193 result = ary.reshape(1, 1, 1)
194 elif ary.ndim == 1:
195 result = ary[_nx.newaxis, :, _nx.newaxis]
196 elif ary.ndim == 2:
197 result = ary[:, :, _nx.newaxis]

Callers 6

test_0D_arrayMethod · 0.90
test_1D_arrayMethod · 0.90
test_2D_arrayMethod · 0.90
test_3D_arrayMethod · 0.90
dstackFunction · 0.90
test_shape_scalarMethod · 0.85

Calls 2

asanyarrayFunction · 0.85
reshapeMethod · 0.80

Tested by 5

test_0D_arrayMethod · 0.72
test_1D_arrayMethod · 0.72
test_2D_arrayMethod · 0.72
test_3D_arrayMethod · 0.72
test_shape_scalarMethod · 0.68