Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create numpy array start from 0 to 1 with increment 0.1

I’m studying in python. I want to create numpy array in python, start from 0 to 1 with increment 0.1. This is the code:

import numpy as np
t=np.arange(0, 1, 0.1)
print(t)

the result is

[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]

But "1" is not include in array, how to include 1 in array? Does we manually adding 1 to array, or any other ways?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As stated in the np.arange dccumentation

arange(stop): Values are generated within the half-open interval
[0, stop) (in other words, the interval including start but
excluding stop).

arange(start, stop): Values are generated within the half-open interval [start, stop).

arange(start, stop, step) Values are generated within the half-open interval [start, stop), with spacing between values given by
step.

So, to include 1, just change to np.arange(0, 1.1, 0.1) (any value
greater than 1 and less or equal with 1.1 would work)

>>> np.arange(0, 1.1, 0.1)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading