API Reference¶
KneeLocator¶
The primary class for knee/elbow point detection.
kneed.knee_locator.KneeLocator
¶
Bases: object
Once instantiated, this class attempts to find the point of maximum
curvature on a line. The knee is accessible via the .knee attribute.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
x values, must be the same length as y. |
required |
y
|
array - like
|
y values, must be the same length as x. |
required |
S
|
float
|
Sensitivity. The number of minimum data points below the local distance maximum before calling a knee. The original paper suggests a default of 1.0. |
1.0
|
curve
|
str
|
If |
"concave"
|
direction
|
str
|
One of |
"increasing"
|
interp_method
|
str
|
One of |
"interp1d"
|
online
|
bool
|
If True, kneed will correct old knee points as it traverses the curve. If False, it returns the first knee found. |
False
|
polynomial_degree
|
int
|
The degree of the fitting polynomial. Only used when
|
7
|
Attributes:
| Name | Type | Description |
|---|---|---|
x |
ndarray
|
x values. |
y |
ndarray
|
y values. |
S |
float
|
Sensitivity, original paper suggests default of 1.0. |
curve |
str
|
If |
direction |
str
|
One of |
interp_method |
str
|
One of |
online |
bool
|
If True, corrects old knee points. If False, returns first knee. |
polynomial_degree |
int
|
The degree of the fitting polynomial. |
N |
int
|
The number of |
Ds_y |
ndarray
|
The y values from the fitted spline. |
x_normalized |
ndarray
|
The normalized x values. |
y_normalized |
ndarray
|
The normalized y values. |
x_difference |
ndarray
|
The x values of the difference curve. |
y_difference |
ndarray
|
The y values of the difference curve. |
maxima_indices |
ndarray
|
The indices of each of the maxima on the difference curve. |
x_difference_maxima |
ndarray
|
The x values from the difference curve where the local maxima are located. |
y_difference_maxima |
ndarray
|
The y values from the difference curve where the local maxima are located. |
minima_indices |
ndarray
|
The indices of each of the minima on the difference curve. |
x_difference_minima |
ndarray
|
The x values from the difference curve where the local minima are located. |
y_difference_minima |
ndarray
|
The y values from the difference curve where the local minima are located. |
Tmx |
ndarray
|
The threshold values on the difference curve for determining the knee point. |
knee |
float or None
|
The x value of the knee point. None if no knee/elbow was detected. |
knee_y |
float or None
|
The y value of the knee point. None if no knee/elbow was detected. |
norm_knee |
float or None
|
The normalized x value of the knee point. |
norm_knee_y |
float or None
|
The normalized y value of the knee point. |
all_knees |
set
|
All the x values of the identified knee points. |
all_norm_knees |
set
|
All the normalized x values of the identified knee points. |
all_knees_y |
list
|
All the y values of the identified knee points. |
all_norm_knees_y |
list
|
All the normalized y values of the identified knee points. |
elbow |
float or None
|
Alias for |
elbow_y |
float or None
|
Alias for |
norm_elbow |
float or None
|
Alias for |
norm_elbow_y |
float or None
|
Alias for |
all_elbows |
set
|
Alias for |
all_elbows_y |
list
|
Alias for |
all_norm_elbows |
set
|
Alias for |
all_norm_elbows_y |
list
|
Alias for |
Source code in kneed/knee_locator.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
__normalize(a)
staticmethod
¶
Normalize an array to [0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
array - like
|
The array to normalize. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The normalized array. |
Source code in kneed/knee_locator.py
transform_y(y, direction, curve)
staticmethod
¶
Transform y to concave, increasing based on given direction and curve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
array - like
|
The y values to transform. |
required |
direction
|
str
|
One of |
required |
curve
|
str
|
One of |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The transformed y values. |
Source code in kneed/knee_locator.py
find_knee()
¶
Identify the knee value and set the instance attributes.
This method is called automatically when KneeLocator is
instantiated.
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in kneed/knee_locator.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
plot_knee_normalized(figsize=None, title='Normalized Knee Point', xlabel=None, ylabel=None)
¶
Plot the normalized curve, the difference curve, and the knee.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
figsize
|
tuple of int
|
The figure size of the plot, e.g. |
None
|
title
|
str
|
Title of the visualization. |
"Normalized Knee Point"
|
xlabel
|
str
|
X-axis label. |
None
|
ylabel
|
str
|
Y-axis label. |
None
|
Source code in kneed/knee_locator.py
plot_knee(figsize=None, title='Knee Point', xlabel=None, ylabel=None)
¶
Plot the curve and the knee, if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
figsize
|
tuple of int
|
The figure size of the plot, e.g. |
None
|
title
|
str
|
Title of the visualization. |
"Knee Point"
|
xlabel
|
str
|
X-axis label. |
None
|
ylabel
|
str
|
Y-axis label. |
None
|
Source code in kneed/knee_locator.py
DataGenerator¶
Utility class for generating synthetic test data.
kneed.data_generator.DataGenerator
¶
Bases: object
Generate synthetic data to work with kneed.
Source code in kneed/data_generator.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
noisy_gaussian(mu=50, sigma=10, N=100, seed=42)
staticmethod
¶
Recreate NoisyGaussian from the original Kneedle paper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mu
|
float
|
The mean value to build a normal distribution around. |
50
|
sigma
|
float
|
The standard deviation of the distribution. |
10
|
N
|
int
|
The number of samples to draw from to build the normal distribution. |
100
|
seed
|
int
|
An integer to set the random seed. |
42
|
Returns:
| Type | Description |
|---|---|
tuple of numpy.ndarray
|
|
Source code in kneed/data_generator.py
figure2()
staticmethod
¶
Recreate the values in figure 2 from the original Kneedle paper.
Returns:
| Type | Description |
|---|---|
tuple of numpy.ndarray
|
|
Source code in kneed/data_generator.py
convex_increasing()
staticmethod
¶
Generate a sample increasing convex function.
Returns:
| Type | Description |
|---|---|
tuple of numpy.ndarray
|
|
Source code in kneed/data_generator.py
convex_decreasing()
staticmethod
¶
Generate a sample decreasing convex function.
Returns:
| Type | Description |
|---|---|
tuple of numpy.ndarray
|
|
Source code in kneed/data_generator.py
concave_decreasing()
staticmethod
¶
Generate a sample decreasing concave function.
Returns:
| Type | Description |
|---|---|
tuple of numpy.ndarray
|
|
Source code in kneed/data_generator.py
concave_increasing()
staticmethod
¶
Generate a sample increasing concave function.
Returns:
| Type | Description |
|---|---|
tuple of numpy.ndarray
|
|
Source code in kneed/data_generator.py
bumpy()
staticmethod
¶
Generate a sample function with local minima/maxima.
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in kneed/data_generator.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
find_shape¶
Utility function to auto-detect the curve direction and type.
kneed.shape_detector.find_shape(x, y)
¶
Detect the direction and curve type of the data.
Fits a first-degree polynomial to the data and uses the coefficients to determine if the curve is increasing/decreasing and concave/convex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array - like
|
x values. |
required |
y
|
array - like
|
y values. |
required |
Returns:
| Type | Description |
|---|---|
tuple of str
|
|