2015-10-06 103 views
1

我正在嘗試根據數據點的密度對數據進行聚類。如何根據密度對數據進行聚類?

1

我想根據density.Like所以周圍繪製這些區域的輪廓:

2

我試圖從here適應下面的代碼來得到這一點:

import numpy as np 
import matplotlib.pyplot as plt 
from scipy.stats import gaussian_kde 

# Generate fake data 
x = np.random.normal(size=1000) 
y = x * 3 + np.random.normal(size=1000) 

# Calculate the point density 
xy = np.vstack([x,y]) 
z = gaussian_kde(xy)(xy) 

# Sort the points by density, so that the densest points are plotted last 
idx = z.argsort() 
x, y, z = x[idx], y[idx], z[idx] 

fig, ax = plt.subplots() 
img=ax.scatter(x, y, c=z, edgecolor='') 

plt.show() 
+0

[鏈接](http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gaussian_kde.html) 也許這裏的代碼可以幫助你 – Glostas

+0

那麼什麼去你嘗試過什麼錯?此外,包含用於繪製上述內容的數據和代碼,以便人們可以提供幫助(http://stackoverflow.com/help/mcve)。 –

+0

@EdSmith:我複製/粘貼來自科學論文的圖片, – user1361488

回答

0

要按密度聚類,請嘗試像這樣的算法0。但是,看起來您更想估計密度本身,而不是將點集中在一起,因爲您想按密度對輸出進行着色。在這種情況下,如果同時具有寬而尖銳的峯值,則使用簡單的Kernel密度估計(R中的density函數)或自適應內核密度估計。舉例Matlab的:Adaptive Kernel density estimate on Matlab File Exchange

相關問題