简体   繁体   中英

Maximal Growth Period in time series

I am trying to find the key idea behind this problem:

I Am given the following input in coordinates x,y:

0,2 3,0 4,5 7,8 8,6 9,5 13,6 15,9 17,10 21,8
1,3 3,4 5,9 7,5 10,2 11,4 20,10
0,0 6,6 12,3 19,6

Graphically correspond to the following: 图形

Using Python, I need to find the maximal growth interval of all the n functions that are passed with the stdin.

Until now I managed to get the following output:

[['0', '2', '-'], ['3', '0', '+'], ['4', '5'], ['7', '8', '-'], ['8', '6'], ['9', '5', '+'], ['13', '6'], ['15', '9'], ['17', '10', '-'], ['21', '8']]
[['1', '3', '+'], ['3', '4'], ['5', '9', '-'], ['7', '5'], ['10', '2', '+'], ['11', '4'], ['20', '10']]
[['0', '0', '+'], ['6', '6', '-'], ['12', '3', '+'], ['19', '6']]

which are the section where the function are growing.

But now I am having big troubles to understand how to do the next step, which is basically comparing the functions together in order to get the maximum growth interval.

Any suggestion will be appreciated!

You could use numpy .

Import numpy and create make the datapoints into an array.

In [1]: import numpy as np

In [2]: a = np.array([(0,2), (3,0), (4,5), (7,8), (8,6), (9,5), (13,6), (15,9), (17,10), (21,8)])

In [3]: a
Out[3]: 
array([[ 0,  2],
       [ 3,  0],
       [ 4,  5],
       [ 7,  8],
       [ 8,  6],
       [ 9,  5],
       [13,  6],
       [15,  9],
       [17, 10],
       [21,  8]])

Find the difference between the sequential points in x and y;

In [4]: diff = a[1:] - a[:-1]

In [5]: diff
Out[5]: 
array([[ 3, -2],
       [ 1,  5],
       [ 3,  3],
       [ 1, -2],
       [ 1, -1],
       [ 4,  1],
       [ 2,  3],
       [ 2,  1],
       [ 4, -2]])

Separate and divide to create the slope;

In [6]: dx, dy = np.hsplit(diff, 2)

In [7]: slope = dy/dx

In [8]: slope
Out[8]: 
array([[-0.66666667],
       [ 5.        ],
       [ 1.        ],
       [-2.        ],
       [-1.        ],
       [ 0.25      ],
       [ 1.5       ],
       [ 0.5       ],
       [-0.5       ]])

As you can see, the maximum slope is slope[1] , which equals 5. This is the second element of the slope array, so this occurs between the second and third element of a;

In [8]: a[1:3]
Out[8]: 
array([[3, 0],
       [4, 5]])

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