-1

I want create a list from 0 to 1 with step 0.05, the result will like this: [0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1]

I try with following code, but the output seems not correct. Anyone could help? Thanks.

print(np.arange(0, 1, 0.05).tolist())

Output:

[0.0, 0.05, 0.1, 0.15000000000000002, 0.2, 0.25, 0.30000000000000004, 0.35000000000000003, 0.4, 0.45, 0.5, 0.55, 0.6000000000000001, 0.65, 0.7000000000000001, 0.75, 0.8, 0.8500000000000001, 0.9, 0.9500000000000001]
ah bon
  • 9,293
  • 12
  • 65
  • 148

3 Answers3

1

Its not necessary use .tolist().

Try this:

a = np.arange(0, 1, 0.05)
print (a)

Output:

[0.   0.05 0.1  0.15 0.2  0.25 0.3  0.35 0.4  0.45 0.5  0.55 0.6  0.65
 0.7  0.75 0.8  0.85 0.9  0.95]
SSR
  • 137
  • 9
  • OK, but I need to convert to a list anyway. – ah bon Feb 13 '20 at 01:42
  • a = list(np.arange(0, 1, 0.05)) print (type(a)) – SSR Feb 13 '20 at 07:01
  • Convert the result to list, you can check with type. – SSR Feb 13 '20 at 07:02
  • With your code I get, `[0.0, 0.05, 0.1, 0.15000000000000002, 0.2, 0.25, 0.30000000000000004, 0.35000000000000003, 0.4, 0.45, 0.5, 0.55, 0.6000000000000001, 0.65, 0.7000000000000001, 0.75, 0.8, 0.8500000000000001, 0.9, 0.9500000000000001]` – ah bon Feb 13 '20 at 07:04
  • 1
    Thats strange, if know the number of decimals you could round – SSR Feb 13 '20 at 07:05
  • Yes, this `np.arange(0, 1, 0.05).round(2).tolist()` or `list(np.arange(0, 1, 0.05).round(2))` solved my problem. – ah bon Feb 13 '20 at 07:08
  • If you find out the technical reason of that comment – SSR Feb 13 '20 at 07:10
  • 1
    https://stackoverflow.com/questions/47243190/numpy-arange-how-to-make-precise-array-of-floats – ah bon Feb 13 '20 at 07:14
  • https://stackoverflow.com/questions/53580811/np-arange-does-not-work-as-expected-with-floating-point-arguments – ah bon Feb 13 '20 at 07:14
1

You want np.linspace()

np.linspace(0, 1, 21)
Out[]: 
array([0.  , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.5 ,
       0.55, 0.6 , 0.65, 0.7 , 0.75, 0.8 , 0.85, 0.9 , 0.95, 1.  ])
Daniel F
  • 13,620
  • 2
  • 29
  • 55
  • Hi Daniel. `np.linspace(0, 1, 21)[3]` still gets me `0.15000000000000002`. It appears as `0.15` when outputting the entire array, like in your example, but isn't that just an artifact of how it's summarized in the output? I don't see how `linspace` should be able to do any magic that deals with floating point precision issues. I believe you'll still need to do something like `np.around(np.linspace(0, 1, 21)[3], 3)` to deal with this. What am I missing? – Janus Varmarken May 20 '21 at 21:42
  • Checking the history, it seems I answered within the "invisible edit" time window for the question. I can't imagine I gave this answer to the question as asked now. For dealing with the floating point errors, your solution (or the self-answer below) would probably be best. – Daniel F May 21 '21 at 09:25
  • Makes sense, thank you for the clarification! – Janus Varmarken May 21 '21 at 16:32
0

This works:

print(np.arange(0, 1, 0.05).round(2).tolist())

Output:

[0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95]
ah bon
  • 9,293
  • 12
  • 65
  • 148