Explaining the differences between dim, shape, rank, dimension and axis in numpy

后端 未结 3 1492
难免孤独
难免孤独 2021-02-06 03:35

I\'m new to python and numpy in general. I read several tutorials and still so confused between the differences in dim, ranks, shape, aixes and dimensions. My mind seems to be s

3条回答
  •  花落未央
    2021-02-06 04:25

    Dimensionality of NumPy arrays must be understood in the data structures sense, not the mathematical sense, i.e. it's the number of scalar indices you need to obtain a scalar value.(*)

    E.g., this is a 3-d array:

    >>> X = np.arange(24).reshape(2, 3, 4)
    >>> X
    array([[[ 0,  1,  2,  3],
            [ 4,  5,  6,  7],
            [ 8,  9, 10, 11]],
    
           [[12, 13, 14, 15],
            [16, 17, 18, 19],
            [20, 21, 22, 23]]])
    

    Indexing once gives a 2-d array (matrix):

    >>> X[0]
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    

    Indexing twice gives a 1-d array (vector), and indexing three times gives a scalar.

    The rank of X is its number of dimensions:

    >>> X.ndim
    3
    >>> np.rank(X)
    3
    

    Axis is roughly synonymous with dimension; it's used in broadcasting operations:

    >>> X.sum(axis=0)
    array([[12, 14, 16, 18],
           [20, 22, 24, 26],
           [28, 30, 32, 34]])
    >>> X.sum(axis=1)
    array([[12, 15, 18, 21],
           [48, 51, 54, 57]])
    >>> X.sum(axis=2)
    array([[ 6, 22, 38],
           [54, 70, 86]])
    

    To be honest, I find this definition of "rank" confusing since it matches neither the name of the attribute ndim nor the linear algebra definition of rank.

    Now regarding np.dot, what you have to understand is that there are three ways to represent a vector in NumPy: 1-d array, a column vector of shape (n, 1) or a row vector of shape (1, n). (Actually, there are more ways, e.g. as a (1, n, 1)-shaped array, but these are quite rare.) np.dot performs vector multiplication when both arguments are 1-d, matrix-vector multiplication when one argument is 1-d and the other is 2-d, and otherwise it performs a (generalized) matrix multiplication:

    >>> A = np.random.randn(2, 3)
    >>> v1d = np.random.randn(2)
    >>> np.dot(v1d, A)
    array([-0.29269547, -0.52215117,  0.478753  ])
    >>> vrow = np.atleast_2d(v1d)
    >>> np.dot(vrow, A)
    array([[-0.29269547, -0.52215117,  0.478753  ]])
    >>> vcol = vrow.T
    >>> np.dot(vcol, A)
    Traceback (most recent call last):
      File "", line 1, in 
        np.dot(vcol, A)
    ValueError: matrices are not aligned
    

    The rule "sum product over the last axis of a and the second-to-last of b" matches and generalizes the common definition of matrix multiplication.

    (*) Arrays of dtype=object are a bit of an exception, since they treat any Python object as a scalar.

提交回复
热议问题