A Python library for detecting knee (elbow) points in curves using the Kneedle algorithm. Given a set of x and y values, kneed returns the point of maximum curvature.
Features
Detect knee/elbow points in concave or convex curves
Support for increasing and decreasing functions
Automatic curve shape detection with find_shape()
Multiple knee detection via online mode (all_knees / all_elbows)
kneed has been tested with Python 3.10, 3.11, 3.12, 3.13, 3.14, and 3.15.
anaconda
conda install -c conda-forge kneed
pip
pip install kneed # knee-detection only
pip install kneed[plot] # also install matplotlib for visualizations
Clone from GitHub
git clone https://github.com/arvkevi/kneed.git && cd kneed
pip install -e .
Quick Start
from kneed import KneeLocator, DataGenerator
# Generate sample data
x, y = DataGenerator.figure2()
# Find the knee point
kl = KneeLocator(x, y, curve="concave", direction="increasing")
print(kl.knee) # 0.222
print(kl.knee_y) # 1.897
If you're unsure about the curve type and direction, use find_shape() to auto-detect:
from kneed import find_shape
direction, curve = find_shape(x, y)
kl = KneeLocator(x, y, curve=curve, direction=direction)
Usage
These steps reproduce Figure 2 from the original Kneedle manuscript.
Input Data
The DataGenerator class is a utility to generate sample datasets.
Note: x and y must be equal length arrays.
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]
Find Knee
The knee (or elbow) point is calculated by instantiating the KneeLocator class with x, y and the appropriate curve and direction.
Here, kneedle.knee and kneedle.elbow store the point of maximum curvature.
Contributions are welcome, please refer to CONTRIBUTING
to learn more about how to contribute.
Citation
If you use kneed in your research, please cite:
Satopa, V., Albrecht, J., Irwin, D., and Raghavan, B. (2011). "Finding a 'Kneedle' in a Haystack: Detecting Knee Points in System Behavior." 31st International Conference on Distributed Computing Systems Workshops, pp. 166-171.
@inproceedings{satopa2011kneedle,
title={Finding a "Kneedle" in a Haystack: Detecting Knee Points in System Behavior},
author={Satopa, Ville and Albrecht, Jeannie and Irwin, David and Raghavan, Barath},
booktitle={31st International Conference on Distributed Computing Systems Workshops},
pages={166--171},
year={2011},
}
Works well with
Use together with kneed: extensions, drivers, servers and companion tools.
numpyPythie’s Index: 92Fundamental package for array computing in Python1.2B downloads/mo
scipyPythie’s Index: 86Fundamental algorithms for scientific computing in Python423.2M downloads/mo