-
-
Notifications
You must be signed in to change notification settings - Fork 12k
Closed
Labels
Description
Take a look at the following:
>>> np.linspace(0,10,num=50,endpoint=False,dtype=np.int32)
array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4,
4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8,
8, 9, 9, 9, 9, 9], dtype=int32)
>>> np.linspace(-1,9,num=50,endpoint=False,dtype=np.int32)
array([-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5,
5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8],
dtype=int32)
As you can see, linspace creates a nice linear integer case in the first case. Note so much if the starting point is negative. IMHO, linspace should round towards -infinity when negative numbers are involved.
As a workaround, one can use the following which has the desired output:
>>> np.linspace(0,10,num=50,endpoint=False,dtype=np.int32)-1
array([-1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5,
5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8],
dtype=int32)