找到“弯头”使用Python优化曲线 [英] find the "elbow point" on an optimization curve with Python

查看:344
本文介绍了找到“弯头”使用Python优化曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个点列表,这些点是kmeans算法的惯性值。

要确定最佳的聚类数量,我需要找到该曲线开始展平的点。



数据示例



这是我的值列表的创建和填充方式:

  sum_squared_dist = [] 
K =在K中,k的范围为(1,50)

km = KMeans(n_clusters = k,random_state = 0)
km = km.fit(normalized_modeling_data)
sum_squared_dist.append(km.inertia_)

print( sum_squared_dist)

我如何找到该曲线的音高增加的点(曲线在下降,因此一阶导数为负)?



我的方法

  derivates = [] 
对于范围内的我(len(sum_squared_dist)):
derivates.append(sum_squared_dist [i]-sum_squared_dist [i-1])$ ​​b $ b

我想使用弯头方法找到任何给定数据的最佳簇数。有人可以帮我找到惯性值列表开始变平的点吗?



编辑

数据点:

  [7342.1301373073857,6881.7109460930769,6531.1657905495022,
6356.2255554679778,6209.8382535595829,6094.9052166741121,
5980.0191582610196, 5880.1869867848218,5779.8957906367368,
5691.1879324562778,5617.5153566271356,5532.2613232619951,
5467.352265375117,5395.4493783888756,5345.3459908298091,
5290.6769823693812,5243.5271656371 532,$ bbb $ bb





$ b $ 529 $ c




$ b >

图:

解决方案

我研究了


i have a list of points which are the inertia values of a kmeans algorithm.
To determine the optimum amount of clusters i need to find the point, where this curve starts to flatten.

Data example

Here is how my list of values is created and filled:

sum_squared_dist = []
K = range(1,50)
for k in K:
    km = KMeans(n_clusters=k, random_state=0)
    km = km.fit(normalized_modeling_data)
    sum_squared_dist.append(km.inertia_)

print(sum_squared_dist)

How can i find a point, where the pitch of this curve increases (the curve is falling, so the first derivation is negative)?

My approach

derivates = []
for i in range(len(sum_squared_dist)):
    derivates.append(sum_squared_dist[i] - sum_squared_dist[i-1])

I want to find the optimum number of clusters any given data using the elbow method. Could someone help me how i can find the point where the list of the inertia values starts to flatten?

Edit
Datapoints:

[7342.1301373073857, 6881.7109460930769, 6531.1657905495022,  
6356.2255554679778, 6209.8382535595829, 6094.9052166741121, 
5980.0191582610196, 5880.1869867848218, 5779.8957906367368, 
5691.1879324562778, 5617.5153566271356, 5532.2613232619951, 
5467.352265375117, 5395.4493783888756, 5345.3459908298091, 
5290.6769823693812, 5243.5271656371888, 5207.2501206569532, 
5164.9617535255456]

Graph:

解决方案

I worked on a Python package modeled after the Kneedle algorithm. It finds x=5 as the point where the curve starts to flatten. The documentation and the paper discuss the algorithm for choosing the knee point in more detail.

y = [7342.1301373073857, 6881.7109460930769, 6531.1657905495022,  
6356.2255554679778, 6209.8382535595829, 6094.9052166741121, 
5980.0191582610196, 5880.1869867848218, 5779.8957906367368, 
5691.1879324562778, 5617.5153566271356, 5532.2613232619951, 
5467.352265375117, 5395.4493783888756, 5345.3459908298091, 
5290.6769823693812, 5243.5271656371888, 5207.2501206569532, 
5164.9617535255456]

x = range(1, len(y)+1)

from kneed import KneeLocator
kn = KneeLocator(x, y, curve='convex', direction='decreasing')
print(kn.knee)
5

import matplotlib.pyplot as plt
plt.xlabel('number of clusters k')
plt.ylabel('Sum of squared distances')
plt.plot(x, y, 'bx-')
plt.vlines(kn.knee, plt.ylim()[0], plt.ylim()[1], linestyles='dashed')

这篇关于找到“弯头”使用Python优化曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆