pyckmeans.knee package

Module contents

Knee and elbow search.

class pyckmeans.knee.KneeLocator(x: numpy.ndarray, y: numpy.ndarray, S: float = 1.0, curve: str = 'concave', direction: str = 'increasing', interp_method: str = 'interp1d', online: bool = False, polynomial_degree: int = 7)

Bases: object

An implementation of the Kneedle algorithm [1].

Once instantiated, this class attempts to find the point of maximum curvature on a line. The knee is accessible via the .knee attribute.

Parameters
xnumpy.ndarray

x values.

ynumpy.ndarray

y values.

Sfloat, optional

Sensitivity, original paper suggests default of 1.0, by default 1.0.

curvestr, optional

If ‘concave’, algorithm will detect knees. If ‘convex’, it will detect elbows., by default ‘concave’.

directionstr, optional

Curve direction. One of {‘increasing’, ‘decreasing’}, by default ‘increasing’.

interp_methodstr, optional

Interpolation method. One of

  • ‘interp1d’ - no interpolation

  • ‘polynomial’ - polynomial interpolation

By default ‘interp1d’.

onlinebool, optional

Correct old knee points if True, will return first knee if False, by default False.

polynomial_degreeint, optional

The degree of the fitting polynomial. Only used when interp_method=’polynomial’. This argument is passed to numpy polyfit deg parameter., by default 7.

Raises
ValueError

Raised if invalid curve or direction argument passed.

ValueError

Raised if invalid interp_method argument passed.

References

1

Satopaa, V., J., Albrecht, D., Irwin, B., Raghavan. 2011. “Finding a “Kneedle” in a Haystack: Detecting Knee Points in System Behavior”. 31st International Conference on Distributed Computing Systems Workshops. doi: 10.1109/ICDCSW.2011.20.

Attributes
all_elbows
all_elbows_y
all_norm_elbows
all_norm_elbows_y
elbow
elbow_y
norm_elbow
norm_elbow_y

Methods

find_knee()

This function is called when KneeLocator is instantiated.

transform_y(y, direction, curve)

transform y to concave, increasing based on given direction and curve

property all_elbows
property all_elbows_y
property all_norm_elbows
property all_norm_elbows_y
property elbow
property elbow_y
find_knee()

This function is called when KneeLocator is instantiated. It identifies the knee value and sets the instance attributes.

property norm_elbow
property norm_elbow_y
static transform_y(y: Iterable[float], direction: str, curve: str) float

transform y to concave, increasing based on given direction and curve

pyckmeans.knee.rel_extrema_idcs(x: numpy.ndarray, cmp_fun: typing.Callable[[numpy.ndarray, numpy.ndarray], numpy.ndarray] = <ufunc 'greater'>, mode: str = 'clip') numpy.ndarray

Find indices of relative extrema. A relative extremum is found if at an element, if cmp_fun returns true for both of its neighbors.

Parameters
xnumpy.ndarray

Data vector.

cmp_funCallable[[numpy.ndarray, numpy.ndarray], numpy.ndarray], optional

Compare function function, by default numpy.greater

modestr, optional

Specifies how out-of-bounds indices will behave.

  • ‘raise’ - raise an error (default)

  • ‘wrap’ - wrap around

  • ‘clip’ - clip to the range

‘clip’ mode means that all indices that are too large are replaced by the index that addresses the last element along that axis. Note that this disables indexing with negative numbers.

(mode documentation copied from numpy.take)

Returns
numpy.ndarray

Indices of the extrema.