python Get an equally spaced array instance

  • 2021-07-10 20:02:24
  • OfStack

You can use the linspace function in numpy


np.linspace(start, stop, num, endpoint, retstep, dtype)
#start And stop Is the starting and ending positions, both scalar 
#num To include start And stop The total number of interval points of, the default is 50
#endpoint For bool Value, which is False Will remove the last 1 Point calculation interval 
#restep For bool Value, which is True Returns both the data list and the interval value 
#dtype The default is the type of the input variable. Given the type, the generated array type will be changed to the target type 

np.linspace(1,3,num=4)
Out[17]: array([1.    , 1.66666667, 2.33333333, 3.    ])

np.linspace(1,3,num=4,endpoint=False)
Out[18]: array([1. , 1.5, 2. , 2.5])

np.linspace(1,3,num=4,endpoint=False,retstep=True)
Out[19]: (array([1. , 1.5, 2. , 2.5]), 0.5)

np.linspace(1,3,num=4,endpoint=False,retstep=True,dtype=float)
Out[20]: (array([1. , 1.5, 2. , 2.5]), 0.5)

np.linspace(1,3,num=4,endpoint=False,retstep=True,dtype=int)
Out[21]: (array([1, 1, 2, 2]), 0.5)

Related articles: