小编Mac*_*ack的帖子

关联代理SQLAlchemy

此源详细说明了如何使用关联代理创建具有ORM对象值的视图和对象.

但是,当我追加一个与数据库中现有对象匹配的值(并且所述值是唯一的或主键)时,它会创建一个冲突的对象,因此我无法提交.

所以在我的情况下这只是一个有用的视图,我需要使用ORM查询来检索要追加的对象.

这是我唯一的选择,还是我可以使用merge(如果它是主键而不是唯一约束,我可能只能这样做),或者设置构造函数,使它在数据库中使用现有对象(如果存在)而不是创建一个新对象?

例如来自文档:

user.keywords.append('cheese inspector')

# Is translated by the association proxy into the operation:

user.kw.append(Keyword('cheese inspector'))
Run Code Online (Sandbox Code Playgroud)

但是我想要翻译成更像的东西:(当然查询可能会失败).

keyword = session.query(Keyword).filter(Keyword.keyword == 'cheese inspector').one()
user.kw.append(keyword)
Run Code Online (Sandbox Code Playgroud)

或者理想情况下

user.kw.append(Keyword('cheese inspector'))
session.merge() # retrieves identical object from the database, or keeps new one
session.commit() # success!
Run Code Online (Sandbox Code Playgroud)

我想这可能不是一个好主意,但它可能在某些用例:)

sqlalchemy

7
推荐指数
1
解决办法
4549
查看次数

如何在 Python 中对多元对数正态分布进行采样?

使用 Python,如何从多元对数正态分布中采样数据?例如,对于多元正态分布,有两个选项。假设我们有一个 3 x 3 协方差矩阵和一个 3 维均值向量 mu。

# Method 1
sample = np.random.multivariate_normal(mu, covariance)

# Method 2
L = np.linalg.cholesky(covariance)
sample = L.dot(np.random.randn(3)) + mu
Run Code Online (Sandbox Code Playgroud)

我发现 numpy 的numpy.random.lognormal,但这似乎只适用于单变量样本。我还注意到 scipy 的scipy.stats.lognorm。这似乎确实具有多元样本的潜力。但是,我不知道该怎么做。

python statistics numpy probability scipy

7
推荐指数
1
解决办法
7200
查看次数

使用`LKJCorr`先验修改PyMC3中的BPMF:使用`NUTS的PositiveDefiniteError

我之前实现了原始的贝叶斯概率矩阵分解(BPMF)模型pymc3.请参阅我之前的问题以供参考,数据源和问题设置.根据@twiecki对该问题的回答,我已经使用priors实现了模型的变体,LKJCorr用于相关矩阵和标准偏差的均匀先验.在原始模型中,协方差矩阵是从Wishart分布中提取的,但由于当前的局限性pymc3,Wishart分布无法从中正确采样.这个松散相关问题的答案为LKJCorr先验的选择提供了简洁的解释.新模型如下.

import pymc3 as pm
import numpy as np
import theano.tensor as t


n, m = train.shape
dim = 10  # dimensionality
beta_0 = 1  # scaling factor for lambdas; unclear on its use
alpha = 2  # fixed precision for likelihood function
std = .05  # how much noise to use for model initialization

# We will use separate priors for sigma and …
Run Code Online (Sandbox Code Playgroud)

python bayesian mcmc pymc pymc3

6
推荐指数
1
解决办法
933
查看次数

`id` 属性是否需要在 iframe 中唯一?

例如,这是有效的吗?

<!doctype html>
<html>

  <head>
    <title>Some Iframes</title>
  </head>

  <body>
    <iframe id="frame1" src="/html/test-frame.html"></iframe>
    <iframe id="frame2" src="/html/test-frame.html"></iframe>
  </body>

</html>
Run Code Online (Sandbox Code Playgroud)

该文件test-frame.html包含以下内容:

<!doctype html>
<html>

  <head>
    <title>Test Iframe</title>
  </head>

  <body>
    <button id="subscribe">Subscribe</button>
  </body>

 </html>
Run Code Online (Sandbox Code Playgroud)

我希望能够对一组 iframe 使用相同的 html 和相同的脚本。

html iframe

5
推荐指数
1
解决办法
1639
查看次数

带有PyMC3的贝叶斯概率矩阵分解(BPMF):使用`NUTS的PositiveDefiniteError

我在Python中实现了贝叶斯概率矩阵分解算法pymc3.我还实现了它的前驱,概率矩阵分解(PMF).请参阅我之前的问题,以获取此处使用的数据的参考.

我在使用NUTS采样器绘制MCMC样本时遇到问题.我使用来自PMF的MAP初始化模型参数,使用高斯随机抽取的超参数在0附近散布.但是,我PositiveDefiniteError在为采样器设置步骤对象时得到了一个.我已经验证了PMF的MAP估计是合理的,所以我希望它与超参数初始化的方式有关.这是PMF模型:

import pymc3 as pm
import numpy as np
import pandas as pd
import theano
import scipy as sp

data = pd.read_csv('jester-dense-subset-100x20.csv')    
n, m = data.shape
test_size = m / 10
train_size = m - test_size

train = data.copy()
train.ix[:,train_size:] = np.nan  # remove test set data
train[train.isnull()] = train.mean().mean()  # mean value imputation
train = train.values

test = data.copy()
test.ix[:,:train_size] = np.nan  # remove train set data
test = test.values …
Run Code Online (Sandbox Code Playgroud)

bayesian mcmc pymc

5
推荐指数
1
解决办法
982
查看次数

在 Python 中使用 Peewee 在 SQL DDL 中使用 SQLite 触发器和日期时间默认值

我有一个 SQLite 表,定义如下:

create table if not exists KeyValuePair (
    key         CHAR(255) primary key not null,
    val         text not null,
    fup         timestamp default current_timestamp not null,  -- time of first upload
    lup         timestamp default current_timestamp not null  -- time of last upload
);

create trigger if not exists entry_first_insert after insert
on KeyValuePair
begin
    update KeyValuePair set lup = current_timestamp where key = new.key;
end;

create trigger if not exists entry_last_updated after update of value
on KeyValuePair
begin
    update KeyValuePair …
Run Code Online (Sandbox Code Playgroud)

python sqlite peewee

5
推荐指数
1
解决办法
2016
查看次数

标签 统计

python ×3

bayesian ×2

mcmc ×2

pymc ×2

html ×1

iframe ×1

numpy ×1

peewee ×1

probability ×1

pymc3 ×1

scipy ×1

sqlalchemy ×1

sqlite ×1

statistics ×1