mirror of
https://github.com/saymrwulf/prophet.git
synced 2026-11-18 08:53:42 +00:00
Speed up fourier_series (#2334)
This commit is contained in:
parent
5e2221f480
commit
e2b4ef3a8e
1 changed files with 21 additions and 14 deletions
|
|
@ -9,11 +9,12 @@ from __future__ import absolute_import, division, print_function
|
||||||
import logging
|
import logging
|
||||||
from collections import OrderedDict, defaultdict
|
from collections import OrderedDict, defaultdict
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from datetime import timedelta, datetime
|
from datetime import timedelta
|
||||||
from typing import Dict, List
|
from typing import Dict, List, Union
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
from numpy.typing import NDArray
|
||||||
|
|
||||||
from prophet.make_holidays import get_holiday_names, make_holidays_df
|
from prophet.make_holidays import get_holiday_names, make_holidays_df
|
||||||
from prophet.models import StanBackendEnum
|
from prophet.models import StanBackendEnum
|
||||||
|
|
@ -21,7 +22,7 @@ from prophet.plot import (plot, plot_components)
|
||||||
|
|
||||||
logger = logging.getLogger('prophet')
|
logger = logging.getLogger('prophet')
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
|
NANOSECONDS_TO_SECONDS = 1000 * 1000 * 1000
|
||||||
|
|
||||||
class Prophet(object):
|
class Prophet(object):
|
||||||
"""Prophet forecaster.
|
"""Prophet forecaster.
|
||||||
|
|
@ -421,7 +422,11 @@ class Prophet(object):
|
||||||
self.changepoints_t = np.array([0]) # dummy changepoint
|
self.changepoints_t = np.array([0]) # dummy changepoint
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fourier_series(dates, period, series_order):
|
def fourier_series(
|
||||||
|
dates: pd.Series,
|
||||||
|
period: Union[int, float],
|
||||||
|
series_order: int,
|
||||||
|
) -> NDArray[np.float_]:
|
||||||
"""Provides Fourier series components with the specified frequency
|
"""Provides Fourier series components with the specified frequency
|
||||||
and order.
|
and order.
|
||||||
|
|
||||||
|
|
@ -435,17 +440,19 @@ class Prophet(object):
|
||||||
-------
|
-------
|
||||||
Matrix with seasonality features.
|
Matrix with seasonality features.
|
||||||
"""
|
"""
|
||||||
|
if not (series_order >= 1):
|
||||||
|
raise ValueError("series_order must be >= 1")
|
||||||
|
|
||||||
# convert to days since epoch
|
# convert to days since epoch
|
||||||
t = np.array(
|
t = dates.to_numpy(dtype=int) // NANOSECONDS_TO_SECONDS / (3600 * 24.)
|
||||||
(dates - datetime(1970, 1, 1))
|
|
||||||
.dt.total_seconds()
|
x_T = t * np.pi * 2
|
||||||
.astype(float)
|
fourier_components = np.empty((dates.shape[0], 2 * series_order))
|
||||||
) / (3600 * 24.)
|
for i in range(series_order):
|
||||||
return np.column_stack([
|
c = x_T * (i + 1) / period
|
||||||
fun((2.0 * (i + 1) * np.pi * t / period))
|
fourier_components[:, 2 * i] = np.sin(c)
|
||||||
for i in range(series_order)
|
fourier_components[:, (2 * i) + 1] = np.cos(c)
|
||||||
for fun in (np.sin, np.cos)
|
return fourier_components
|
||||||
])
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def make_seasonality_features(cls, dates, period, series_order, prefix):
|
def make_seasonality_features(cls, dates, period, series_order, prefix):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue