Knn
polars_ts.classification.knn
K-Nearest Neighbors classification for time series using precomputed distances.
TimeSeriesKNNClassifier
K-Nearest Neighbors time series classifier.
Parameters
k
Number of nearest neighbors. Default 3.
metric
Distance metric name (e.g. "dtw", "erp", "lcss"). Default "dtw".
**distance_kwargs
Extra keyword arguments forwarded to the distance function.
fit(df, *, label_col='label')
Fit the classifier with training data.
Parameters
df
Training DataFrame with columns unique_id, y, and label_col.
label_col
Name of the column containing class labels.
Returns
self
predict(df)
Predict class labels for test time series.
Parameters
df
Test DataFrame with columns unique_id and y.
Returns
pl.DataFrame
DataFrame with columns unique_id and predicted_label.
knn_classify(train_df, test_df, k=3, method='dtw', id_col='unique_id', target_col='y', label_col='label', **distance_kwargs)
K-Nearest Neighbors time series classification.
Parameters
train_df
Training DataFrame with columns id_col, target_col, and label_col.
The label_col must have one label per id_col.
test_df
Test DataFrame with columns id_col and target_col.
k
Number of nearest neighbors.
method
Distance metric name (e.g. "dtw", "erp", "lcss").
id_col
Column identifying each time series.
target_col
Column with the time series values.
label_col
Column with class labels in the training data.
**distance_kwargs
Extra keyword arguments forwarded to the distance function.
Returns
pl.DataFrame
DataFrame with columns [id_col, "predicted_label"].