无人监督的人口分类

问题描述 投票:2回答:1

我有一个带有2个参数的数据集,看起来像这样(我添加了密度等值线图):

enter image description here

我的目标是将此示例分为2个子集,如下所示:

enter image description here

该图像来自SDSS群中星形成的淬火:中心,卫星和GALACTIC CONFORMITY,Knobel等。 al。,The Astrophysical Journal,800:24(20pp),2015年2月1日,可用here。分离线是用眼睛绘制的,并不完美。

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

enter image description here

不幸的是,所有看起来接近我正在寻找的线性分类(SVM,SVC等)都是有监督的学习。

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

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不符合我的情况。

python machine-learning unsupervised-learning
1个回答
1
投票

以下代码将使用2个组件的高斯混合模型来完成,并生成此结果。 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')
© www.soinside.com 2019 - 2024. All rights reserved.