Skip to main content

Knee-point detection in Python

Project description

kneed

Knee-point detection in Python

Downloads Downloads Binder Build Status CodeFactor

This repository is an attempt to implement the kneedle algorithm, published here. Given a set of x and y values, kneed will return the knee point of the function. The knee point is the point of maximum curvature.

Installation

To install use pip:

 $ pip install kneed                                                                                                                                                                                            

Or clone the repo:

 $ git clone https://github.com/arvkevi/kneed.git                                                                                                                                                               
 $ python setup.py install                                                                                                                                

Tested with Python 3.5 and 3.6

Usage

This reproduces Figure 2 from the manuscript.

x and y must be equal length arrays.
DataGenerator has functions to generate sample datasets.

from kneed import DataGenerator, KneeLocator

x, y = DataGenerator.figure2()

print([round(i, 3) for i in x])
print([round(i, 3) for i in y])

[0.0, 0.111, 0.222, 0.333, 0.444, 0.556, 0.667, 0.778, 0.889, 1.0]
[-5.0, 0.263, 1.897, 2.692, 3.163, 3.475, 3.696, 3.861, 3.989, 4.091]

Instantiating KneeLocator with x, y and the appropriate curve and direction will find the knee (or elbow) point.
Here, kneedle.knee stores the knee point of the curve.

kneedle = KneeLocator(x, y, S=1.0, curve='concave', direction='increasing')

print(round(kneedle.knee, 3))
0.222

# .elbow can also be used to access point of maximum curvature
print(round(kneedle.elbow, 3))
0.222

The KneeLocator class also has some plotting functions for quick visualization of the curve (blue), the distance curve (red) and the knee (dashed line, if present)

kneedle.plot_knee_normalized()

Average Knee from 5000 NoisyGaussians when mu=50 and sigma=10

import numpy as np

knees = []
for i in range(5000):
    x,y = DataGenerator.noisy_gaussian(mu=50, sigma=10, N=1000)
    kneedle = KneeLocator(x, y, curve='concave', direction='increasing')
    knees.append(kneedle.knee)

np.mean(knees)
60.921051806064931

Application

Find the optimal number of clusters (k) to use in k-means clustering. See the tutorial in the notebooks folder, this can be achieved with the direction keyword argument:

KneeLocator(x, y, curve='convex', direction='decreasing')

Contributing

Contributions are welcome, if you have suggestions or would like to make improvements please submit an issue or pull request.

Citation

Finding a “Kneedle” in a Haystack: Detecting Knee Points in System Behavior Ville Satopa † , Jeannie Albrecht† , David Irwin‡ , and Barath Raghavan§ †Williams College, Williamstown, MA ‡University of Massachusetts Amherst, Amherst, MA § International Computer Science Institute, Berkeley, CA

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

kneed-0.2.3.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

kneed-0.2.3-py2.py3-none-any.whl (6.8 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: kneed-0.2.3.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for kneed-0.2.3.tar.gz
Algorithm Hash digest
SHA256 39389c49b0cd9d976a3aaa8400aa9313bfb580818bc1ef60e0fcf4c9bb4f871e
MD5 324aaa63db11421e1d470c05a69a2749
BLAKE2b-256 4286cf0a1fc99c84c0cecf397b643e1cee9be457c55ee42296f27f0f8b8b9af3

See more details on using hashes here.

File details

Details for the file kneed-0.2.3-py2.py3-none-any.whl.

File metadata

  • Download URL: kneed-0.2.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for kneed-0.2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 937c25e712e89637f7d267bfc24450ea48e816c3edb68bea164e460db4fcb298
MD5 5de3483e4753d2dcafc0448d124dd956
BLAKE2b-256 4b5f1a22d47543cec7f64b7e84cfeb04074d8ad8249b3fdca5d5af39e870500a

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