簡體   English   中英

基於kmeans聚類中心繪制邊界線

[英]Drawing boundary lines based on kmeans cluster centres

我是 scikit 學習的新手,但想嘗試一個有趣的項目。

我有英國點的經度和緯度,我曾經使用 scikit 學習 KMeans 類來創建聚類中心。 為了可視化這些數據,而不是將點作為集群,我想在每個集群周圍繪制邊界。 例如,如果一個集群是倫敦,另一個集群是牛津,我目前在每個城市的中心都有一個點,但我想知道是否有辦法使用這些數據來創建基於我的集群的邊界線?

到目前為止,這是我創建集群的代碼:

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

location1="XXX"
df = pd.read_csv(location1, encoding = "ISO-8859-1")

#Run kmeans clustering
X = df[['long','lat']].values #~2k locations in the UK
y=df['label'].values   #Label is a 0 or 1
kmeans = KMeans(n_clusters=30, random_state=0).fit(X, y)
centers=kmeans.cluster_centers_
plt.scatter(centers[:,0],centers[:,1], marker='s', s=100)

所以我希望能夠將上面示例中的中心轉換為划分每個區域的線——這可能嗎?

謝謝,

一只螞蟻

我猜你是在談論空間邊界,在這種情況下你應該遵循 Bunyk 的建議並使用 Voronoi 圖 [ 1 ]。 這是您可以實現的實際演示:http: //nbviewer.jupyter.org/gist/pv/8037100

您可以使用 Scipi 生成 Voronoi 圖。 文檔

對於您的代碼,它將是

from scipy.spatial import Voronoi, voronoi_plot_2d
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

location1="XXX"
df = pd.read_csv(location1, encoding = "ISO-8859-1")

#Run kmeans clustering
X = df[['long','lat']].values #~2k locations in the UK
y=df['label'].values   #Label is a 0 or 1
kmeans = KMeans(n_clusters=30, random_state=0).fit(X, y)
centers=kmeans.cluster_centers_

plt.scatter(centers[:,0],centers[:,1], marker='s', s=100)


vor = Voronoi(centers)
fig = voronoi_plot_2d(vor,plt.gca())

plt.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM