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)
| 138 | |
| 139 | @array_function_dispatch(_atleast_3d_dispatcher) |
| 140 | def 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] |