聚类重叠椭圆

2024-06-16 12:28:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据集,它由多个数据子集组成。如果我画Y和X,我会得到一些重叠的椭圆,我想把它们聚集起来。在

我试过使用来自sklearnmixtureBayesian Gaussian Mixture Model给出了最好的结果,但是它无法识别重叠的数据:

enter image description here

import itertools
import numpy as np
import pylab as plt
from sklearn import mixture
from matplotlib.patches import Ellipse
field_File_1 = './dummy_distrib_3.txt' 
'''
    link to data: 
    https://www.dropbox.com/s/jd3wx1ee8r1mj8p/dummy_distrib_3.txt?dl=0
'''
my_dis_1 = np.loadtxt(field_File_1)

X = my_dis_1[:50000,:2]

BaGaMiMo = mixture.BayesianGaussianMixture(n_components=2, covariance_type='full', 
                                         weight_concentration_prior_type='dirichlet_distribution').fit(X)

X1 = X[BaGaMiMo.predict(X) == 0, :]
X2 = X[BaGaMiMo.predict(X) == 1, :]

plt.figure(figsize=(18.0, 6.0))
plt.subplot(1,3,1)
plt.scatter(X[:,0], X[:,1], 0.2, color='m')

plt.subplot(1,3,2)
plt.scatter(X[BaGaMiMo.predict(X) == 0, 0], X[BaGaMiMo.predict(X) == 0, 1], .2, color='navy')

plt.subplot(1,3,3)
plt.scatter(X[BaGaMiMo.predict(X) == 1, 0], X[BaGaMiMo.predict(X) == 1, 1], .2, color='c')
plt.show()

接下来我要做的是,在青色和海军蓝色分布上安装两个椭圆,然后从青色分布中去除横截面上的粒子

enter image description here

然后根据计算出的比率将它们随机分配到海军和青色分布中:

enter image description here

一个问题是,如果我做了一个直方图的数据,我注意到在两个椭圆的交线的青色数据中存在过多的人口/不连续性,我正在寻找减少这种过度人口的方法,任何帮助都将不胜感激。在

jupyter-notebook可以在这里下载:https://www.dropbox.com/s/z1tdgpx1g1lwtb5/Clustering.ipynb?dl=0

*数据点属于两组带电粒子。在


Tags: 数据fromimportasnppltsklearnpredict
1条回答
网友
1楼 · 发布于 2024-06-16 12:28:26

也许这会有帮助。我用predict_proba()代替predict()来得到一个点属于任一组的概率。然后我玩了切断。把截止值设为0.5,我得到的结果和你一样。经过一番反复试验,0.933的临界值似乎能起作用。在

p1 = X[BaGaMiMo.predict_proba(X)[:,0] > 0.933, :]
p2 = X[BaGaMiMo.predict_proba(X)[:,0] <= 0.933, :]
plt.scatter(p1[:,0], p1[:,1], 0.2, color='m')
plt.scatter(p2[:,0], p2[:,1], 0.2, color='navy')

Scatter plot with 0.933 cutoff between groups

相关问题 更多 >