利用抽象混合模型和组件模型API实现马尔可夫链贝叶斯聚类技术,包括DPM和MFM技术。
bmcc的Python项目详细描述
贝叶斯马尔可夫链蒙特卡罗聚类
马尔可夫链蒙特卡罗贝叶斯聚类技术的实现,包括dpm(dirichlet过程混合模型[1])和mfm(有限混合模型[2])混合模型,具有抽象混合模型和组件模型api。
dpm的超参数更新使用经验bayes更新过程[3]实现。
使用最小二乘聚类[4]实现最终配置选择。
用法
安装和设置
< Python: 首先,使用pip install bmcc安装(或pip3
,具体取决于您的版本)。然后,只需导入bmcc
。
注意:仅正式支持Python3。 R:
首先,确保安装了python 3,并使用pip install bmcc
安装bmcc。然后,用
library(devtools)install_github("https://github.com/thetianshuhuang/bmcc_r")
要使用,请加载包bmcc
。您还需要加载networkite
以处理类型转换。
library(bmcc)library(reticulate)
预期类型
数据集应该是一个二维数组,其中每一行都是一个数据点。数组应该是数据类型为float64的numpy数组,以连续的c顺序排列。
- python:
data=np.array(<;source>;,dtype=np.float64)
- r/网状:
data=np_数组(<;source>;,dtype="float64",order="c")
赋值向量应该是数组。每个索引处的值指示群集分配。因为集群是无序的,所以值本身没有意义,除了确定唯一性之外,应该忽略它。赋值向量的类型为uint16。
- python:
assignments=np.array(<;source>;,dtype=np.uint16)
- r/网状:
赋值=np_数组(<;source>;,dtype="uint16")
注意,当使用网状时,R类型默认为"数值"(双精度)。调用需要整数参数(即索引、维数)的函数时,必须显式指定整数:
x<-25x<-as.integer(x)# orx<-25L
创建模型
模型分为两部分:混合模型和组件模型。目前,组件模型有一个选项,混合模型有两个选项。
正常Wishart
正态Wishart模型假设每个分量都是从Wishart分布中提取的,其自由度在初始值设定项中指定,比例矩阵与C^-1/df成正比,其中C是观测点的协方差矩阵。
- 巨蟒:
component_model = bmcc.NormalWishart(df=3)
- R/网状:
component_model = NormalWishart(df = 3)
mfm(有限混合物的混合物)
见[ 2 ]。接受两个参数:dirichlet混合参数gamma和一个关于簇数的对数先验函数。gamma默认为1,先前的默认为poisson(mu=4)。
注意:请确保以日志形式给出先前的内容!
- 巨蟒:
mixture_model=bmcc.MFM(gamma=1,prior=lambdak:poisson.logpmf(k,4))
- R/网状:
prior<-function(k){dpois(k,4,log=TRUE)}mixture_model=MFM(gamma=1,prior=py_func(prior))
运行吉布斯采样器
目前,只有折叠吉布斯采样器被实现。可以使用api(documentation todo)将其扩展到通用gibbs采样器,但目前,核心库只实现折叠的gibbs。
GibbsMixtureModelGibbs采样器有5个参数:数据集、先前创建的模型、初始赋值向量(通常将所有点赋给同一个簇)和细化因子。如果稀释因子为1,则保留所有样本;否则,在每个稀释样本中,仅保留一个样本,其余的立即丢弃。
- 巨蟒:
sampler=bmcc.GibbsMixtureModel(data=data,component_model=component_model,mixture_model=mixture_model,assignments=np.zeros(1000).astype(np.uint16),thinning=1)
:
sampler=GibbsMixtureModel(data=data,component_model=component_model,mixture_model=mixture_model,assignments=np_array(rep(0,1000),dtype="uint16"),thinning=1L)
最后,只需每次迭代调用iter
方法一次:
- 巨蟒:
library(devtools)install_github("https://github.com/thetianshuhuang/bmcc_r")
0
<
library(devtools)install_github("https://github.com/thetianshuhuang/bmcc_r")
1
您也可以用参数(即sampler.iter(10)
)调用iter立即。我建议用某种进度条来运行循环:
- 巨蟒:
library(devtools)install_github("https://github.com/thetianshuhuang/bmcc_r")
2
:
library(devtools)install_github("https://github.com/thetianshuhuang/bmcc_r")
3
选择结果
目前,只实现了最小二乘配置选择。通过调用具有磨合持续时间的GibbsMixtureModel
对象的select\lstsq
方法来运行。
- 巨蟒:
library(devtools)install_github("https://github.com/thetianshuhuang/bmcc_r")
4
:
library(devtools)install_github("https://github.com/thetianshuhuang/bmcc_r")
5
如果基本事实可用,则调用结果lstsqresult
对象的evaluate
方法来运行评估统计信息。如果Oracle信息可用,则可以选择传递Oracle
(Oracle赋值)和Oracle矩阵
(Oracle成对概率)参数以允许比较。
- 巨蟒:
library(devtools)install_github("https://github.com/thetianshuhuang/bmcc_r")
6
:
library(devtools)install_github("https://github.com/thetianshuhuang/bmcc_r")
7
参考文献
[1]Radford M.Neal(2000),"Dirichlet过程混合模型的马尔可夫链抽样方法"。计算与图形统计杂志,第9卷,第2期。
[2]Jeffrey W.Miller,Matthew T.Harrison(2018),"成分数量上具有先验的混合模型"。美国统计协会杂志,第113卷,521期。
[3]Jon D.McAuliffe,David M.Blei,Michael I.Jordan(2006),"Dirichlet过程混合模型的非参数经验Bayes"。统计与计算,第16卷,第1期。
[4]David B.Dahl(2006),"通过Dirichlet过程混合模型对表达式数据进行基于模型的聚类"。基因表达和蛋白质组学的贝叶斯推断。
推荐PyPI第三方库