I have always used np.arange
. I recently came across np.linspace
. I am wondering what exactly is the difference between them... Looking at their documentation:
Return evenly spaced values within a given interval.
Return evenly spaced numbers over a specified interval.
The only difference I can see is linspace
having more options... Like choosing to include the last element.
Which one of these two would you recommend and why? And in which cases is np.linspace
superior?
linspace(0,1,20)
: 20 evenly spaced numbers from 0 to 1 (inclusive).arange(0, 10, 2)
: however many numbers are needed to go from 0 to 10 (exclusive) in steps of 2.step
value, the other acount
.arange
follows the behavior of the pythonrange
, and is best for creating an array of integers. It's docs recommendlinspace
for floats.np.linspace(start=0,stop=1,num=5)
andnp.arange(start=0,stop=1,step=0.25)
.