對于一組數(shù)據(jù)拐云,如何求拐點是一個經(jīng)常需要做的事情,特別是對于拖尾的數(shù)據(jù)只盹,這里介紹一個python工具包可以簡單的判斷一組list<int>的拐點
1. 安裝
下面任意一種方式都可
1. conda install -c conda-forge kneed
2. pip install kneed
3. git clone https://github.com/arvkevi/kneed.git
python setup.py install
2.使用方法
假設我們有一組數(shù)據(jù):
from kneed import KneeLocator
import matplotlib.pyplot as plt
# 需要注意x韧骗,y的長度必須一致
x = [1, 2, 3.4, 4, 5.3, 6.6, 7.4, 8.2, 9.7, 10.1]
y = [1, 2, 10, 14, 16, 17, 20, 54, 75, 89]
kneedle = KneeLocator(x, y, S=1.0, curve='convex', direction='increasing')
print(f'拐點所在的x軸是: {kneedle.elbow}')
kneedle.plot_knee() # 將數(shù)據(jù)畫出來
['out']: 拐點所在的x軸是: 7.4
3.參數(shù)介紹及說明
1. 當你的數(shù)據(jù)是一個凹函數(shù),并且是遞增的,請使用:
curve='convex' and direction='increasing'
如下:
x = [1,2, 3.4, 4, 5.3, 6.6, 7.4, 8.2, 9.7, 10.1]
y = [1, 2, 10, 14, 16, 17, 20, 54, 75, 89]
kneedle = KneeLocator(x, y, S=1.0, curve='convex', direction='increasing')
kneedle.plot_knee()
print(f'拐點所在的x軸是: {kneedle.elbow}') # 拐點所在x軸的值(不是x軸索引索引)
['out'] :
拐點所在的x軸是: 7.4
2. 當你的數(shù)據(jù)是一個凹函數(shù), 并且是遞減少的, 請使用:
curve='convex' and direction='decreasing'
如下:
x = [1,2, 3.4, 4, 5.3, 6.6, 7.4, 8.2, 9.7, 10.1]
y = [89, 75, 54, 20, 17, 16, 14, 10, 2, 1]
kneedle = KneeLocator(x, y, S=1.0, curve='convex', direction='decreasing')
kneedle.plot_knee()
print(f'拐點所在的x軸是: {kneedle.elbow}')
['out']:
拐點所在的x軸是: 4.0
3. 當你的數(shù)據(jù)是一個凸函數(shù), 并且是遞增的, 請使用:
curve='concave' and direction='increasing'
x = [1,2, 3.4, 4, 5.3, 6.6, 7.4, 8.2, 9.7, 10.1]
y = [1, 10, 15, 30, 37, 38, 39, 42, 41, 41.2]
kneedle = KneeLocator(x, y, S=1.0, curve='concave', direction='increasing')
kneedle.plot_knee()
print(f'拐點所在的x軸是: {kneedle.elbow}')
['out']:
拐點所在的x軸是: 5.3
4. 當你的數(shù)據(jù)是一個凸函數(shù), 并且是遞減的, 請使用:
curve='concave' and direction='decreasing'
x = [1,2, 3.4, 4, 5.3, 6.6, 7.4, 8.2, 9.7, 10.1]
y = [41.2, 41, 42, 39, 38, 37, 30, 15, 10, 1]
kneedle = KneeLocator(x, y, S=1.0, curve='concave', direction='decreasing')
kneedle.plot_knee()
print(f'拐點所在的x軸是: {kneedle.elbow}')
['out']:
拐點所在的x軸是: 6.6
其它參數(shù):
s: 敏感性, 原論文默認推薦為 1.0
curve: 'concave', 算法將會計算 knees(可以理解為凸函數(shù))权埠,這時輸出拐點所在x軸使用 kneedle.knees
'convex', 算法將會計算 elbows(可以理解為凹函數(shù))榨了,這時輸出拐點所在x軸使用 kneedle.elbows
direction: "increasing", "decreasing" 其中一個,分別代表遞增和遞減
還有一些不常用的參數(shù)詳見:
https://pypi.org/project/kneed/