Reputation: 10791
We're all familiar with np.linspace
, which creates an array given a start
, stop
, and num
of elements:
In [1]: import numpy as np
In [2]: np.linspace(0, 10, 9)
Out[2]: array([ 0. , 1.25, 2.5 , 3.75, 5. , 6.25, 7.5 , 8.75, 10. ])
Likewise, who could ever forget np.arange
, which creates an array given a start
, stop
, and step
:
In [4]: np.arange(0, 10, 1.25)
Out[4]: array([ 0. , 1.25, 2.5 , 3.75, 5. , 6.25, 7.5 , 8.75])
But is there a function that allows you to specify a start
, step
, and num
of elements, while omitting the stop
? There should be.
Upvotes: 63
Views: 93794
Reputation: 10626
The shortest and most elegant way (from my perspective) is:
import numpy as np
start = 0
step = 1.25
num = 9
result = start + np.arange(0, num) * step
returns
[ 0. 1.25 2.5 3.75 5. 6.25 7.5 8.75 10. ]
Upvotes: 60
Reputation: 966
Some of the other solutions didn't work for me, so since I was already comfortable using np.linspace
I decided to throw together a function that replaces linspace
's num
with a step
argument.
def linspace(start, stop, step=1.):
"""
Like np.linspace but uses step instead of num
This is inclusive to stop, so if start=1, stop=3, step=0.5
Output is: array([1., 1.5, 2., 2.5, 3.])
"""
return np.linspace(start, stop, int((stop - start) / step + 1))
An example output:
linspace(9.5, 11.5, step=.5)
array([ 9.5, 10. , 10.5, 11. , 11.5])
Edit: I misread the question, the original question wanted a function that omitted the stop
argument. I'll still leave this here, as I think it could be useful to some who stumble upon this question as it's the only one I've found that's similar to my original question of finding a function with start
, stop
, and step
, rather than num
Upvotes: 7
Reputation: 231738
A deleted answer pointed out that linspace
takes an endpoint
parameter.
With that, 2 examples given in other answers can be written as:
In [955]: np.linspace(0, 0+(0.1*3),3,endpoint=False)
Out[955]: array([ 0. , 0.1, 0.2])
In [956]: np.linspace(0, 0+(5*3),3,endpoint=False)
Out[956]: array([ 0., 5., 10.])
In [957]: np.linspace(0, 0+(1.25*9),9,endpoint=False)
Out[957]: array([ 0. , 1.25, 2.5 , 3.75, 5. , 6.25, 7.5 , 8.75, 10. ])
Look at the functions defined in numpy.lib.index_tricks
for other ideas on how to generate ranges and/or grids. For example, np.ogrid[0:10:9j]
behaves like linspace
.
def altspace(start, step, count, endpoint=False, **kwargs):
stop = start+(step*count)
return np.linspace(start, stop, count, endpoint=endpoint, **kwargs)
Upvotes: 10
Reputation: 2074
Here's one that should always work with floats.
>>> import numpy as np
>>> import itertools
>>> def my_range(start, step, num):
... return np.fromiter(itertools.count(start, step), np.float, num)
...
>>> my_range(0, 0.1, 3)
array([ 0. , 0.1, 0.2])
You could make the np.float
an arg (or kwarg) if you want to use it with something other than floats:
>>> import numpy as np
>>> import itertools
>>> def my_range(start, step, num, dtype=np.float):
... return np.fromiter(itertools.count(start, step), dtype, num)
...
>>> my_range(0, 5, 3)
array([ 0., 5., 10.])
>>> my_range(0, 5, 3, dtype=np.int)
array([ 0, 5, 10])
Upvotes: 2
Reputation: 114098
def by_num_ele(start,step,n_elements):
return numpy.arange(start,start+step*n_elements,step)
maybe?
Upvotes: 5