简体   繁体   中英

How to extract progression from an array with numpy?

I know that one can generate an (arithmetic) progression with numpy like so:

>>> import numpy as np
>>> np.arange(10, 60, 10)
array([10, 20, 30, 40, 50])

So my question is can I call something that will do the reverse ?

Eg :

>>> progression(np.array([10, 20, 30, 40, 50]))
(10, 60, 10)

This is most probably not trivial, but it would be nice if there's something in numpy/scipy that can do an approximation. Also bonus points if it can recognize the type of progression without specifying.

Note: I'm aware there are similar questions already asked, but this one specifically hasn't been answered, I think.

Since np.arange works with [start, end-step] with a step size. You can infer the parameters by hand:

def progression(x):
    step = x[1] - x[0]
    return x[0], x[-1] + step, step

This of course assumes the input array follows a sequential arangement!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM