本文整理匯總了Python中scipy.stats.multivariate_normal方法的典型用法代碼示例。如果您正苦於以下問題:Python stats.multivariate_normal方法的具體用法?Python stats.multivariate_normal怎麽用?Python stats.multivariate_normal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.stats
的用法示例。
在下文中一共展示了stats.multivariate_normal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: differential_entropies
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def differential_entropies(X, labels):
n_samples, n_features = X.shape
labels = np.array(labels)
names = sorted(set(labels))
entropies = []
for name in names:
name_idx = np.where(labels == name)[0]
gm = GaussianMixture().fit(X[name_idx, :])
mn = multivariate_normal(
mean=gm.means_.flatten(),
cov=gm.covariances_.reshape(n_features, n_features)
)
entropies.append(mn.entropy())
probs = softmax(entropies)
for name, entropy, prob in zip(names, entropies, probs):
#print('{}\t{}\t{}'.format(name, entropy, prob))
print('{}\t{}'.format(name, entropy))
示例2: precompute_marginals
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def precompute_marginals(self):
sys.stderr.write('Precomputing marginals...\n')
self._pdfs = [None] * self._num_instances
# precomputing all possible marginals
for i in xrange(self._num_instances):
mean = self._corrected_means[i]
cov = self._corrected_covs[i]
self._pdfs[i] = [None] * (2 ** mean.shape[0])
for marginal_pattern in itertools.product([False, True], repeat=mean.shape[0]):
marginal_length = marginal_pattern.count(True)
if marginal_length == 0:
continue
m = np.array(marginal_pattern)
marginal_mean = mean[m]
mm = m[:, np.newaxis]
marginal_cov = cov[np.dot(mm, mm.transpose())].reshape((marginal_length, marginal_length))
self._pdfs[i][hash_bool_array(m)] = multivariate_normal(mean=marginal_mean, cov=marginal_cov)
示例3: sample
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def sample(transition_matrix,
means, covs,
start_state, n_samples,
random_state):
n_states, n_features, _ = covs.shape
states = np.zeros(n_samples, dtype='int')
emissions = np.zeros((n_samples, n_features))
for i in range(n_samples):
if i == 0:
prev_state = start_state
else:
prev_state = states[i - 1]
state = random_state.choice(n_states,
p=transition_matrix[:, prev_state])
emissions[i] = random_state.multivariate_normal(means[state],
covs[state])
states[i] = state
return emissions, states
示例4: setUp_configure
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def setUp_configure(self):
from scipy import stats
self.dist = distributions.MultivariateNormal
self.scipy_dist = stats.multivariate_normal
self.scipy_onebyone = True
d, = self.event_shape
self.test_targets = set([
'batch_shape', 'entropy', 'event_shape', 'log_prob',
'support'])
loc = numpy.random.uniform(
-1, 1, self.shape + self.event_shape).astype(numpy.float32)
cov = numpy.random.normal(
size=(numpy.prod(self.shape),) + (d, d))
cov = [cov_.dot(cov_.T) for cov_ in cov]
cov = numpy.vstack(cov).reshape(self.shape + (d, d))
scale_tril = numpy.linalg.cholesky(cov).astype(numpy.float32)
self.params = {'loc': loc, 'scale_tril': scale_tril}
self.scipy_params = {'mean': loc, 'cov': cov}
示例5: _testPDFShapes
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def _testPDFShapes(self, mvn_dist, mu, sigma):
with self.test_session() as sess:
mvn = mvn_dist(mu, sigma)
x = 2 * tf.ones_like(mu)
log_pdf = mvn.log_pdf(x)
pdf = mvn.pdf(x)
mu_value = np.ones([3, 3, 2])
sigma_value = np.zeros([3, 3, 2, 2])
sigma_value[:] = np.identity(2)
x_value = 2. * np.ones([3, 3, 2])
feed_dict = {mu: mu_value, sigma: sigma_value}
scipy_mvn = stats.multivariate_normal(mean=mu_value[(0, 0)],
cov=sigma_value[(0, 0)])
expected_log_pdf = scipy_mvn.logpdf(x_value[(0, 0)])
expected_pdf = scipy_mvn.pdf(x_value[(0, 0)])
log_pdf_evaled, pdf_evaled = sess.run([log_pdf, pdf], feed_dict=feed_dict)
self.assertAllEqual([3, 3], log_pdf_evaled.shape)
self.assertAllEqual([3, 3], pdf_evaled.shape)
self.assertAllClose(expected_log_pdf, log_pdf_evaled[0, 0])
self.assertAllClose(expected_pdf, pdf_evaled[0, 0])
示例6: testLogPDFScalarBatch
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def testLogPDFScalarBatch(self):
with self.test_session():
mu = self._rng.rand(2)
chol, sigma = self._random_chol(2, 2)
mvn = distributions.MultivariateNormalCholesky(mu, chol)
x = self._rng.rand(2)
log_pdf = mvn.log_pdf(x)
pdf = mvn.pdf(x)
scipy_mvn = stats.multivariate_normal(mean=mu, cov=sigma)
expected_log_pdf = scipy_mvn.logpdf(x)
expected_pdf = scipy_mvn.pdf(x)
self.assertEqual((), log_pdf.get_shape())
self.assertEqual((), pdf.get_shape())
self.assertAllClose(expected_log_pdf, log_pdf.eval())
self.assertAllClose(expected_pdf, pdf.eval())
示例7: testLogPDFXIsHigherRank
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def testLogPDFXIsHigherRank(self):
with self.test_session():
mu = self._rng.rand(2)
chol, sigma = self._random_chol(2, 2)
mvn = distributions.MultivariateNormalCholesky(mu, chol)
x = self._rng.rand(3, 2)
log_pdf = mvn.log_pdf(x)
pdf = mvn.pdf(x)
scipy_mvn = stats.multivariate_normal(mean=mu, cov=sigma)
expected_log_pdf = scipy_mvn.logpdf(x)
expected_pdf = scipy_mvn.pdf(x)
self.assertEqual((3,), log_pdf.get_shape())
self.assertEqual((3,), pdf.get_shape())
self.assertAllClose(expected_log_pdf, log_pdf.eval())
self.assertAllClose(expected_pdf, pdf.eval())
示例8: testLogPDFXLowerDimension
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def testLogPDFXLowerDimension(self):
with self.test_session():
mu = self._rng.rand(3, 2)
chol, sigma = self._random_chol(3, 2, 2)
mvn = distributions.MultivariateNormalCholesky(mu, chol)
x = self._rng.rand(2)
log_pdf = mvn.log_pdf(x)
pdf = mvn.pdf(x)
self.assertEqual((3,), log_pdf.get_shape())
self.assertEqual((3,), pdf.get_shape())
# scipy can't do batches, so just test one of them.
scipy_mvn = stats.multivariate_normal(mean=mu[1, :], cov=sigma[1, :, :])
expected_log_pdf = scipy_mvn.logpdf(x)
expected_pdf = scipy_mvn.pdf(x)
self.assertAllClose(expected_log_pdf, log_pdf.eval()[1])
self.assertAllClose(expected_pdf, pdf.eval()[1])
示例9: testSampleWithSampleShape
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def testSampleWithSampleShape(self):
with self.test_session():
mu = self._rng.rand(3, 5, 2)
chol, sigma = self._random_chol(3, 5, 2, 2)
mvn = distributions.MultivariateNormalCholesky(mu, chol)
samples_val = mvn.sample((10, 11, 12), seed=137).eval()
# Check sample shape
self.assertEqual((10, 11, 12, 3, 5, 2), samples_val.shape)
# Check sample means
x = samples_val[:, :, :, 1, 1, :]
self.assertAllClose(
x.reshape(10 * 11 * 12, 2).mean(axis=0),
mu[1, 1], atol=1e-2)
# Check that log_prob(samples) works
log_prob_val = mvn.log_prob(samples_val).eval()
x_log_pdf = log_prob_val[:, :, :, 1, 1]
expected_log_pdf = stats.multivariate_normal(
mean=mu[1, 1, :], cov=sigma[1, 1, :, :]).logpdf(x)
self.assertAllClose(expected_log_pdf, x_log_pdf)
示例10: draw
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def draw(X, pred, means, covariances, output):
xp = cupy.get_array_module(X)
for i in range(2):
labels = X[pred == i]
if xp is cupy:
labels = labels.get()
plt.scatter(labels[:, 0], labels[:, 1], c=np.random.rand(1, 3))
if xp is cupy:
means = means.get()
covariances = covariances.get()
plt.scatter(means[:, 0], means[:, 1], s=120, marker='s', facecolors='y',
edgecolors='k')
x = np.linspace(-5, 5, 1000)
y = np.linspace(-5, 5, 1000)
X, Y = np.meshgrid(x, y)
for i in range(2):
dist = stats.multivariate_normal(means[i], covariances[i])
Z = dist.pdf(np.stack([X, Y], axis=-1))
plt.contour(X, Y, Z)
plt.savefig(output)
示例11: generate_dependent_normal_samples
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def generate_dependent_normal_samples(n, mu, mcorr, dvar):
d = mcorr.shape[1]
mvar = np.copy(mcorr)
np.fill_diagonal(mvar, 1.)
# logger.debug("mvar:\n%s" % str(mvar))
if d > 1:
for i in range(0, d-1):
for j in range(i+1, d):
mvar[j, i] = mvar[i, j]
p = np.diag(np.sqrt(dvar))
mvar = p.dot(mvar).dot(p)
rv = mvn(mu, mvar)
s = rv.rvs(size=n)
else:
s = np.random.normal(loc=mu[0], scale=np.sqrt(dvar[0]), size=n)
# logger.debug(str(list(s)))
s = np.reshape(s, (-1, 1))
if n == 1:
# handle the case where numpy automatically makes column vector if #rows is 1
s = np.reshape(s, (n, len(s)))
return s
示例12: illustrate_preprocessing
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def illustrate_preprocessing():
x = np.random.multivariate_normal(np.array([5.0,5.0]),
np.array([[5.0,3.0],[3.0,4.0]]),size=1000)
x_demean = x - np.mean(x, axis=0)
x_unitsd = x_demean/(np.std(x_demean,axis=0))
x_whiten = np.dot(x_demean, whitening_matrix(x_demean))
fig = pl.figure(figsize=(10,10))
def mk_subplot(n, data, label):
ax = fig.add_subplot(2,2,n)
ax.scatter(data[:,0], data[:,1])
ax.set_xlim((-10,10))
ax.set_ylim((-10,10))
ax.set_xlabel(label)
mk_subplot(1, x, "Original")
mk_subplot(2, x_demean, "De-meaned")
mk_subplot(3, x_unitsd, "Unit SD")
mk_subplot(4, x_whiten, "Whitened")
pl.show()
示例13: __init__
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def __init__(self, mean=2, cov=None, ndim_x=1, ndim_y=1, has_cdf=True, has_pdf=True, can_sample=True):
self.ndim_x = ndim_x
self.ndim_y = ndim_y
self.ndim = self.ndim_x + self.ndim_y
self.mean = mean
# check if mean is scalar
if isinstance(self.mean, list):
self.mean = np.array(self.ndim_y * [self.mean])
self.cov = cov
if self.cov is None:
self.cov = np.identity(self.ndim_y)
assert self.cov.shape[0] == self.cov.shape[1] == self.ndim_y
# safe data stats
self.y_mean = self.mean
self.y_std = np.sqrt(np.diagonal(self.cov))
self.gaussian = stats.multivariate_normal(mean=self.mean, cov=self.cov)
self.fitted = False
self.can_sample = can_sample
self.has_pdf = has_pdf
self.has_cdf = has_cdf
示例14: _build_model
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def _build_model(self, X, Y):
# save mean and std of data for normalization
self.x_std = np.std(X, axis=0)
self.x_mean = np.mean(X, axis=0)
self.y_mean = np.std(Y, axis=0)
self.y_std = np.std(Y, axis=0)
self.n_train_points = X.shape[0]
# lazy learner - just store training data
self.X_train = self._normalize_x(X)
self.Y_train = Y
# prepare Gaussians centered in the Y points
self.locs_array = np.vsplit(Y, self.n_train_points)
self.log_kernel = multivariate_normal(mean=np.ones(self.ndim_y)).logpdf
# select / properly initialize bandwidth and epsilon
if isinstance(self.bandwidth, (int, float)):
self.bandwidth = self.y_std * self.bandwidth
if self.param_selection == 'normal_reference':
self.bandwidth = self._normal_reference()
elif self.param_selection == 'cv_ml':
self.bandwidth, self.epsilon = self._cv_ml()
示例15: _build_model
# 需要導入模塊: from scipy import stats [as 別名]
# 或者: from scipy.stats import multivariate_normal [as 別名]
def _build_model(self, X, Y):
# save mean and variance of data for normalization
self.x_mean, self.y_mean = np.mean(X, axis=0), np.mean(Y, axis=0)
self.x_std, self.y_std = np.std(X, axis=0), np.std(Y, axis=0)
# get locations of the gaussian kernel centers
if self.center_sampling_method == 'all':
self.n_centers = X.shape[0]
else:
self.n_centers = min(self.n_centers, X.shape[0])
n_locs = self.n_centers
X_Y_normalized = np.concatenate(list(self._normalize(X, Y)), axis=1)
centroids = sample_center_points(X_Y_normalized, method=self.center_sampling_method, k=n_locs,
keep_edges=self.keep_edges, random_state=self.random_state)
self.centr_x = centroids[:, 0:self.ndim_x]
self.centr_y = centroids[:, self.ndim_x:]
#prepare gaussians for sampling
self.gaussians_y = [stats.multivariate_normal(mean=center, cov=self.bandwidth) for center in self.centr_y]
assert self.centr_x.shape == (n_locs, self.ndim_x) and self.centr_y.shape == (n_locs, self.ndim_y)