Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does np.linspace(1,5,5, dtype = int, endpoint=False) result in an array that contains 1 twice?

Tags:

python

numpy

While looking to replicate:

In [61]: np.arange(0,5)
Out[61]: array([0, 1, 2, 3, 4])

using np.linspace(), I observed:

In [70]: np.linspace(1,5,5, dtype = int, endpoint=False)
Out[70]: array([1, 1, 2, 3, 4])

Why does np.linspace() include the value 1 twice in this case?

like image 303
Pyderman Avatar asked Jan 07 '23 11:01

Pyderman


2 Answers

because linspace is defined on floats and returns in your case:

np.linspace(1,5,5, endpoint=False)
array([ 1. ,  1.8,  2.6,  3.4,  4.2])

then using int as dtype just rounds down giving you the result with two 1:

array([ 1 ,  1,  2,  3,  4])

Btw: Using np.arange might be better suited if you want to create arrays containing integer:

np.arange(1,5)
array([ 1,  2,  3,  4])
like image 97
MSeifert Avatar answered Jan 10 '23 22:01

MSeifert


I can't test because dtype was introduced in 1.9 and I have 1.8. Without dtype, I get:

np.linspace(1,5,5, endpoint=False)
array([ 1. ,  1.8,  2.6,  3.4,  4.2])

Then

array([1, 1, 2, 3, 4])

is a round down of this.

You'd get what you expect using

np.linspace(1,5,5, dtype=int)

but as suggested in another answer, you'd be better off using np.arange().

like image 44
Jérôme Avatar answered Jan 11 '23 00:01

Jérôme