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