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