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?
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])
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With