Skip to main content

Sliding-window multivariate time-series feature extraction and classification

Project description

slimtsf · Sliding-Window Multivariate Time-Series Forest

PyPI version CI Python 3.9+ License: MIT

A minimal, scikit-learn–compatible library for classifying multivariate time-series data using multi-scale sliding-window feature extraction.


Install

pip install slimtsf

Quick Start

Full pipeline (recommended)

import numpy as np
from slimtsf import SlimTSFClassifier

# X: (n_cases, n_channels, n_timepoints)  — 3-D numpy array
X_train = np.random.randn(100, 3, 120)
y_train = np.array([0] * 50 + [1] * 50)

clf = SlimTSFClassifier(n_estimators=200, random_state=42)
clf.fit(X_train, y_train)

X_test = np.random.randn(20, 3, 120)
predictions  = clf.predict(X_test)        # shape (20,)
probabilities = clf.predict_proba(X_test) # shape (20, 2)

Transformers only (composable use)

from slimtsf import SlidingWindowIntervalTransformer, IntervalStatsPoolingTransformer

# Stage 1 — extract sliding-window features
stage1 = SlidingWindowIntervalTransformer(
    window_sizes=[8, 16, 32],      # or None for auto
    window_step_ratio=0.5,
    feature_functions=["mean", "std", "slope"],
)
interval_features = stage1.fit_transform(X_train)  # (n_cases, n_interval_features)

# Stage 2 — pool across windows
stage2 = IntervalStatsPoolingTransformer(aggregations=("min", "mean", "max"))
pooled = stage2.fit_transform(
    interval_features,
    feature_metadata=stage1.feature_metadata_,     # wires Stage 1 → Stage 2
)  # (n_cases, n_pooled_features)

Use with scikit-learn tools

Because SlimTSFClassifier exposes fitted stage attributes, you can access the underlying sklearn RF and use it with standard sklearn utilities:

from sklearn.model_selection import cross_val_score

# Fit first, then use sklearn metrics on transformed data
clf.fit(X_train, y_train)
Xt = clf.stage2_.transform(clf.stage1_.transform(X_train))

scores = cross_val_score(clf.stage3_, Xt, y_train, cv=5)
print(scores.mean())

How It Works

3-D time-series X  (n_cases, n_channels, n_timepoints)
    │
    ▼  Stage 1 — SlidingWindowIntervalTransformer
    │  Slide windows of multiple sizes across each channel.
    │  Compute mean / std / slope per window.
    │  Output: 2-D matrix  (n_cases, n_interval_features)
    │
    ▼  Stage 2 — IntervalStatsPoolingTransformer
    │  For each (channel, feature) group,
    │  pool across windows: min / mean / max.
    │  Output: 2-D compact matrix  (n_cases, n_pooled_features)
    │
    ▼  Stage 3 — RandomForestClassifier (scikit-learn)
       Classify the pooled feature matrix.
       Output: predicted labels / probabilities

API Reference

SlimTSFClassifier

Parameter Type Default Description
window_sizes list[int] | None None Window sizes. Auto if None ([T, T//2, …]).
window_step_ratio float 0.5 Step = ratio × window size.
feature_functions list[str|FeatureFunction] ("mean","std","slope") Per-window features.
aggregations list[str] ("min","mean","max") Pooling statistics across windows.
n_estimators int 200 Number of RF trees.
max_depth int|None None Max tree depth.
class_weight str|dict|None "balanced" RF class weighting.
random_state int|None None Reproducibility seed.
n_jobs int 1 Parallel jobs for RF (-1 = all CPUs).

Methods: fit(X, y) · predict(X) · predict_proba(X) · get_feature_names_out()

Fitted attributes: stage1_ · stage2_ · stage3_ · classes_ · n_features_in_


SlidingWindowIntervalTransformer

Input: X — shape (n_cases, n_channels, n_timepoints)
Output: 2-D feature matrix (n_cases, n_interval_features)

Methods: fit(X) · transform(X) · fit_transform(X) · get_feature_names_out()
Fitted attributes: feature_metadata_ · interval_list_


IntervalStatsPoolingTransformer

Input: 2-D interval feature matrix from Stage 1 + feature_metadata
Output: 2-D pooled feature matrix (n_cases, n_pooled_features)

Methods: fit(X, feature_metadata) · transform(X) · fit_transform(X, feature_metadata) · get_feature_names_out()


Custom Feature Functions

from slimtsf import FeatureFunction, SlidingWindowIntervalTransformer
import numpy as np

# A custom feature: interquartile range
iqr = FeatureFunction(
    name="iqr",
    function=lambda seg: np.percentile(seg, 75, axis=1) - np.percentile(seg, 25, axis=1),
)

transformer = SlidingWindowIntervalTransformer(feature_functions=["mean", iqr])

Versioning

This project follows Semantic Versioning and Conventional Commits:

Commit prefix Effect
fix: patch release (0.1.x)
feat: minor release (0.x.0)
feat!: / BREAKING CHANGE: major release (x.0.0)
docs: chore: test: no release

Development

git clone https://github.com/kennaruk/slimtsf.git
cd slimtsf
pip install -e ".[dev]"
pytest -v

License

MIT — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slimtsf-1.0.2.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slimtsf-1.0.2-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file slimtsf-1.0.2.tar.gz.

File metadata

  • Download URL: slimtsf-1.0.2.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for slimtsf-1.0.2.tar.gz
Algorithm Hash digest
SHA256 73ea0316ef3617144acc645248325d298d51dbacbed41a2e75dff932c889530c
MD5 d6eb91d389d85d9c678341f3ecc744f3
BLAKE2b-256 6d5f7fa77ef98a08428c5e0aace4c14c03f1a5569b7e1cbc2f28fd21f6b29a5e

See more details on using hashes here.

File details

Details for the file slimtsf-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: slimtsf-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for slimtsf-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dc3fc869726be95b28b783d1d579711e736ba5cd57931ce4546d1b846f219b9a
MD5 b259e41ad0ecbb0084c707787cb0d413
BLAKE2b-256 bb77e5d25ab80910745e5be8e2d0714ec1f56d1b323a678572a09bb3e427f411

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page