Skip to main content

fast rolling rank for numpy

Project description

rollingrank

rollingrank is a fast implementation of rolling rank transformation (described as the following code).

import pandas as pd

# x is numpy array
def rollingrank(x, window=None):
    def to_rank(x):
        # result[i] is the rank of x[i] in x
        return np.sum(np.less(x, x[-1]))
    return pd.Series(x).rolling(window).apply(to_rank).values

Motivation

Rolling rank is a good tool to create features for time series prediction. However, rolling rank was not easy to use in python. There were no exact methods to do it. The simple implementation using pandas and numpy is too slow.

Performance

Implementation Complexity
rollingrank O(n * log(w)) (not implemented)
pandas rolling + numpy O(n * w)

n: input length w: rolling window size

Install

pip install rollingrank

Example

import numpy as np
import rollingrank

x = np.array([0.1, 0.2, 0.3, 0.25, 0.1, 0.2, 0.3])
y = rollingrank.rollingrank(x, window=3)
print(y)
# [nan nan  2.  1.  0.  1.  2.]

y = rollingrank.rollingrank(x, window=3, pct=True)
print(y)
# [nan nan 1.  0.5 0.  0.5 1. ]

Kaggle Example

https://www.kaggle.com/bakuage/rollingrank-example

Development

test

python -m unittest discover tests

build/upload

python setup.py sdist
twine upload --repository pypitest dist/*
twine upload --repository pypi dist/*

TODO

  • support axis
  • O(n * log(w)) algorithm

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

rollingrank-0.2.3.tar.gz (68.0 kB view details)

Uploaded Source

File details

Details for the file rollingrank-0.2.3.tar.gz.

File metadata

  • Download URL: rollingrank-0.2.3.tar.gz
  • Upload date:
  • Size: 68.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.7

File hashes

Hashes for rollingrank-0.2.3.tar.gz
Algorithm Hash digest
SHA256 2821f92d2942be0ad83dd893b74b43ce2165ee1be1bf489e8867d89ece783c6d
MD5 559345c56cd1c705372fd05de27d80b3
BLAKE2b-256 42415663e3c46320b2019d13868411bcbf50f6b543bbb1b6c257bff31ba084d1

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