What is the difference between np.linspace and np.arange? What is the difference between np.linspace and np.arange? numpy numpy

What is the difference between np.linspace and np.arange?


np.linspace allows you to define how many values you get including the specified min and max value. It infers the stepsize:

>>> np.linspace(0,1,11)array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])

np.arange allows you to define the stepsize and infers the number of steps(the number of values you get).

>>> np.arange(0,1,.1)array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])

contributions from user2357112:

np.arange excludes the maximum value unless rounding error makes it do otherwise.

For example, the following results occur due to rounding error:

>>> numpy.arange(1, 1.3, 0.1)array([1. , 1.1, 1.2, 1.3])

You can exclude the stop value (in our case 1.3) using endpoint=False:

>>> numpy.linspace(1, 1.3, 3, endpoint=False)array([1. , 1.1, 1.2])