Docs /Templates/ sinapsis-sktime

KNeighborsTimeSeriesClassifierSKTimeWrapper

sinapsis-sktimeSKTimedynamicinferencemodelsprediction

Python template · sinapsis_sktime.templates.sktime_classifiers

Overview

Template for SkTime classification model training and inference for the distance_based module.

KNeighborsTimeSeriesClassifier — inherited documentation

K-nearest neighbours Time Series Classifier.

An adapted version of the ``scikit-learn`` ``KNeighborsClassifier``, adapted for time series data.

This class is a KNN classifier which supports time series distance measures.

Time series distances are passed as the ``distance argument``, which can be:

* a string. This will substitute a hard-coded distance metric from ``sktime.dists_kernels._numba_distances``. These default distances are intended to be performant, but cannot deal with unequal length or multivariate series. * a ``sktime`` pairwise transformer. These are available in ``sktime.dists_kernels``, and can be discovered via ``registry.all_estimators`` by searching for ``pairwise-transformer`` type.and are composable first class citizens in the ``sktime`` framework. Distances dealing with unequal length or multivariate series are available, these can be discovered via ``capability:unequal_length`` and ``capability:multivariate`` tags. * a callable. The exact signature for callables is described below.

Parameters ---------- n_neighbors : int, optional, default = 1 k in "k nearest neighbours"

weights : {'uniform', 'distance'} or callable, default='uniform' Weight function used in prediction.

Possible values:

- 'uniform' : uniform weights. All points in each neighborhood are weighted equally. - 'distance' : weight points by the inverse of their distance. in this case, closer neighbors of a query point will have a greater influence than neighbors which are further away. - [callable] : a user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.

algorithm : str, optional. default = 'brute' search method for neighbours one of {'ball_tree', 'brute', 'brute_incr'}

* 'brute' precomputes the distance matrix and applies ``sklearn`` ``KNeighborsClassifier`` directly. This algorithm is not memory efficient as it scales with the size of the distance matrix, but may be more runtime efficient. * 'brute_incr' passes the distance to ``sklearn`` ``KNeighborsClassifier``, with ``algorithm='brute'``. This is useful for large datasets, for memory efficiency, as the distance is used incrementally, without precomputation. However, this may be less runtime efficient. * 'ball_tree' uses a ball tree to find the nearest neighbors, using ``KNeighborsClassifier`` from ``sklearn``. May be more runtime and memory efficient on mid-to-large datasets, however, the distance computation may be slower.

distance : str, sktime pairwise transformer, or callable, optional. default ='dtw' distance measure between time series

* if str, must be one of the following strings: 'euclidean', 'squared', 'dtw', 'ddtw', 'wdtw', 'wddtw', 'lcss', 'edr', 'erp', 'msm', 'twe' this will substitute a hard-coded distance metric from ``sktime.dists_kernels._numba_distances``. * if ``sktime`` pairwise transformer, must implement the ``pairwise-transformer`` interface. ``sktime`` transformers are available in ``sktime.dists_kernels``, and discoverable via ``registry.all_estimators`` by searching for ``pairwise-transformer`` type. * if non-class callable, parameters can be passed via distance_params Example: knn_dtw = KNeighborsTimeSeriesClassifier( distance='dtw', distance_params={'epsilon':0.1}) * if any callable, must be of signature ``(X: Panel, X2: Panel) -> np.ndarray``. The output must be mxn array if X is Panel of m Series, X2 of n Series; if ``distance_mtype`` is not set, must be able to take ``X``, ``X2`` which are of ``pd_multiindex`` and ``numpy3D`` mtype

distance_params : dict, optional, default = None. dictionary for distance parameters, in case that distance is a str or callable distance_mtype : str, or list of str optional. default = None. mtype that distance expects for X and X2, if a callable only set this if distance is not BasePairwiseTransformerPanel descendant pass_train_distances : bool, optional, default = False. Whether distances between training points are computed and passed to sklearn. Passing is superfluous for algorithm='brute', but may have impact otherwise.

leaf_size : int, optional, default=30 Leaf size passed to ``BallTree`` or ``KDTree``. This can affect the speed of the construction and query, as well as the memory required to store the tree. The optimal value depends on the nature of the problem.

n_jobs : int, optional, default=None The number of parallel jobs to run for neighbors search. ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context. ``-1`` means using all processors. Does not affect the ``fit`` method.

Examples -------- >>> from sktime.classification.distance_based import KNeighborsTimeSeriesClassifier >>> from sktime.datasets import load_unit_test >>> X_train, y_train = load_unit_test(return_X_y=True, split="train") >>> X_test, y_test = load_unit_test(return_X_y=True, split="test") >>> classifier = KNeighborsTimeSeriesClassifier(distance="euclidean") >>> classifier.fit(X_train, y_train) KNeighborsTimeSeriesClassifier(...) >>> y_pred = classifier.predict(X_test)

Capabilities

Capabilities: not set

Installation

Install the published package using pip or uv.

Terminalbash
pip install sinapsis-sktime==1.0.0
uv pip install sinapsis-sktime==1.0.0
uv add sinapsis-sktime==1.0.0

Configuration

Effective fields from the inspector schema, including nested Pydantic model fields.

metadata
sinapsis_core.template_base.base_models.TemplateMetadata | None
Optional
Default: null
root_dir
str
Optional
Default: "/sinapsis-hub/private/platform/sinapsis-hub/playground/publish"
model_save_pathRequired
Path where the trained model will be saved. kneighborstimeseriesclassifier_init n_neighbors <class 'int'>: default: 1 weights typing.Union[str, typing.Any, NoneType]: default: uniform algorithm <class 'str'>: default:… Read full field description

Path where the trained model will be saved. kneighborstimeseriesclassifier_init n_neighbors <class 'int'>: default: 1 weights typing.Union[str, typing.Any, NoneType]: default: uniform algorithm <class 'str'>: default: brute distance <class 'str'>: default: dtw distance_params typing.Optional[dict]: default: None distance_mtype typing.Optional[str]: default: None pass_train_distances <class 'bool'>: default: False leaf_size <class 'int'>: default: 30 n_jobs typing.Optional[int]: default: None

str
Required
Default: null
kneighborstimeseriesclassifier_initRequired
KNeighborsTimeSeriesClassifierAttributes
Required
Default: null
n_jobs
typing.Optional[int]
Optional
Default: null
weights
typing.Union[str, typing.Any, NoneType]
Optional
Default: "uniform"
distance
str
Optional
Default: "dtw"
algorithm
str
Optional
Default: "brute"
leaf_size
int
Optional
Default: 30
n_neighbors
int
Optional
Default: 1
distance_mtype
typing.Optional[str]
Optional
Default: null
distance_params
typing.Optional[dict]
Optional
Default: null
pass_train_distances
bool
Optional
Default: false

Configuration example

Generated from documented defaults. Replace any placeholders and complete required nested settings before running.

yaml
templates:
  - template_name: "KNeighborsTimeSeriesClassifierSKTimeWrapper"
    class_name: "KNeighborsTimeSeriesClassifierSKTimeWrapper"
    attributes:
      root_dir: "/sinapsis-hub/private/platform/sinapsis-hub/playground/publish"
      model_save_path: "<required>"
      kneighborstimeseriesclassifier_init:
        n_jobs: null
        weights: "uniform"
        distance: "dtw"
        algorithm: "brute"
        leaf_size: 30
        n_neighbors: 1
        distance_mtype: null
        distance_params: null
        pass_train_distances: false

Inheritance

Immediate parent: sinapsis_sktime.templates.sktime_classifiers.SKTimeDistanceClassifiers

Environment variables

Environment definitions belong to their canonical Python modules.

SINAPSIS_BUILD_DOCS · defined elsewhere