Python Forum
output shape problem with np.arange
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
output shape problem with np.arange
#1
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)
Reply
#2
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 ?
alan6690 likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
(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.
Reply
#4
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.
alan6690 likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
(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.
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in output of a snippet code akbarza 2 385 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  problem in output of a function akbarza 9 1,209 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 936 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Facing problem with Pycharm - Not getting the expected output amortal03 1 863 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  simplekml change shape&color issac_n 2 2,848 Aug-20-2022, 07:15 PM
Last Post: Joseph_Paintsil
  Can't properly shape an array maaaa2401 3 2,336 Dec-18-2020, 09:32 AM
Last Post: maaaa2401
  single input infinite output problem Chase91 2 1,956 Sep-23-2020, 10:01 PM
Last Post: Chase91
  ValueError: shape mismatched: objects cannot be broadcast to a single shape Laplace12 0 4,523 Jul-14-2020, 11:45 AM
Last Post: Laplace12
  Receiving ValueError("bad input shape {0}".format(shape)) error SuryaCitizen 2 3,516 Jun-01-2020, 06:45 AM
Last Post: pyzyx3qwerty
  Fill a value in triangle shape in Matrix lynx 0 1,887 Dec-07-2019, 06:32 AM
Last Post: lynx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020