本文整理汇总了Python中numpy.diag函数的典型用法代码示例。如果您正苦于以下问题:Python diag函数的具体用法?Python diag怎么用?Python diag使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了diag函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _dpmm
def _dpmm(coords, alpha, null_density, dof, prior_precision, prior_h0,
subjects, sampling_coords=None, n_iter=1000, burnin=100,
co_clust=False):
"""Apply the dpmm analysis to compute clusters from regions coordinates
"""
from nipy.algorithms.clustering.imm import MixedIMM
dim = coords.shape[1]
migmm = MixedIMM(alpha, dim)
migmm.set_priors(coords)
migmm.set_constant_densities(
null_dens=null_density, prior_dens=null_density)
migmm._prior_dof = dof
migmm._prior_scale = np.diag(prior_precision[0] / dof)
migmm._inv_prior_scale_ = [np.diag(dof * 1. / (prior_precision[0]))]
migmm.sample(coords, null_class_proba=prior_h0, niter=burnin, init=False,
kfold=subjects)
# sampling
like, pproba, co_clustering = migmm.sample(
coords, null_class_proba=prior_h0, niter=n_iter, kfold=subjects,
sampling_points=sampling_coords, co_clustering=True)
if co_clust:
return like, 1 - pproba, co_clustering
else:
return like, 1 - pproba
开发者ID:Lx37,项目名称:nipy,代码行数:27,代码来源:bayesian_structural_analysis.py
示例2: grad_EVzxVzxT_by_hyper_exact
def grad_EVzxVzxT_by_hyper_exact(self, EVzxVzxT_list_this, Z, A, B, hyperno):
P = Z.shape[0]
R = Z.shape[1]
N = A.shape[0]
if hyperno != 0:
return EVzxVzxT_list_this * 0
alpha = self.length_scale * self.length_scale
I = np.identity(R)
S = np.diag(B[0, :] * B[0, :])
Sinv = np.diag(1 / B[0, :] * B[0, :])
C = I * alpha
Cinv = I * (1 / alpha)
CinvSinv = 2 * Cinv + Sinv
CinvSinv_inv = np.diag(1 / CinvSinv.diagonal())
dC = self.length_scale * I
dCinv = -Cinv.dot(dC).dot(Cinv)
dCinvSinv = 2 * dCinv
dCinvSinv_inv = -CinvSinv_inv.dot(dCinvSinv).dot(CinvSinv_inv)
S1 = (
dCinv
- dCinv.dot(CinvSinv_inv).dot(Cinv)
- Cinv.dot(dCinvSinv_inv).dot(Cinv)
- Cinv.dot(CinvSinv_inv).dot(dCinv)
)
S2 = -Sinv.dot(dCinvSinv_inv).dot(Sinv)
S3 = Sinv.dot(dCinvSinv_inv).dot(Cinv) + Sinv.dot(CinvSinv_inv).dot(dCinv)
S4 = dCinv.dot(CinvSinv_inv).dot(Cinv) + Cinv.dot(dCinvSinv_inv).dot(Cinv) + Cinv.dot(CinvSinv_inv).dot(dCinv)
T1s = np.tile(Z.dot(S1).dot(Z.T).diagonal(), [P, 1])
T1 = np.tile(T1s, [N, 1, 1])
T2s = T1s.T
T2 = np.tile(T2s, [N, 1, 1])
T3 = np.tile(Z.dot(S4).dot(Z.T), [N, 1, 1])
T4 = np.tile(A.dot(S2).dot(A.T).diagonal(), [P, 1]).T
T4 = np.expand_dims(T4, axis=2)
T4 = np.repeat(T4, P, axis=2)
T5 = A.dot(S3).dot(Z.T)
T5 = np.expand_dims(T5, axis=2)
T5 = np.repeat(T5, P, axis=2)
T6 = np.swapaxes(T5, 1, 2)
SCinvI = 2 * Cinv.dot(S) + I
SCinvI_inv = np.diag(1 / SCinvI.diagonal())
(temp, logDetSCinvI) = np.linalg.slogdet(SCinvI)
detSCinvI = np.exp(logDetSCinvI)
dDetSCinvI = -0.5 * np.power(detSCinvI, -0.5) * SCinvI_inv.dot(2 * dCinv).dot(S).trace()
expTerm = EVzxVzxT_list_this / np.power(detSCinvI, -0.5)
res = EVzxVzxT_list_this * (-0.5 * T1 - 0.5 * T2 + T3 - 0.5 * T4 + T5 + T6) + dDetSCinvI * expTerm
res = np.sum(res, axis=0)
return res
开发者ID:LinZhineng,项目名称:atldgp,代码行数:60,代码来源:RBFKernel.py
示例3: Haffine_from_points
def Haffine_from_points(fp, tp):
'''计算仿射变换的单应性矩阵H,使得tp是由fp经过仿射变换得到的'''
if fp.shape != tp.shape:
raise RuntimeError('number of points do not match')
# 对点进行归一化
# 映射起始点
m = numpy.mean(fp[:2], axis=1)
maxstd = numpy.max(numpy.std(fp[:2], axis=1)) + 1e-9
C1 = numpy.diag([1/maxstd, 1/maxstd, 1])
C1[0, 2] = -m[0] / maxstd
C1[1, 2] = -m[1] / maxstd
fp_cond = numpy.dot(C1, fp)
# 映射对应点
m = numpy.mean(tp[:2], axis=1)
maxstd = numpy.max(numpy.std(tp[:2], axis=1)) + 1e-9
C2 = numpy.diag([1/maxstd, 1/maxstd, 1])
C2[0, 2] = -m[0] / maxstd
C2[1, 2] = -m[1] / maxstd
tp_cond = numpy.dot(C2, tp)
# 因为归一化之后点的均值为0,所以平移量为0
A = numpy.concatenate((fp_cond[:2], tp_cond[:2]), axis=0)
U, S, V = numpy.linalg.svd(A.T)
# 创建矩阵B和C
tmp = V[:2].T
B = tmp[:2]
C = tmp[2:4]
tmp2 = numpy.concatenate((numpy.dot(C, numpy.linalg.pinv(B)), numpy.zeros((2, 1))), axis=1)
H = numpy.vstack((tmp2, [0, 0, 1]))
H = numpy.dot(numpy.linalg.inv(C2), numpy.dot(H, C1)) # 反归一化
return H / H[2, 2] # 归一化,然后返回
开发者ID:MarkPrecursor,项目名称:Programming-Computer-Vision-with-python,代码行数:35,代码来源:homography.py
示例4: objective_gradient
def objective_gradient(self, X, J=None, return_K=False):
"""
Computes MPF objective gradient on input data X given coupling
strengths J.
Parameters
----------
X : numpy array
(M, N)-dim array of binary input patterns of length N,
where N is the number of nodes in the network
J : numpy array, optional
Coupling matrix of size N x N, where N denotes the number
of nodes in the network (default None)
return_K : bool, optional
Flag wether to return K (default False)
Returns
-------
dJ [, K] : numpy array [, numpy array]
Update to coupling matrix J [and K if return_K is True]
"""
if J is None:
J = self._J
J[np.eye(self._N, dtype=bool)] = -2 * self._theta
X = np.atleast_2d(X)
M, N = X.shape
S = 2 * X - 1
Kfull = np.exp(-S * np.dot(X, J.T) + .5 * np.diag(J)[None, :])
dJ = -np.dot(X.T, Kfull * S) + .5 * np.diag(Kfull.sum(0))
if self._symmetric is True:
dJ = .5 * (dJ + dJ.T)
if return_K:
return Kfull.sum() / M, dJ / M
else:
return dJ / M
开发者ID:team-hdnet,项目名称:hdnet,代码行数:35,代码来源:hopfield.py
示例5: svdUpdate
def svdUpdate(U, S, V, a, b):
"""
Update SVD of an (m x n) matrix `X = U * S * V^T` so that
`[X + a * b^T] = U' * S' * V'^T`
and return `U'`, `S'`, `V'`.
`a` and `b` are (m, 1) and (n, 1) rank-1 matrices, so that svdUpdate can simulate
incremental addition of one new document and/or term to an already existing
decomposition.
"""
rank = U.shape[1]
m = U.T * a
p = a - U * m
Ra = numpy.sqrt(p.T * p)
assert float(Ra) > 1e-10
P = (1.0 / float(Ra)) * p
n = V.T * b
q = b - V * n
Rb = numpy.sqrt(q.T * q)
assert float(Rb) > 1e-10
Q = (1.0 / float(Rb)) * q
K = numpy.matrix(numpy.diag(list(numpy.diag(S)) + [0.0])) + numpy.bmat("m ; Ra") * numpy.bmat(" n; Rb").T
u, s, vt = numpy.linalg.svd(K, full_matrices=False)
tUp = numpy.matrix(u[:, :rank])
tVp = numpy.matrix(vt.T[:, :rank])
tSp = numpy.matrix(numpy.diag(s[:rank]))
Up = numpy.bmat("U P") * tUp
Vp = numpy.bmat("V Q") * tVp
Sp = tSp
return Up, Sp, Vp
开发者ID:beibeiyang,项目名称:Latent-Dirichlet-Allocation,代码行数:31,代码来源:lsimodel.py
示例6: psi
def psi(self, x, y):
# x is unaries
# y is a labeling
## unary features:
gx, gy = np.ogrid[:x.shape[0], :x.shape[1]]
selected_unaries = x[gx, gy, y]
unaries_acc = np.sum(x[gx, gy, y])
unaries_acc = np.bincount(y.ravel(), selected_unaries.ravel(),
minlength=self.n_states)
##accumulated pairwise
#make one hot encoding
labels = np.zeros((y.shape[0], y.shape[1], self.n_states),
dtype=np.int)
gx, gy = np.ogrid[:y.shape[0], :y.shape[1]]
labels[gx, gy, y] = 1
# vertical edges
vert = np.dot(labels[1:, :, :].reshape(-1, self.n_states).T,
labels[:-1, :, :].reshape(-1, self.n_states))
# horizontal edges
horz = np.dot(labels[:, 1:, :].reshape(-1, self.n_states).T, labels[:,
:-1, :].reshape(-1, self.n_states))
pw = vert + horz
pw = pw + pw.T - np.diag(np.diag(pw))
feature = np.hstack([unaries_acc, pw[np.tri(self.n_states,
dtype=np.bool)]])
return feature
开发者ID:cshen,项目名称:pystruct,代码行数:27,代码来源:crf.py
示例7: get_eq_from_eig
def get_eq_from_eig(m):
''' get the equilibrium frequencies from the matrix. the eq freqs are the left eigenvector corresponding to eigenvalue of 0.
Code here is largely taken from Bloom. See here - https://github.com/jbloom/phyloExpCM/blob/master/src/submatrix.py, specifically in the fxn StationaryStates
'''
(w, v) = linalg.eig(m, left=True, right=False)
max_i = 0
max_w = w[max_i]
for i in range(1, len(w)):
if w[i] > max_w:
max_w = w[i]
max_i = i
assert( abs(max_w) < ZERO ), "Maximum eigenvalue is not close to zero."
max_v = v[:,max_i]
max_v /= np.sum(max_v)
eq_freqs = max_v.real # these are the stationary frequencies
# SOME SANITY CHECKS
assert np.allclose(np.zeros(61), np.dot(eq_freqs, m)) # should be true since eigenvalue of zero
pi_inv = np.diag(1.0 / eq_freqs)
s = np.dot(m, pi_inv)
assert np.allclose(m, np.dot(s, np.diag(eq_freqs)), atol=ZERO, rtol=1e-5), "exchangeability and equilibrium does not recover matrix"
# And for some impressive overkill, double check pi_i*q_ij = pi_j*q_ji
for i in range(61):
pi_i = eq_freqs[i]
for j in range(61):
pi_j = eq_freqs[j]
forward = pi_i * m[i][j]
backward = pi_j * m[j][i]
assert(abs(forward - backward) < ZERO), "Detailed balance violated."
return eq_freqs
开发者ID:sjspielman,项目名称:dnds_1rate_2rate,代码行数:31,代码来源:function_library.py
示例8: test_diags_vs_diag
def test_diags_vs_diag(self):
# Check that
#
# diags([a, b, ...], [i, j, ...]) == diag(a, i) + diag(b, j) + ...
#
np.random.seed(1234)
for n_diags in [1, 2, 3, 4, 5, 10]:
n = 1 + n_diags//2 + np.random.randint(0, 10)
offsets = np.arange(-n+1, n-1)
np.random.shuffle(offsets)
offsets = offsets[:n_diags]
diagonals = [np.random.rand(n - abs(q)) for q in offsets]
mat = construct.diags(diagonals, offsets)
dense_mat = sum([np.diag(x, j) for x, j in zip(diagonals, offsets)])
assert_array_almost_equal_nulp(mat.todense(), dense_mat)
if len(offsets) == 1:
mat = construct.diags(diagonals[0], offsets[0])
dense_mat = np.diag(diagonals[0], offsets[0])
assert_array_almost_equal_nulp(mat.todense(), dense_mat)
开发者ID:7924102,项目名称:scipy,代码行数:26,代码来源:test_construct.py
示例9: R_from_homography
def R_from_homography(H, f1, f2):
K1 = np.diag([f1, f1, 1])
K2 = np.diag([f2, f2, 1])
K2inv = np.linalg.inv(K2)
R = K2inv.dot(H).dot(K1)
R = project_to_rotation_matrix(R)
return R
开发者ID:dakotabenjamin,项目名称:OpenSfM,代码行数:7,代码来源:multiview.py
示例10: genGraph
def genGraph(S_actual, S_est, S_previous, empCov_set, nodeID, e1, e2, e3, e4, display = False):
D = np.where(S_est != 0)[0].shape[0]
T = np.where(S_actual != 0)[0].shape[0]
TandD = float(np.where(np.logical_and(S_actual,S_est) == True)[0].shape[0])
P = TandD/D
R = TandD/T
offDiagDiff = S_actual - S_est
offDiagDiff = offDiagDiff - np.diag(np.diag(offDiagDiff))
S_diff = (S_est - S_previous)
S_diff = S_diff - np.diag(np.diag(S_diff))
ind = (S_diff < 1e-2) & (S_diff > - 1e-2)
S_diff[ind] = 0
K = np.count_nonzero(S_diff)
e1.append( alg.norm(offDiagDiff, 'fro'))
e2.append(2* P*R/(P+R))
K = float(np.where(np.logical_and((S_est>0) != (S_previous>0), S_est>0) == True)[0].shape[0])
e3.append(-np.log(alg.det(S_est)) + np.trace(np.dot(S_est, empCov_set[nodeID])) + K)
e4.append(alg.norm(S_est - S_previous, 'fro'))
display = False
if display == True:
if (nodeID >timeShift -10) and (nodeID < timeShift + 10):
print 'nodeID = ', nodeID
print 'S_true = ', S_actual,'\nS_est', S_est
# print 'S_error = ',S_actual - S_est, '\n its Fro error = ', alg.norm(S_actual - S_est, 'fro')
print 'D = ',D,'T = ', T,'TandD = ', TandD,'K = ', K,'P = ', P,'R = ', R,'Score = ', 2* P*R/(P+R)
return e1, e2, e3, e4
开发者ID:lucasant10,项目名称:Twitter,代码行数:30,代码来源:SynGraphL2.py
示例11: integration_recruitment
def integration_recruitment(MA, S):
'''
Input Module-Allegiance "MA" and community strucutre "S"
Output Integration and Recruitment
'''
# transform S to a column vector
if min(S) == 1:
S = S-1
if np.shape(S)[0] == 1:
S = S.T
MA = np.double(MA)
num_node = len(S)
num_cl = max(S)+1
H = np.zeros(shape=(num_node, num_cl), dtype = np.double)
for i in range(num_cl):
H[:,i] = (S==i)
D_H = (H.T).dot(H)
recruitment = np.zeros(shape = (num_cl, num_cl))
integration = np.zeros(shape = (num_cl, num_cl))
D_H_Inv = linalg.inv(D_H)
recruitment = D_H_Inv.dot(H.T).dot(MA).dot(H).dot(D_H_Inv)
D = np.diag(np.diag(recruitment))
D_Inv_Sqr = np.power(D, -0.5)
integration = D_Inv_Sqr.dot(recruitment).dot(D_Inv_Sqr)
return (integration,recruitment)
开发者ID:nangongwubu,项目名称:Python-Version-for-Network-Community-Architecture-Toobox,代码行数:30,代码来源:ncat.py
示例12: sig_lmc
def sig_lmc(C, A):
'''
This a function that using Lumped Markov chain to calculate
the significance of clusters in a give communinity structure.
refer to "Piccardi 2011 in PloS one".
Here we normalize the original definition of persistence by
the size of the corresponding cluster to get a better
INPUT:
"A" is a N-by-N weighted adjacency matrix
"C" is a N-by-1 partition(cluster) vector
OUTPUT:
normalized persistence probability of all clusters
'''
'''
Transition Matrix
'''
C = np.asarray(C)
A = np.double(A)
P = np.linalg.solve(np.diag(np.sum(A,axis = 1)),A)
[eval, evec] = linalg.eigs(P.T, 1)
if min(evec)<0:
evec = -evec
pi = np.double(evec.T)
num_node = np.double(np.shape(A)[0])
cl_label = np.double(np.unique(C))
num_cl = len(cl_label)
H = np.zeros((num_node, num_cl),dtype = np.double)
for i in range(num_cl):
H[:, i] = np.double((C==cl_label[i]))
# Transition matrix of the lumped Markov chain
Q = np.dot(np.dot(np.dot(np.linalg.solve(np.diag(np.dot(pi,H).flatten()),H.T),np.diag(pi.flatten())),P),H)
persistence = np.multiply(np.divide(np.diag(Q), np.sum(H,axis = 0)),np.sum(H))
return persistence
开发者ID:nangongwubu,项目名称:Python-Version-for-Network-Community-Architecture-Toobox,代码行数:35,代码来源:ncat.py
示例13: initialize_hypers
def initialize_hypers(self, W):
mu_0 = W.mean(axis=(0,1))
sigma_0 = np.diag(W.var(axis=(0,1)))
# Set the global cov
nu_0 = self._cov_model.nu_0
self._cov_model.sigma_0 = sigma_0 * (nu_0 - self.B - 1)
# Set the mean
for c1 in xrange(self.C):
for c2 in xrange(self.C):
self._gaussians[c1][c2].mu_0 = mu_0
self._gaussians[c1][c2].sigma = self._cov_model.sigma_0
self._gaussians[c1][c2].resample()
if self.special_case_self_conns:
W_self = W[np.arange(self.N), np.arange(self.N)]
self._self_gaussian.mu_0 = W_self.mean(axis=0)
self._self_gaussian.sigma_0 = np.diag(W_self.var(axis=0))
self._self_gaussian.resample()
# Cluster the neurons based on their rows and columns
from sklearn.cluster import KMeans
features = np.hstack((W[:,:,0], W[:,:,0].T))
km = KMeans(n_clusters=self.C)
km.fit(features)
self.c = km.labels_.astype(np.int)
print "Initial c: ", self.c
开发者ID:slinderman,项目名称:graphistician,代码行数:29,代码来源:weights.py
示例14: generate_time_of_use_periods
def generate_time_of_use_periods(self):
"""
time of use periods will be described by NxM indicator matricies
"""
N = const.DAILY_UNITS
quarters = self.generate_quarter_hours()
peak_indicator = [1 if ( (t >= const.PEAK_TIME_RANGE[0]) & (t < const.PEAK_TIME_RANGE[1])) else 0 for t in quarters]
part_peak_indicator = [1 if ( (t >= const.PART_PEAK_TIME_RANGE[0][0]) and (t < const.PART_PEAK_TIME_RANGE[0][1])
or t >= const.PART_PEAK_TIME_RANGE[1][0]) and (t < const.PART_PEAK_TIME_RANGE[1][1]) else 0 for t in quarters]
off_peak_indicator = [1 if ( (t >= const.OFF_PEAK_TIME_RANGE[0][0]) and (t < const.OFF_PEAK_TIME_RANGE[0][1])
or t >= const.OFF_PEAK_TIME_RANGE[1][0]) and (t < const.OFF_PEAK_TIME_RANGE[1][1]) else 0 for t in quarters]
peak_day = np.diag(peak_indicator)
part_peak = np.diag(part_peak_indicator)
off_peak_weekday = np.diag(off_peak_indicator)
off_peak_weekend_off = np.zeros([N,N]) # used for peak, part_peak
off_peak_weekend_on = np.diag([1]*N) # used for off_peak
# each of these will block_diag 5 week day indicators and 2 weekend indicators
self.peak_mat = block_diag(peak_day, peak_day, peak_day, peak_day, peak_day,
off_peak_weekend_off, off_peak_weekend_off)
self.part_peak_mat = block_diag(part_peak, part_peak, part_peak, part_peak, part_peak,
off_peak_weekend_off,off_peak_weekend_off)
self.off_peak_mat = block_diag(off_peak_weekday, off_peak_weekday, off_peak_weekday, off_peak_weekday, off_peak_weekday,
off_peak_weekend_on, off_peak_weekend_on)
self.all_peak_mat = np.eye(self.horizon)
开发者ID:klaventall,项目名称:battery_sim,代码行数:31,代码来源:utility_rate_generator.py
示例15: compute_matrix_c
def compute_matrix_c(clean_labels, noisy_labels):
cm = confusion_matrix(clean_labels, noisy_labels)
cm -= np.diag(np.diag(cm))
cm = cm * 1.0 / cm.sum(axis=1, keepdims=True)
cm = cm.T
L = len(cm)
alpha = 1.0 / (L - 1)
C = np.zeros((L, L))
for j in xrange(L):
f = cm[:, j].ravel()
f = zip(f, xrange(L))
f.sort(reverse=True)
best_lik = -np.inf
best_i = -1
for i in xrange(L + 1):
c = np.zeros((L,))
for k in xrange(0, i):
c[k] = f[k][0]
if c.sum() > 0:
c /= c.sum()
lik = 0
for k in xrange(0, i):
lik += f[k][0] * np.log(c[k])
for k in xrange(i, L):
lik += f[k][0] * np.log(alpha)
if lik >= best_lik:
best_lik = lik
best_i = i
if i < L and f[i][0] == 0:
break
for k in xrange(0, best_i):
C[f[k][1], j] = f[k][0]
return C / C.sum(axis=0)
开发者ID:Cysu,项目名称:noisy_label,代码行数:33,代码来源:make_aux_data.py
示例16: main
def main():
n=10
# prepare matrix a
a=np.diag([5.]*n)
a+=np.diagflat([2.]*(n-1),1)
a+=np.diagflat([2.]*(n-1),-1)
print(a)
b=np.array([3,1,4,0,5,-1,6,-2,7,-15],dtype='f').T
#initial value x
x=np.ones(10).T
D=np.diag(np.diag(a))
L=np.tril(a,-1)
U=np.triu(a,+1)
M= -np.linalg.inv(D) @ (L+U)
N=np.linalg.inv(D)
for k in range(maxiteration):
x_new=M @ x + N @ b
if(np.linalg.norm(x_new-x) <epsilon):
break
x=x_new
else:
print("fail jacobi method ...")
exit(1)
print("the sol of ax = b using Jacobi method is \n{}".format(x))
print("iteration {} times".format(k))
print("indeed ax-b is \n{}".format(a @x -b))
print("you can check sol x using sympy...")
sy_a=sy.Matrix(a)
sy_x=sy_a.solve(b)
print(sy_x)
开发者ID:terasakisatoshi,项目名称:PythonCode,代码行数:33,代码来源:jacobi.py
示例17: testImplicitLargeDiag
def testImplicitLargeDiag(self):
mu = np.array([[1., 2, 3],
[11, 22, 33]]) # shape: [b, k] = [2, 3]
u = np.array([[[1., 2],
[3, 4],
[5, 6]],
[[0.5, 0.75],
[1, 0.25],
[1.5, 1.25]]]) # shape: [b, k, r] = [2, 3, 2]
m = np.array([[0.1, 0.2],
[0.4, 0.5]]) # shape: [b, r] = [2, 2]
scale = np.stack([
np.eye(3) + np.matmul(np.matmul(u[0], np.diag(m[0])),
np.transpose(u[0])),
np.eye(3) + np.matmul(np.matmul(u[1], np.diag(m[1])),
np.transpose(u[1])),
])
cov = np.stack([np.matmul(scale[0], scale[0].T),
np.matmul(scale[1], scale[1].T)])
logging.vlog(2, "expected_cov:\n{}".format(cov))
with self.test_session():
mvn = ds.MultivariateNormalDiagPlusLowRank(
loc=mu,
scale_perturb_factor=u,
scale_perturb_diag=m)
self.assertAllClose(cov, mvn.covariance().eval(), atol=0., rtol=1e-6)
开发者ID:1000sprites,项目名称:tensorflow,代码行数:26,代码来源:mvn_diag_plus_low_rank_test.py
示例18: KRt_from_P
def KRt_from_P(P):
'''Factorize the camera matrix into K,R,t as P = K[R|t].
>>> K = np.array([[1, 2, 3],
... [0, 4, 5],
... [0, 0, 1]])
>>> R = np.array([[ 0.57313786, -0.60900664, 0.54829181],
... [ 0.74034884, 0.6716445 , -0.02787928],
... [-0.35127851, 0.42190588, 0.83582225]])
>>> t = np.array([1, 2, 3])
>>> P = P_from_KRt(K, R, t)
>>> KK, RR, tt = KRt_from_P(P)
>>> np.allclose(K, KK)
True
>>> np.allclose(R, RR)
True
>>> np.allclose(t, tt)
True
'''
K, R = rq(P[:, :3])
T = np.diag(np.sign(np.diag(K))) # ensure K has positive diagonal
K = np.dot(K, T)
R = np.dot(T, R)
t = np.linalg.solve(K, P[:,3])
if np.linalg.det(R) < 0: # ensure det(R) = 1
R = -R
t = -t
K /= K[2, 2] # normalise K
return K, R, t
开发者ID:dakotabenjamin,项目名称:OpenSfM,代码行数:31,代码来源:multiview.py
示例19: test_extra_kwargs_2d
def test_extra_kwargs_2d(self):
sigma = np.random.random((25, 25))
sigma = sigma + sigma.T - np.diag(np.diag(sigma))
data = sm_data.handle_data(self.y, self.X, 'drop', sigma=sigma)
idx = ~np.isnan(np.c_[self.y, self.X]).any(axis=1)
sigma = sigma[idx][:,idx]
np.testing.assert_array_equal(data.sigma, sigma)
开发者ID:dengemann,项目名称:statsmodels,代码行数:7,代码来源:test_data.py
示例20: random_cov
def random_cov(d, diff=None):
"""Generate random covariance matrix.
Generates a random covariance matrix, or two dependent covariance matrices
if the argument `diff` is given.
"""
S = 0.8*np.random.randn(d,d)
copy_triu_to_tril(S)
np.fill_diagonal(S,0)
mineig = linalg.eigvalsh(S, eigvals=(0,0))[0]
drand = 0.8*np.random.randn(d)
if mineig < 0:
S += np.diag(np.exp(drand)-mineig)
else:
S += np.diag(np.exp(drand))
if not diff:
return S.T
S2 = S * np.random.randint(2, size=(d,d))*np.exp(diff*np.random.randn(d,d))
copy_triu_to_tril(S2)
np.fill_diagonal(S2,0)
mineig = linalg.eigvalsh(S2, eigvals=(0,0))[0]
drand += diff*np.random.randn(d)
if mineig < 0:
S2 += np.diag(np.exp(drand)-mineig)
else:
S2 += np.diag(np.exp(drand))
return S.T, S2.T
开发者ID:gelman,项目名称:ep-stan,代码行数:28,代码来源:test_olse.py
注:本文中的numpy.diag函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论