Knee-point detection in Python

0.8.6 · active · verified Sun Apr 12

kneed is a Python library (current version 0.8.6) for detecting knee (also known as elbow) points in curves. It implements the Kneedle algorithm to identify the point of maximum curvature in a given set of x and y values. The library is actively maintained with regular patch releases and occasional feature updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `KneeLocator` to find a knee point in a dataset, both by manually specifying `curve` and `direction`, and by automatically detecting them using `find_shape`. The `knee` and `knee_y` attributes provide the x and y coordinates of the detected knee point.

import numpy as np
from kneed import KneeLocator, find_shape

# Example 1: Basic usage with known curve type and direction
x = np.arange(1, 11)
y = np.array([1, 2, 3, 4, 5, 5.5, 5.6, 5.7, 5.8, 5.9])

kl = KneeLocator(x, y, curve="concave", direction="increasing")
print(f"Knee at x: {kl.knee}")
print(f"Corresponding y: {kl.knee_y}")

# Example 2: Using find_shape for auto-detection
x_auto = np.arange(1, 101)
y_auto = np.sin(x_auto / 10) + np.log(x_auto) + np.random.rand(100)

auto_direction, auto_curve = find_shape(x_auto, y_auto)
kl_auto = KneeLocator(x_auto, y_auto, curve=auto_curve, direction=auto_direction)
print(f"\nAuto-detected curve: {auto_curve}, direction: {auto_direction}")
print(f"Knee at x (auto-detected): {kl_auto.knee}")

view raw JSON →