TSModelInference

class ibm_watsonx_ai.foundation_models.inference.TSModelInference(model_id, params=None, credentials=None, project_id=None, space_id=None, verify=None, api_client=None)[source]

Bases: WMLResource

Instantiate the time series model interface

Parameters:
  • model_id (str) – type of model to use

  • params (dict, TSForecastParameters, optional) – parameters to use during request generation

  • credentials (Credentials or dict, optional) – credentials for the Watson Machine Learning instance

  • project_id (str, optional) – ID of the Watson Studio project

  • space_id (str, optional) – ID of the Watson Studio space

  • verify (bool or str, optional) –

    You can pass one of the following as verify:

    • the path to a CA_BUNDLE file

    • the path of directory with certificates of trusted CAs

    • True - default path to truststore will be taken

    • False - no verification will be made

  • api_client (APIClient, optional) – initialized APIClient object with a set project ID or space ID. If passed, credentials and project_id/space_id are not required.

Example:

from ibm_watsonx_ai import Credentials
from ibm_watsonx_ai.foundation_models import TSModelInference

forecasting_params = {
    "prediction_length": 10
}

ts_model = TSModelInference(
    model_id="<TIME SERIES MODEL>",
    params=forecasting_params,
    credentials=Credentials(
        api_key = "***",
        url = "https://us-south.ml.cloud.ibm.com"),
    project_id=project_id
)
forecast(data, params=None, **kwargs)[source]

Generates a forecast based on the provided data and model parameters.

Parameters:
  • data (dict, pd.DataFrame, required) – A payload of data matching the schema provided. For more information about the data limitation see the product documentation https://cloud.ibm.com/apidocs/watsonx-ai.

  • params (dict, TSForecastParameters, optional) – Contains basic metadata about your time series data input. These metadata are used by the server to understand which field represents a time stamp or which are unique identifiers for separating time series from different input channels.

Example:

# number of elements in the array for each field must be at least 512, 1024, or 1536 depending on the model; for example 512 for ibm/granite-ttm-512-96-r2
data = {
        "date": [
            "2017-10-02T16:00:00",
            "2017-10-02T17:00:00",
            "2017-10-02T18:00:00"
            ...
        ],
        "HUFL": [
            1.1,
            2.2,
            3.3
            ...
        ]
    }

params =  {
    "timestamp_column": "date",
    "target_columns": [
        "HUFL",
    ],
    "prediction_length": 10
    "freq": "1h"
},

# The number of elements in the array for each field must be the prediction length of the model depending on the model; for example 96 for ibm/granite-ttm-512-96-r2,

response = ts_model.forecast(data=data, params=params)

# Print all response
print(response)

Enums

class TimeSeriesModels

Bases: StrEnum

This represents a dynamically generated Enum for Time Series Foundation Models.

Example of getting TimeSeriesModels:

# GET TimeSeriesModels ENUM
client.foundation_models.TimeSeriesModels

# PRINT dict of Enums
client.foundation_models.TimeSeriesModels.show()

Example Output:

{'GRANITE_TTM_1024_96_R2': 'ibm/granite-ttm-1024-96-r2',
 'GRANITE_TTM_1536_96_R2': 'ibm/granite-ttm-1536-96-r2',
 'GRANITE_TTM_512_96_R2': 'ibm/granite-ttm-512-96-r2'}

Example of initialising ModelInference with TimeSeriesModels Enum:

from ibm_watsonx_ai.foundation_models import TSModelInference

model = TSModelInference(
    model_id=client.foundation_models.TimeSeriesModels.GRANITE_TTM_1024_96_R2,
    credentials=Credentials(...),
    project_id=project_id,
)