Cara menggunakan NP.EXPAND_DIMS pada Python

Np expand_dims | Numpy expand_dims() Contoh dengan Python
Metode numpy expand_dims() memperluas bentuk array. np expand_dims menyisipkan sumbu baru yang akan muncul pada posisi sumbu dalam bentuk array yang diperluas.
Metode Python Numpy expand_dims() memperluas array dengan memasukkan sumbu baru pada posisi yang ditentukan. Fungsi ini membutuhkan dua parameter.


#python #numpy #np #programming

Cara menggunakan NP.EXPAND_DIMS pada Python

appdividend.com

Numpy expand_dims() memperluas bentuk array. np expand_dims menyisipkan sumbu baru yang akan muncul pada posisi sumbu dalam bentuk array yang diperluas.



View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    With the help of Numpy.expand_dims() method, we can get the expanded dimensions of an array by using Numpy.expand_dims() method.

    Syntax : Numpy.expand_dims()

    Return : Return the expanded array.

    Example #1 :
    In this example we can see that using Numpy.expand_dims() method, we are able to get the expanded array using this method.

    import numpy as np

    gfg = np.array([1, 2])

    print(gfg.shape)

    gfg = np.expand_dims(gfg, axis = 0)

    print(gfg.shape)

    Output :

    (2, )
    (1, 2)

    Example #2 :

    import numpy as np

    gfg = np.array([[1, 2], [7, 8]])

    print(gfg.shape)

    gfg = np.expand_dims(gfg, axis = 0)

    print(gfg.shape)

    Output :

    (2, 2)
    (1, 2, 2)

    numpy.expand_dims(a, axis)[source]#

    Expand the shape of an array.

    Insert a new axis that will appear at the axis position in the expanded array shape.

    Parametersaarray_like

    Input array.

    axisint or tuple of ints

    Position in the expanded axes where the new axis (or axes) is placed.

    Deprecated since version 1.13.0: Passing an axis where axis > a.ndim will be treated as axis == a.ndim, and passing axis < -a.ndim - 1 will be treated as axis == 0. This behavior is deprecated.

    Changed in version 1.18.0: A tuple of axes is now supported. Out of range axes as described above are now forbidden and raise an AxisError.

    Returnsresultndarray

    View of a with the number of dimensions increased.

    Examples

    >>> x = np.array([1, 2])
    >>> x.shape
    (2,)
    

    The following is equivalent to x[np.newaxis, :] or x[np.newaxis]:

    >>> y = np.expand_dims(x, axis=0)
    >>> y
    array([[1, 2]])
    >>> y.shape
    (1, 2)
    

    The following is equivalent to x[:, np.newaxis]:

    >>> y = np.expand_dims(x, axis=1)
    >>> y
    array([[1],
           [2]])
    >>> y.shape
    (2, 1)
    

    axis may also be a tuple:

    >>> y = np.expand_dims(x, axis=(0, 1))
    >>> y
    array([[[1, 2]]])
    

    >>> y = np.expand_dims(x, axis=(2, 0))
    >>> y
    array([[[1],
            [2]]])
    

    Note that some examples may use None instead of np.newaxis. These are the same objects:

    >>> np.newaxis is None
    True