numpy automatically generates array details

  • 2020-06-19 10:34:25
  • OfStack

1 np. arange(), similar to range, creates a 1-dimensional array representing an arithmetic sequence by specifying a start value, a end value, and a step length. Note that this function and range1 do not contain a end value.


>>> np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.arange(0,1,0.1)
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
>>>

2 The np. linspace() function, unlike np. arange above, the third argument of this function specifies the number of elements, which represents a given starting and ending value and the number of elements, and generates a 1-dimensional arithmetic sequence. Contains the parameter endpoint Boolean value, defaults to True to include the final value, and False to exclude the final value.


>>> np.linspace(0,1,10)
array([ 0.    , 0.11111111, 0.22222222, 0.33333333, 0.44444444,
    0.55555556, 0.66666667, 0.77777778, 0.88888889, 1.    ])
>>> np.linspace(0,1,10,endpoint = False)
array([ 0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

3 np. logspace, which is similar to np. linspace, but produces an array of geometric Numbers with a default cardinality of 10


>>> np.logspace(0,4,5)
array([ 1.00000000e+00,  1.00000000e+01,  1.00000000e+02,
     1.00000000e+03,  1.00000000e+04])

However, the cardinality can also be changed. For example, the cardinality base = 2 is set as follows:


>>> np.logspace(0,3,5,base = 2)
array([ 1. , 1.68179283, 2.82842712, 4.75682846, 8. ])

As shown above, the starting point is 2^0 = 1, and the ending point is 2^3 = 8. 1 generates 5 points according to the geometric sequence, so the common ratio q = 2^(3/4).

4. np. zeros(), np. ones(), np. empty() can create an array of the specified shapes and types, where np. enpty() allocates only the memory used by the array and does not function for data initialization.


>>> np.empty((2,3),np.int32)
array([[ 8078112, 37431728, 8078112],
    [47828800, 47828712,    10]])

Note that the 2 by 3 array created above is not initialized.


>>> np.ones(4)
array([ 1., 1., 1., 1.])
>>> np.ones((2,3))
array([[ 1., 1., 1.],
    [ 1., 1., 1.]])
>>> np.ones(4,dtype = np.bool)
array([ True, True, True, True], dtype=bool)
>>> np.zeros(4,dtype = np.bool)
array([False, False, False, False], dtype=bool)
>>> np.zeros(4)
array([ 0., 0., 0., 0.])

The np. full() function generates an array initialized to the specified value


>> np.full(4,np.pi)
array([ 3.14159265, 3.14159265, 3.14159265, 3.14159265])
>>> np.full((2,3),np.pi)
array([[ 3.14159265, 3.14159265, 3.14159265],
    [ 3.14159265, 3.14159265, 3.14159265]])

In addition, np.zeros_like (), np.ones_like () and other functions are created in the array with the same parameter shape, namely np.zeros_like (a) and np.zeros (a.shape,dtype = a.type)


>>> a = np.arange(10).reshape(2,5)
>>> np.zeros_like(a)
array([[0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]])

np. fromfunction(), which generates an array from the specified function, with the first argument being the function name and the second argument being the array shape.


>>> np.fromfunction(lambda a,b:a == b,(3,3))
array([[ True, False, False],
    [False, True, False],
    [False, False, True]], dtype=bool)
>>> np.fromfunction(lambda i:i%7 +1,(10,))
array([ 1., 2., 3., 4., 5., 6., 7., 1., 2., 3.])

conclusion

That's the end of this article on numpy auto-generated array detail, I hope you find it helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: