python选择曲线,找到“弯头”使用Python优化曲线

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:

23747bcc1e1f312437211c31b405b069.png

解决方案

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')

754223b8491a01159bc2312c1679c90f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值