Is there a numpy function that allows you to specify start, step, and number? Is there a numpy function that allows you to specify start, step, and number? numpy numpy

Is there a numpy function that allows you to specify start, step, and number?


Thanks for that question. I had the same issue. The (from my perspective) shortest and most elegant way is:

import numpy as npstart=0step=1.25num=9result=np.arange(0,num)*step+startprint(result)

returns

[  0.     1.25   2.5    3.75   5.     6.25   7.5    8.75  10.  ]


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)


def by_num_ele(start,step,n_elements):    return numpy.arange(start,start+step*n_elements,step)

maybe?