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

Function expand_dims

numpy/lib/shape_base.py:513–602  ·  view source on GitHub ↗

Expand the shape of an array. Insert a new axis that will appear at the `axis` position in the expanded array shape. Parameters ---------- a : array_like Input array. axis : int or tuple of ints Position in the expanded axes where the new axis (or axes)

(a, axis)

Source from the content-addressed store, hash-verified

511
512@array_function_dispatch(_expand_dims_dispatcher)
513def expand_dims(a, axis):
514 """
515 Expand the shape of an array.
516
517 Insert a new axis that will appear at the `axis` position in the expanded
518 array shape.
519
520 Parameters
521 ----------
522 a : array_like
523 Input array.
524 axis : int or tuple of ints
525 Position in the expanded axes where the new axis (or axes) is placed.
526
527 .. deprecated:: 1.13.0
528 Passing an axis where ``axis > a.ndim`` will be treated as
529 ``axis == a.ndim``, and passing ``axis < -a.ndim - 1`` will
530 be treated as ``axis == 0``. This behavior is deprecated.
531
532 .. versionchanged:: 1.18.0
533 A tuple of axes is now supported. Out of range axes as
534 described above are now forbidden and raise an `AxisError`.
535
536 Returns
537 -------
538 result : ndarray
539 View of `a` with the number of dimensions increased.
540
541 See Also
542 --------
543 squeeze : The inverse operation, removing singleton dimensions
544 reshape : Insert, remove, and combine dimensions, and resize existing ones
545 doc.indexing, atleast_1d, atleast_2d, atleast_3d
546
547 Examples
548 --------
549 >>> x = np.array([1, 2])
550 >>> x.shape
551 (2,)
552
553 The following is equivalent to ``x[np.newaxis, :]`` or ``x[np.newaxis]``:
554
555 >>> y = np.expand_dims(x, axis=0)
556 >>> y
557 array([[1, 2]])
558 >>> y.shape
559 (1, 2)
560
561 The following is equivalent to ``x[:, np.newaxis]``:
562
563 >>> y = np.expand_dims(x, axis=1)
564 >>> y
565 array([[1],
566 [2]])
567 >>> y.shape
568 (2, 1)
569
570 ``axis`` may also be a tuple:

Callers 4

test_functionalityMethod · 0.90
anomMethod · 0.90
apply_over_axesFunction · 0.70
kronFunction · 0.70

Calls 5

normalize_axis_tupleFunction · 0.90
asanyarrayFunction · 0.85
reshapeMethod · 0.80
asarrayFunction · 0.50
nextFunction · 0.50

Tested by 1

test_functionalityMethod · 0.72