In Python NumPy what is a dimension and axis?

前端 未结 6 2206
终归单人心
终归单人心 2020-12-04 05:29

I am coding with Pythons NumPy module. If coordinates of a point in 3D space are described as [1, 2, 1], wouldn\'t that be three dimensions, three

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 06:13

    Just paste part of answer from this answer:

    In Numpy, dimension, axis/axes, shape are related and sometimes similar concepts:

    In [1]: import numpy as np
    
    In [2]: a = np.array([[1,2],[3,4]])
    

    dimension

    In Mathematics/Physics, dimension or dimensionality is informally defined as the minimum number of coordinates needed to specify any point within a space. But in Numpy, according to the numpy doc, it's the same as axis/axes:

    In Numpy dimensions are called axes. The number of axes is rank.

    In [3]: a.ndim  # num of dimensions/axes, *Mathematics definition of dimension*
    Out[3]: 2
    

    axis/axes

    the nth coordinate to index an array in Numpy. And multidimensional arrays can have one index per axis.

    In [4]: a[1,0]  # to index `a`, we specific 1 at the first axis and 0 at the second axis.
    Out[4]: 3  # which results in 3 (locate at the row 1 and column 0, 0-based index)
    

    shape

    describes how many data along each available axis.

    In [5]: a.shape
    Out[5]: (2, 2)  # both the first and second axis have 2 (columns/rows/pages/blocks/...) data
    

提交回复
热议问题