xxxstack
xxxstack

Reputation: 11

Why does numpy linspace() function result in equally-spaced float values instead of integers?

vec4=np.linspace(0,100,10)
print(vec4)

Running this results in

[  0.          11.11111111  22.22222222  33.33333333  44.44444444
  55.55555556  66.66666667  77.77777778  88.88888889 100.        ]

Why is this not giving integers? I was expecting this below

[1    2    3    4    ..so on]

Upvotes: 1

Views: 1159

Answers (1)

Simon Hawe
Simon Hawe

Reputation: 4529

When you call linspace like np.linspace(start,stop,n_elements), you are telling numpy to create an array of length n_elements that have an equal distance and that include start and stop. Due to including start and stop, the space/distance is equal to `(stop-start)/(n_elements - 1)? which should explain the numbers you got.

If you just want integers, you can use np.arange(start, end, step), however, this would not include end. Or, for you example, you can do np.linspace(0, 100, 11).astype(int)

Upvotes: 2

Related Questions