Skip to main content

tsknn is a Python implementation of the k-nearest neighbors (KNN) algorithm aimed at time series forecasting

Project description

tsknn

PyPI version

TSKNN (Time Series K-Nearest Neighbors) is a Python implementation of the k-nearest neighbors (KNN) algorithm designed specifically for time series forecasting.

It is a pure Python and NumPy reimplementation of the tsfknn package from R—completely rewritten from scratch without relying on C extensions. By leveraging optimized NumPy operations, TSKNN achieves high performance while remaining lightweight and easy to install.

Installation

pip install tsknn

Or clone the repository and install locally:

git clone https://github.com/ricardozago/tsknn.git
cd tsknn
pip install .

Usage example

import numpy as np

from tsknn import tsknn

X = np.random.rand(1000)

model = tsknn(
    cf="mean",  # also median or weighted
    h=24,  # forecast horizon
    transform="multiplicative",  # also additive or None
    msas="recursive",  # also mimo
    lags=[1, 2, 3, 4, 5, 6], # lags to consider as features
    k=[5, 7, 9], # number of neighbors to consider
)
model.fit(X)
tsknn_resp = model.predict()
print(tsknn_resp)

Tests

To run the tests and check coverage:

python -m pytest --cov=tsknn --cov-report term-missing

To run all tests, you’ll need R installed, since the results are compared against the original tsfknn package. You’ll also need the rpy2 library to interface between Python and R.

Contributing

Contributions are welcome! Open issues or pull requests.

  1. Fork the project
  2. Create your branch (git checkout -b feature/feature-name)
  3. Commit your changes (git commit -am 'feat: new feature')
  4. Push to the branch (git push origin feature/feature-name)
  5. Open a Pull Request

License

This project is licensed under the MIT license. See the LICENSE file for more details.

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

tsknn-0.1.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

tsknn-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file tsknn-0.1.0.tar.gz.

File metadata

  • Download URL: tsknn-0.1.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.8

File hashes

Hashes for tsknn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 48bdc2eed3bc7cbc45967b74a5763fcfb20fd166317627a8e290d9560ca7398c
MD5 1f87a4d69909d5cf805e7618368d2acd
BLAKE2b-256 286bf5ebbc7b3e04fd193de4d3babd941537fa40379797701403e0f16ea5c36f

See more details on using hashes here.

File details

Details for the file tsknn-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tsknn-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.8

File hashes

Hashes for tsknn-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b63692dc1ada7912a2add06e3f204564d320e7a4ef89282ea065a622022a52e
MD5 545865e05d0f72e32aa6ba7e2c8d91b1
BLAKE2b-256 ba97f8ec0d8ea42888586e3bbd9a01784cceb829a903d57da3c230280317067c

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