Time series with 2 Values

42 Views Asked by At

I have a problem to solve with the time series. My data set looks like:

Date;hours;result
2021-01-01;180;2.78
2021-01-01;196;2.68
2021-01-01;170;2.53
2021-01-01;181;2.71
2021-01-01;169;2.43
2021-01-01;201;2.89

What would be the best approach to estimate the number of hours for the next day, to achieve the highest result?

I was thinking about Random Walk for Times Series but i have no clue how can I force the algorith to combine all 3 factors. In all examples i found there is always prediction f(x).

1

There are 1 best solutions below

0
mozway On

The question is unclear, especially regarding the part on the next day.

Assuming you want to find the hours needed to obtain the maximum result for the current day, using a polynomial regression:

import numpy as np

def hour_max(g, deg=2):
    p = np.polynomial.Polynomial.fit(df['hours'], df['result'], deg=deg)
    x, y = p.linspace()
    idx = y.argmax()
    return x[idx]

df.groupby('Date').apply(hour_max)

Output:

Date
2021-01-01    195.828283
dtype: float64

If you also want to have a visual:

def hour_max(g, deg=2, plot=False):
    p = np.polynomial.Polynomial.fit(df['hours'], df['result'], deg=deg)
    x, y = p.linspace()
    idx = y.argmax()
    
    if plot:
        ax = g.plot.scatter(x='hours', y='result')
        ax.plot(x, y)
        ax.plot(x[idx], y[idx], marker='o')
        ax.set_title(g.name)
    
    return x[idx]

df.groupby('Date').apply(hour_max, plot=True)

Output:

Date
2021-01-01    195.828283
dtype: float64

Image:

polynomial regression