Python Forum

Full Version: output shape problem with np.arange
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When I generate an array using np.arange(start=0, stop= 29 * 4.42, step=4.42), the output turns out to be an array of size (30,) where it should be of size (29,). (While the numbers I use here seems to be hard-coded, they are necessarily in that way)

What I notice is that it only happens when I use a step size of 4.42 and make it stop at 29*4.42, which is 128.18. A slight tweak to either of these two number would make things right again, gernerating a array of size (29,) as it should be.

I am wondering if it is because 128.18 is so close to 128, which is 2^7, so it causes certain errors regarding to storage size of the variables? Any thought or insight would be appreciated. Thx!!

For reference, my numpy version is 1.21.5. And the problem is reproduced as in the following codes:
step = 4.42
a = np.arange(0, 29*step, step)
print(a)
print(a.shape)
Why not use np.linspace(0, 28 * step, 29) which lets you state explicitly the size of the resulting array independently from the rounding issues of the floating numbers ?
(Dec-23-2023, 11:19 AM)Gribouillis Wrote: [ -> ]Why not use np.linspace(0, 28 * step, 29) which lets you state explicitly the size of the resulting array independently from the rounding issues of the floating numbers ?

Yes linspace seems to be a better choice when you play with decimal values. I just wonder if the rounding error in arange unsolvable in this scenario. These errors are such a slient killer.
You could do
>>> step = 4.42
>>> a = np.arange(0, (29 - 0.5) * step, step)
>>> a.shape
(29,)
>>> 
Removing half the step ensures that the upper bound is not in the array.
(Dec-23-2023, 12:57 PM)Gribouillis Wrote: [ -> ]You could do
>>> step = 4.42
>>> a = np.arange(0, (29 - 0.5) * step, step)
>>> a.shape
(29,)
>>> 
Removing half the step ensures that the upper bound is not in the array.

That would be a good workaround. Except when the rounding digit causing the error is already larger than half of the step? May try and dig into that in spare time. For now, linspace seems to be the best move. Thank you for your help.
Quote:When I generate an array using np.arange(start=0, stop= 29 * 4.42, step=4.42), the output turns out to be an array of size (30,) where it should be of size (29,). (While the numbers I use here seems to be hard-coded, they are necessarily in that way)
You aren't thinking about the problem correctly. Your first step takes you from 0 to 4.42. After just one step you already have two values, and you will always have one more value than the number of steps.