无监督人口分类

2024-10-03 09:08:56 发布

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

我有一个包含两个参数的数据集,如下所示(我添加了密度等值线图):

enter image description here

我的目标是将这个样本分成两个子集,如下所示:

enter image description here

这张图片来自SDSS中恒星形成的淬火组:中心,卫星和星系一致性,Knobel等人,《天体物理学杂志》,800:24(20pp),2015年2月1日,可查阅here。这个 分隔线是用眼睛画的,并不完美。你知道吗

我需要的是像这张漂亮的维基百科图表中的红线(最大化距离):

enter image description here

不幸的是,所有的线性分类,似乎接近我要找的(支持向量机,SVC等)是监督学习。你知道吗

我尝试过无监督学习,比如KMeans 2 clusteers,这种方式(CompactSFR[['lgm_tot_p50','sSFR']]是您可以在本文末尾找到的熊猫数据集):

X = CompactSFR[['lgm_tot_p50','sSFR']]
from sklearn.cluster import KMeans

kmeans2 = KMeans(n_clusters=2)
# Fitting the input data
kmeans2 = kmeans2.fit(X)
# Getting the cluster labels
labels2 = kmeans2.predict(X)
# Centroid values
centroids = kmeans2.cluster_centers_
f, (ax1,ax2) = plt.subplots(nrows=1, ncols=2, figsize=(10, 5), sharey=True)
ax1.scatter(CompactSFR['lgm_tot_p50'],CompactSFR['sSFR'],c=labels2);
X2 = kmeans2.transform(X)
ax1.set_title("Kmeans 2 clusters", fontsize=15)
ax1.set_xlabel('$\log_{10}(M)$',fontsize=10) ;
ax1.set_ylabel('sSFR',fontsize=10) ;
f.subplots_adjust(hspace=0)

但我得到的分类是:

enter image description here

这是行不通的。你知道吗

此外,我想要的不是简单的分类,而是分离线的方程(这显然与线性回归非常不同)。你知道吗

我想避免发展贝叶斯模型的最大可能性,如果有的东西已经存在。你知道吗

你可以找到一个小样本(959点)here。你知道吗

注意:this question与我的情况不符。你知道吗


Tags: 数据here分类样本clusterkmeanssetfontsize
1条回答
网友
1楼 · 发布于 2024-10-03 09:08:56

下面的代码将使用由两个分量组成的高斯混合模型来实现这一点,并生成这个结果。result figure

首先,从文件中读取数据并删除异常值:

import pandas as pd
import numpy as np
from sklearn.neighbors import KernelDensity

frm = pd.read_csv(FILE, index_col=0)
kd = KernelDensity(kernel='gaussian')
kd.fit(frm.values)
density = np.exp(kd.score_samples(frm.values))
filtered = frm.values[density>0.05,:]

然后拟合高斯混合模型:

from sklearn.mixture import GaussianMixture
model = GaussianMixture(n_components=2, covariance_type='full')
model.fit(filtered)
cl = model.predict(filtered)

要获得绘图:

import matplotlib.pyplot as plt
plt.scatter(filtered[cl==0,0], filtered[cl==0,1], color='Blue')
plt.scatter(filtered[cl==1,0], filtered[cl==1,1], color='Red')

相关问题 更多 >