Join a sequence of arrays along a new axis. The ``axis`` parameter specifies the index of the new axis in the dimensions of the result. For example, if ``axis=0`` it will be the first dimension and if ``axis=-1`` it will be the last dimension. .. versionadded:: 1.10.0 Par
(arrays, axis=0, out=None, *, dtype=None, casting="same_kind")
| 371 | |
| 372 | @array_function_dispatch(_stack_dispatcher) |
| 373 | def stack(arrays, axis=0, out=None, *, dtype=None, casting="same_kind"): |
| 374 | """ |
| 375 | Join a sequence of arrays along a new axis. |
| 376 | |
| 377 | The ``axis`` parameter specifies the index of the new axis in the |
| 378 | dimensions of the result. For example, if ``axis=0`` it will be the first |
| 379 | dimension and if ``axis=-1`` it will be the last dimension. |
| 380 | |
| 381 | .. versionadded:: 1.10.0 |
| 382 | |
| 383 | Parameters |
| 384 | ---------- |
| 385 | arrays : sequence of array_like |
| 386 | Each array must have the same shape. |
| 387 | |
| 388 | axis : int, optional |
| 389 | The axis in the result array along which the input arrays are stacked. |
| 390 | |
| 391 | out : ndarray, optional |
| 392 | If provided, the destination to place the result. The shape must be |
| 393 | correct, matching that of what stack would have returned if no |
| 394 | out argument were specified. |
| 395 | |
| 396 | dtype : str or dtype |
| 397 | If provided, the destination array will have this dtype. Cannot be |
| 398 | provided together with `out`. |
| 399 | |
| 400 | .. versionadded:: 1.24 |
| 401 | |
| 402 | casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional |
| 403 | Controls what kind of data casting may occur. Defaults to 'same_kind'. |
| 404 | |
| 405 | .. versionadded:: 1.24 |
| 406 | |
| 407 | |
| 408 | Returns |
| 409 | ------- |
| 410 | stacked : ndarray |
| 411 | The stacked array has one more dimension than the input arrays. |
| 412 | |
| 413 | See Also |
| 414 | -------- |
| 415 | concatenate : Join a sequence of arrays along an existing axis. |
| 416 | block : Assemble an nd-array from nested lists of blocks. |
| 417 | split : Split array into a list of multiple sub-arrays of equal size. |
| 418 | |
| 419 | Examples |
| 420 | -------- |
| 421 | >>> arrays = [np.random.randn(3, 4) for _ in range(10)] |
| 422 | >>> np.stack(arrays, axis=0).shape |
| 423 | (10, 3, 4) |
| 424 | |
| 425 | >>> np.stack(arrays, axis=1).shape |
| 426 | (3, 10, 4) |
| 427 | |
| 428 | >>> np.stack(arrays, axis=2).shape |
| 429 | (3, 4, 10) |
| 430 |