Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 927
dbliss Avatar asked Aug 04 '15 21:08

dbliss


People also ask

Which function provided by NumPy to create sequence of numbers?

arange() NumPy arange() is one of the array creation routines based on numerical ranges. It creates an instance of ndarray with evenly spaced values and returns the reference to it.

What is Linspace function in NumPy?

The numpy. linspace() function returns number spaces evenly w.r.t interval.

What is the difference between Range () and arange () functions in Python?

For using the range(), you do not have to install any module. For using the arange(), you have to install the NumPy module. This comes as a built-in default function with the Python interpreter. This comes as a third-party module-based function.

What is the difference between Linspace and arange?

linspace allow you to define the number of steps. linspace(0,1,20) : 20 evenly spaced numbers from 0 to 1 (inclusive). arange(0, 10, 2) : however many numbers are needed to go from 0 to 10 (exclusive) in steps of 2. The big difference is that one uses a step value, the other a count .


2 Answers

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

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

returns

[  0.     1.25   2.5    3.75   5.     6.25   7.5    8.75  10.  ] 
like image 122
Markus Dutschke Avatar answered Sep 23 '22 01:09

Markus Dutschke


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) 
like image 21
hpaulj Avatar answered Sep 25 '22 01:09

hpaulj