后向数据库的python接口

posteriordb的Python项目详细描述


Python版本

目前只支持Python3.6+。如果需要,可以添加python3.5+支持。我们不打算支持Python2。在

安装

建议从PyPI安装。在

pip install posteriordb

从本地克隆安装。在

^{pr2}$

使用python的后部数据库

包含的数据库包含访问数据、模型代码和个别后验者信息的方便函数。在

首先我们创建后验数据库来使用,这里是克隆的后验数据库。在

>>>fromposteriordbimportPosteriorDatabase>>>importos>>>pdb_path=os.path.join(os.getcwd(),"posterior_database")>>>my_pdb=PosteriorDatabase(pdb_path)

上面的代码要求您的工作目录位于副本的主文件夹中 这个项目的。或者,可以直接指定文件夹的路径。在

联机数据库可以与PosteriorDatabaseGithub类一起使用。记住创建并设置GITHUB_PAT环境变量。 建议用户为posteriordb创建一个只读(没有额外权限)GitHub Personal Access Token (PAT)。我们还建议 GITHUB_PAT环境变量被添加到用户环境变量中,并且在Python脚本中没有显示,如下例所示。在

如果没有显式定义,PosteriorDatabasePosteriorDatabaseGithub将创建一个位于POSTERIOR_DB_PATH的新数据库(或使用旧数据库) 定义。^如果没有设置环境变量,{}将最终使用$HOME/.posteriordb/posterior_database作为后备位置。 每个模型和数据只在需要时下载和缓存。在

>>>fromposteriordbimportPosteriorDatabaseGithub>>>importos>>># It is recommended that GITHUB_PAT is added to the user environmental variables>>># outside python and not in a python script as shown in this example code>>>os.environ["GITHUB_PAT"]="token-string-here">>>my_pdb=PosteriorDatabaseGithub()

要列出可用的后验,请使用posterior_names。在

>>>pos=my_pdb.posterior_names()>>>pos[:5]['roaches-roaches_negbin','syn_gmK2D1n200-gmm_diagonal_nonordered','radon_mn-radon_variable_intercept_centered','syn_gmK3D2n300-gmm_nonordered','radon-radon_hierarchical_intercept_centered']

同样,我们可以将数据库中包含的数据和模型列为

>>>mn=my_pdb.model_names()>>>mn[:5]['gmm_diagonal_nonordered','radon_pool','radon_partial_pool_noncentered','blr','radon_hierarchical_intercept_noncentered']>>>dn=my_pdb.dataset_names()>>>dn[:5]['radon_mn','wells_centered','radon','wells_centered_educ4_interact','wells_centered_educ4']

后部的名称由数据和模型拟合组成 数据。这两个因素共同决定了后验分布。 要进入后面的物体,我们可以用后面的名字。在

>>>posterior=my_pdb.posterior("eight_schools-eight_schools_centered")

从后面我们可以访问数据集和模型

>>>model=posterior.model>>>data=posterior.data

我们还可以访问后验、模型和数据集的名称。在

>>>posterior.name"eight_schools-eight_schools_centered">>>model.name"eight_schools_centered">>>data.name"eight_schools"

我们也可以直接从后部数据库访问相同的模型和数据集

>>>model=my_pdb.model("eight_schools_centered")>>>data=my_pdb.data("eight_schools")

从模型中我们可以访问模型代码和有关模型的信息

>>>model.code("stan")data{int<lower=0>J;//numberofschoolsrealy[J];//estimatedtreatmentreal<lower=0>sigma[J];//stdofestimatedeffect}parameters{realtheta[J];//treatmenteffectinschooljrealmu;//hyper-parameterofmeanreal<lower=0>tau;//hyper-parameterofsdv}model{tau~cauchy(0,5);//anon-informativepriortheta~normal(mu,tau);y~normal(theta,sigma);mu~normal(0,5);}>>>model.code_file_path("stan")'/home/eero/posterior_database/content/models/stan/eight_schools_centered.stan'>>>model.information{'keywords':['bda3_example','hiearchical'],'description':'A centered hiearchical model for the 8 schools example of Rubin (1981)','urls':['http://www.stat.columbia.edu/~gelman/arm/examples/schools'],'title':'A centered hiearchical model for 8 schools','references':['rubin1981estimation','gelman2013bayesian'],'added_by':'Mans Magnusson','added_date':'2019-08-12'}

注意,这些引用引用引用的是可以在content/references/references.bib中找到的BibTeX项。在

从数据集中我们可以访问数据值和有关它的信息

>>>data.values(){'J':8,'y':[28,8,-3,7,-1,1,18,12],'sigma':[15,10,16,11,9,11,10,18]}>>>data.file_path()'/tmp/tmpx16edu0w'>>>data.information{'keywords':['bda3_example'],'description':'A study for the Educational Testing Service to analyze the effects of\nspecial coaching programs on test scores. See Gelman et. al. (2014), Section 5.5 for details.','urls':['http://www.stat.columbia.edu/~gelman/arm/examples/schools'],'title':'The 8 schools dataset of Rubin (1981)','references':['rubin1981estimation','gelman2013bayesian'],'added_by':'Mans Magnusson','added_date':'2019-08-12'}

为了获得黄金标准的后牵引,我们可以使用reference_draws,如下所示。在

>>>posterior.reference_draws_info(){'name':'eight_schools-eight_schools_noncentered','inference':{'method':'stan_sampling','method_arguments':{'chains':10,'iter':20000,'warmup':10000,'thin':10,'seed':4711,'control':{'adapt_delta':0.95}}},'diagnostics':{'diagnostic_information':{'names':['mu','tau','theta[1]',...>>>gs=posterior.reference_draws()>>>importpandasaspd>>>pd.DataFrame(gs)theta[1]theta[2]0[10.6802773011458,6.45383910854259,-2.241629...[9.71770681295263,4.41030824418493,0.7617047...1[5.70891361633589,10.3012059848039,4.2439533...[-2.32310565394337,14.8121789773659,6.517256...2[7.23747096507585,-0.427831558524343,9.14782...[7.35425759420389,8.69579738064637,8.9058764...3[4.44915522912766,2.34711393762556,17.680378...[2.4368039319606,5.89809320808632,8.63031558......

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Cassandra复制因子大于节点数   java J2EE JTA事务回滚不适用于OSE Glassfish 4.0(Build 89)   java spring安全预认证用户登录   org的java类文件。反应流。从RxJava编译示例时未找到Publisher?   java在使用dataFormat作为POJO通过Camel调用Web服务时无法设置SOAP标头   Javafx类的java静态实例   java如何防止一个部件在关闭时覆盖另一个部件的位置   sql server无法从我的java代码连接到数据库   java在JList(Swing)中显示带有的ArrayList   从Java中的CXF服务获取WSAddressing数据   使用资产文件夹进行java简单json解析(本地)   java LDAPException未绑定的无效凭据   JavaJSFspring部署到weblogic   JAVA中字符数组中的特定元素排列?   如果脚本位于不同的目录中,则ant不会使用exec标记运行Javashell脚本