Python library numpy knowledge record

    numpy

    1. zeros

     It is used to create an array with all 0 elements, and the dimension of the array is based on the parameter.

     Examples:

>>> np.zeros(5)
array([ 0.,  0.,  0.,  0.,  0.])
>>> np.zeros((5,), dtype=np.int)
array([0, 0, 0, 0, 0])
>>> np.zeros((2, 1))
# This means creating a 2-dimensional array (that is, a matrix), which contains 2 rows and 1 column
array([[ 0.],
       [ 0.]])
>>> s = (2,2)
>>> np.zeros(s)
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> np.zeros((2,3,4))
# This means creating a 3-dimensional array (that is, a tensor), which contains 2 matrices of 3 * 4
array([[[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]],
 [[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]])
>>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
array([(0, 0), (0, 0)],
      dtype=[('x', '<i4'), ('y', '<i4')])

    It can be seen that the first parameter of the zeros function:

    If it is a pure number, it represents a one-dimensional array, and the number is equal to this number, which is actually equivalent to np.zeros((n)).

    If it is a tuple containing n elements, it means the created n-dimensional array, n1,n2,...,n, that is, the first dimension contains n1, and the second dimension contains n2... elements.

 

    2. array.shape

    What is returned is the size of a tuple, the return value is represented by a tuple, and there are several numbers in it to indicate a few-dimensional array.

   Examples:

>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326560942&siteId=291194637