1

I want to create a numpy array.

T = 200

I want to create an array from 0 to 199, in which each value will be divided by 200.

l = [0, 1/200, 2/200, ...]

Numpy have any such method for calculation?

4 Answers 4

3

Alternatively one can use linspace:

>>> np.linspace(0, 1., 200, endpoint=False)
array([ 0.   ,  0.005,  0.01 ,  0.015,  0.02 ,  0.025,  0.03 ,  0.035,
        0.04 ,  0.045,  0.05 ,  0.055,  0.06 ,  0.065,  0.07 ,  0.075,
          ...
        0.92 ,  0.925,  0.93 ,  0.935,  0.94 ,  0.945,  0.95 ,  0.955,
        0.96 ,  0.965,  0.97 ,  0.975,  0.98 ,  0.985,  0.99 ,  0.995])
Sign up to request clarification or add additional context in comments.

Comments

2

Use np.arange:

>>> import numpy as np  
>>> np.arange(200, dtype=np.float)/200
array([ 0.   ,  0.005,  0.01 ,  0.015,  0.02 ,  0.025,  0.03 ,  0.035,
        0.04 ,  0.045,  0.05 ,  0.055,  0.06 ,  0.065,  0.07 ,  0.075,
        0.08 ,  0.085,  0.09 ,  0.095,  0.1  ,  0.105,  0.11 ,  0.115,
        ...
        0.88 ,  0.885,  0.89 ,  0.895,  0.9  ,  0.905,  0.91 ,  0.915,
        0.92 ,  0.925,  0.93 ,  0.935,  0.94 ,  0.945,  0.95 ,  0.955,
        0.96 ,  0.965,  0.97 ,  0.975,  0.98 ,  0.985,  0.99 ,  0.995])

Comments

1
T = 200.0
l = [x / float(T) for x in range(200)]

Comments

1
import numpy as np
T = 200
np.linspace(0.0, 1.0 - 1.0 / float(T), T)

Personally I prefer linspace for creating evenly spaced arrays in general. It is more complex in this case as the endpoint depends on the number of points T.

1 Comment

If you have a recent enough numpy, you can use the endpoint argument of linspace. See @alko's answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.