error【TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to ... ... ]when using kneed

问题描述 投票:0回答:1

我想找到L曲线的肘点,以选择肘点的y轴值作为阈值。并删除y轴值低于阈值的点。为了实现这一点,我使用了 kned(https://github.com/arvkevi/kneed),一个用于查找膝盖/肘点的 python 包。

我是这样写函数的,

def findThresholdLCurve(data:dict): 
    if len(data) > 0:
        # 统计所有词语出现的次数并按出现次数降序排序
        Occ = sorted(list(data.values()), reverse=True)

        x = list(range(1, len(data) + 1))
        y = Occ
        kneedle = KneeLocator(x, y, curve="convex", direction="decreasing", online=True,interp_method="polynomial")
        th = kneedle.knee_y
        kneedle.plot_knee() 
        return th

大部分数据运行成功,得到一个合适的弯头点。像这样, enter image description here

但是,这样的数据就有点麻烦了,

data = {'apple': 0.5966618322834488, 'banana': 0.32877029629550597, 'peach': 0.5540431299774883}

程序仍然返回一个图,但是没有膝点/肘点, enter image description here 和这样的错误,

Traceback (most recent call last):
  File "D:\my_study\AUDESOME_py_chatgpt\main.py", line 35, in <module>
    S = cal_S(O1, L)
  File "D:\my_study\AUDESOME_py_chatgpt\cal_S.py", line 43, in cal_S
    th2 = findThresholdLCurve(S[ci])
  File "D:\my_study\AUDESOME_py_chatgpt\findThresholdLCurve.py", line 23, in findThresholdLCurve
    kneedle.plot_knee()  
  File "D:\Anaconda\lib\site-packages\kneed\knee_locator.py", line 386, in plot_knee
    plt.vlines(
  File "D:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 3012, in vlines
    return gca().vlines(
  File "D:\Anaconda\lib\site-packages\matplotlib\__init__.py", line 1412, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "D:\Anaconda\lib\site-packages\matplotlib\axes\_axes.py", line 1148, in vlines
    self.update_datalim(corners)
  File "D:\Anaconda\lib\site-packages\matplotlib\axes\_base.py", line 2481, in update_datalim
    if not np.any(np.isfinite(xys)):
TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

Process finished with exit code 1

我想可能是数据y的值太长了,所以我把小数点后的位数改为10,但是还是有这个问题

我该如何解决?或者是否有任何其他方法可以找到肘/膝点?

python point
1个回答
0
投票

我完成了。 我通过使用 kneebow(https://github.com/georg-un/kneebow) 找到 L 曲线的肘点。

这样的代码,

from kneebow.rotor import Rotor
def function(data):
    rotor = Rotor()  # 实例化一个对象
    rotor.fit_rotate(data)
    elbow_idx = rotor.get_elbow_index()
    if elbow_idx == 0:
        knee_idx = rotor.get_knee_index()
        return knee_idx
        # rotor.plot_knee()
    else:
        # rotor.plot_elbow()
        return elbow_idx

其中数据是二维数组。

© www.soinside.com 2019 - 2024. All rights reserved.