forecastingapi.forecastingapi
1import requests 2from .config import BASE_URL 3 4 5def get_forecast( 6 path_to_file, 7 base_model="RidgeCV", 8 n_hidden_features=5, 9 lags=25, 10 type_pi="gaussian", 11 replications=None, 12 h=10, 13): 14 """ 15 Get a forecast from the Techtonique API. 16 17 Parameters: 18 ----------- 19 20 path_to_file : str 21 Path to the input file or URL containing the time series data. 22 23 base_model : str 24 Forecasting method to use (default is "RidgeCV"); for now scikit-learn model names. 25 26 n_hidden_features : int 27 Number of hidden features for the model (default is 5). 28 29 lags : int 30 Number of lags to use in the model (default is 25). 31 32 type_pi : str 33 Type of prediction interval to use (default is 'gaussian'). 34 35 replications : int 36 Number of replications for certain methods (default is None). 37 38 h : int 39 Forecast horizon (default is 10). 40 41 Returns: 42 -------- 43 dict 44 A dictionary containing the forecast results (mean, lower bound, upper bound, and simulations). 45 46 Example: 47 -------- 48 >>> from forecastingapi import get_forecast 49 >>> # path to your local timeseries data (examples in https://github.com/Techtonique/datasets/tree/main/time_series) 50 >>> file_path = "path/to/your/timeseries/data.csv" 51 >>> forecast = get_forecast(file_path, h=15) 52 >>> print(forecast) 53 """ 54 55 token = input("Enter your token (from https://www.techtonique.net/token): ") 56 57 headers = { 58 'Authorization': 'Bearer ' + token, 59 } 60 61 params = { 62 'base_model': str(base_model), 63 'n_hidden_features': str(n_hidden_features), 64 'lags': str(lags), 65 'type_pi': str(type_pi), 66 'replications': str(replications), 67 'h': str(h), 68 } 69 70 files = { 71 'file': (path_to_file, read_file_or_url(path_to_file), 'text/csv'), 72 } 73 74 response = requests.post(BASE_URL + '/forecasting', 75 params=params, headers=headers, files=files) 76 77 return response.json() 78 79 80def read_file_or_url(path): 81 if path.startswith("http://") or path.startswith("https://"): 82 response = requests.get(path) 83 return response.content 84 else: 85 return open(path, "rb")
def
get_forecast( path_to_file, base_model='RidgeCV', n_hidden_features=5, lags=25, type_pi='gaussian', replications=None, h=10):
6def get_forecast( 7 path_to_file, 8 base_model="RidgeCV", 9 n_hidden_features=5, 10 lags=25, 11 type_pi="gaussian", 12 replications=None, 13 h=10, 14): 15 """ 16 Get a forecast from the Techtonique API. 17 18 Parameters: 19 ----------- 20 21 path_to_file : str 22 Path to the input file or URL containing the time series data. 23 24 base_model : str 25 Forecasting method to use (default is "RidgeCV"); for now scikit-learn model names. 26 27 n_hidden_features : int 28 Number of hidden features for the model (default is 5). 29 30 lags : int 31 Number of lags to use in the model (default is 25). 32 33 type_pi : str 34 Type of prediction interval to use (default is 'gaussian'). 35 36 replications : int 37 Number of replications for certain methods (default is None). 38 39 h : int 40 Forecast horizon (default is 10). 41 42 Returns: 43 -------- 44 dict 45 A dictionary containing the forecast results (mean, lower bound, upper bound, and simulations). 46 47 Example: 48 -------- 49 >>> from forecastingapi import get_forecast 50 >>> # path to your local timeseries data (examples in https://github.com/Techtonique/datasets/tree/main/time_series) 51 >>> file_path = "path/to/your/timeseries/data.csv" 52 >>> forecast = get_forecast(file_path, h=15) 53 >>> print(forecast) 54 """ 55 56 token = input("Enter your token (from https://www.techtonique.net/token): ") 57 58 headers = { 59 'Authorization': 'Bearer ' + token, 60 } 61 62 params = { 63 'base_model': str(base_model), 64 'n_hidden_features': str(n_hidden_features), 65 'lags': str(lags), 66 'type_pi': str(type_pi), 67 'replications': str(replications), 68 'h': str(h), 69 } 70 71 files = { 72 'file': (path_to_file, read_file_or_url(path_to_file), 'text/csv'), 73 } 74 75 response = requests.post(BASE_URL + '/forecasting', 76 params=params, headers=headers, files=files) 77 78 return response.json()
Get a forecast from the Techtonique API.
Parameters:
path_to_file : str Path to the input file or URL containing the time series data.
base_model : str Forecasting method to use (default is "RidgeCV"); for now scikit-learn model names.
n_hidden_features : int Number of hidden features for the model (default is 5).
lags : int Number of lags to use in the model (default is 25).
type_pi : str Type of prediction interval to use (default is 'gaussian').
replications : int Number of replications for certain methods (default is None).
h : int Forecast horizon (default is 10).
Returns:
dict A dictionary containing the forecast results (mean, lower bound, upper bound, and simulations).
Example:
>>> from forecastingapi import get_forecast
>>> # path to your local timeseries data (examples in https://github.com/Techtonique/datasets/tree/main/time_series)
>>> file_path = "path/to/your/timeseries/data.csv"
>>> forecast = get_forecast(file_path, h=15)
>>> print(forecast)
def
read_file_or_url(path):