np.linspace allows you to define how many values you get including the specified min and max value. It infers the stepsize:
>>> np.linspace(0,1,11)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
np.arange allows you to define the stepsize and infers the number of steps(the number of values you get).
>>> np.arange(0,1,.1)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
contributions from user2357112:
np.arange excludes the maximum value unless rounding error makes it do otherwise.
For example, the following results occur due to rounding error:
>>> numpy.arange(1, 1.3, 0.1)
array([1. , 1.1, 1.2, 1.3])
You can exclude the stop value (in our case 1.3) using endpoint=False:
>>> numpy.linspace(1, 1.3, 3, endpoint=False)
array([1. , 1.1, 1.2])
Answer from warped on Stack Overflowpython - What is the difference between np.linspace and np.arange? - Stack Overflow
python - Creating numpy linspace out of datetime - Stack Overflow
Python Numpy Linspace function for bidimensional array - Stack Overflow
python - Why does np.linspace(1,5,5, dtype = int, endpoint=False) result in an array that contains 1 twice? - Stack Overflow
np.linspace allows you to define how many values you get including the specified min and max value. It infers the stepsize:
>>> np.linspace(0,1,11)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
np.arange allows you to define the stepsize and infers the number of steps(the number of values you get).
>>> np.arange(0,1,.1)
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
contributions from user2357112:
np.arange excludes the maximum value unless rounding error makes it do otherwise.
For example, the following results occur due to rounding error:
>>> numpy.arange(1, 1.3, 0.1)
array([1. , 1.1, 1.2, 1.3])
You can exclude the stop value (in our case 1.3) using endpoint=False:
>>> numpy.linspace(1, 1.3, 3, endpoint=False)
array([1. , 1.1, 1.2])
np.arange(start, stop, step)
np.linspace(start,stop,number)
Example:
np.arange(0,10,2) o/p --> array([0,2,4,6,8])
np.linspace(0,10,2) o/p --> array([0., 10.])
Update - 2022
As pointed out by @Joooeey and @Ehtesh Choudhury, pandas now has date_range, which makes creating numpy.linspace-like time series much simpler.
t = pd.date_range(
start='2022-03-10',
end='2022-03-15',
periods=5
)
If it's important to have this time series as a numpy array, simply
>>> t.values
array(['2022-03-10T00:00:00.000000000', '2022-03-11T06:00:00.000000000',
'2022-03-12T12:00:00.000000000', '2022-03-13T18:00:00.000000000',
'2022-03-15T00:00:00.000000000'], dtype='datetime64[ns]')
Original answer
Have you considered using pandas? Using an approach from this possible duplicate question, you can make use of np.linspace in the following way
import pandas as pd
start = pd.Timestamp('2015-07-01')
end = pd.Timestamp('2015-08-01')
t = np.linspace(start.value, end.value, 100)
t = pd.to_datetime(t)
To obtain an np.array of the linear timeseries
In [3]: np.asarray(t)
Out[3]:
array(['2015-06-30T17:00:00.000000000-0700',
'2015-07-01T00:30:54.545454592-0700',
'2015-07-01T08:01:49.090909184-0700',
...
'2015-07-31T01:58:10.909090816-0700',
'2015-07-31T09:29:05.454545408-0700',
'2015-07-31T17:00:00.000000000-0700'], dtype='datetime64[ns]')
As of pandas 0.23 you can use date_range:
import pandas as pd
x = pd.date_range(min(dates), max(dates), periods=500).to_pydatetime()
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])
If n > a - b + 1, it is impossible for np.linspace(a, b, n, dtype=int) to give unique values. If n < a - b + 1, the inner values may undesirably be rounded down (e.g. 3.9 -> 3).
To get a more evenly-spaced array, use this instead:
np.linspace(a, b, n).round().astype(int)