Python numpy.diag() Examples
The following are 30 code examples for showing how to use numpy.diag(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: mmdetection Author: open-mmlab File: test_masks.py License: Apache License 2.0 | 7 votes |
def test_bitmap_mask_resize(): # resize with empty bitmap masks raw_masks = dummy_raw_bitmap_masks((0, 28, 28)) bitmap_masks = BitmapMasks(raw_masks, 28, 28) resized_masks = bitmap_masks.resize((56, 72)) assert len(resized_masks) == 0 assert resized_masks.height == 56 assert resized_masks.width == 72 # resize with bitmap masks contain 1 instances raw_masks = np.diag(np.ones(4, dtype=np.uint8))[np.newaxis, ...] bitmap_masks = BitmapMasks(raw_masks, 4, 4) resized_masks = bitmap_masks.resize((8, 8)) assert len(resized_masks) == 1 assert resized_masks.height == 8 assert resized_masks.width == 8 truth = np.array([[[1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1]]]) assert (resized_masks.masks == truth).all()
Example 2
Project: LanczosNetwork Author: lrjconan File: data_helper.py License: MIT License | 7 votes |
def normalize_adj(A, is_sym=True, exponent=0.5): """ Normalize adjacency matrix is_sym=True: D^{-1/2} A D^{-1/2} is_sym=False: D^{-1} A """ rowsum = np.array(A.sum(1)) if is_sym: r_inv = np.power(rowsum, -exponent).flatten() else: r_inv = np.power(rowsum, -1.0).flatten() r_inv[np.isinf(r_inv)] = 0. if sp.isspmatrix(A): r_mat_inv = sp.diags(r_inv.squeeze()) else: r_mat_inv = np.diag(r_inv) if is_sym: return r_mat_inv.dot(A).dot(r_mat_inv) else: return r_mat_inv.dot(A)
Example 3
Project: FRIDA Author: LCAV File: point_cloud.py License: MIT License | 6 votes |
def classical_mds(self, D): ''' Classical multidimensional scaling Parameters ---------- D : square 2D ndarray Euclidean Distance Matrix (matrix containing squared distances between points ''' # Apply MDS algorithm for denoising n = D.shape[0] J = np.eye(n) - np.ones((n,n))/float(n) G = -0.5*np.dot(J, np.dot(D, J)) s, U = np.linalg.eig(G) # we need to sort the eigenvalues in decreasing order s = np.real(s) o = np.argsort(s) s = s[o[::-1]] U = U[:,o[::-1]] S = np.diag(s)[0:self.dim,:] self.X = np.dot(np.sqrt(S),U.T)
Example 4
Project: StructEngPy Author: zhuoju36 File: dynamic.py License: MIT License | 6 votes |
def spectrum_analysis(model,n,spec): """ sepctrum analysis params: n: number of modes to use\n spec: a list of tuples (period,acceleration response) """ freq,mode=eigen_mode(model,n) M_=np.dot(mode.T,model.M) M_=np.dot(M_,mode) K_=np.dot(mode.T,model.K) K_=np.dot(K_,mode) C_=np.dot(mode.T,model.C) C_=np.dot(C_,mode) d_=[] for (m_,k_,c_) in zip(M_.diag(),K_.diag(),C_.diag()): sdof=SDOFSystem(m_,k_) T=sdof.omega_d() d_.append(np.interp(T,spec[0],spec[1]*m_)) d=np.dot(d_,mode) #CQC return d
Example 5
Project: kalman_filter_multi_object_tracking Author: srianant File: kalman_filter.py License: MIT License | 6 votes |
def __init__(self): """Initialize variable used by Kalman Filter class Args: None Return: None """ self.dt = 0.005 # delta time self.A = np.array([[1, 0], [0, 1]]) # matrix in observation equations self.u = np.zeros((2, 1)) # previous state vector # (x,y) tracking object center self.b = np.array([[0], [255]]) # vector of observations self.P = np.diag((3.0, 3.0)) # covariance matrix self.F = np.array([[1.0, self.dt], [0.0, 1.0]]) # state transition mat self.Q = np.eye(self.u.shape[0]) # process noise matrix self.R = np.eye(self.b.shape[0]) # observation noise matrix self.lastResult = np.array([[0], [255]])
Example 6
Project: fuku-ml Author: fukuball File: KernelRidgeRegression.py License: MIT License | 6 votes |
def train(self): if (self.status != 'init'): print("Please load train data and init W first.") return self.W self.status = 'train' original_X = self.train_X[:, 1:] K = utility.Kernel.kernel_matrix(self, original_X) I = np.diag(np.ones(self.data_num)) inverse_part = np.linalg.inv(self.lambda_p * I + K) self.beta = np.dot(inverse_part, self.train_Y) return self.W
Example 7
Project: OpenFermion-Cirq Author: quantumlib File: fermionic_simulation.py License: Apache License 2.0 | 6 votes |
def _eigen_components(self): components = [(0, np.diag([1, 1, 1, 0, 1, 0, 0, 1]))] nontrivial_part = np.zeros((3, 3), dtype=np.complex128) for ij, w in zip([(1, 2), (0, 2), (0, 1)], self.weights): nontrivial_part[ij] = w nontrivial_part[ij[::-1]] = w.conjugate() assert np.allclose(nontrivial_part, nontrivial_part.conj().T) eig_vals, eig_vecs = np.linalg.eigh(nontrivial_part) for eig_val, eig_vec in zip(eig_vals, eig_vecs.T): exp_factor = -eig_val / np.pi proj = np.zeros((8, 8), dtype=np.complex128) nontrivial_indices = np.array([3, 5, 6], dtype=np.intp) proj[nontrivial_indices[:, np.newaxis], nontrivial_indices] = ( np.outer(eig_vec.conjugate(), eig_vec)) components.append((exp_factor, proj)) return components
Example 8
Project: OpenFermion-Cirq Author: quantumlib File: fermionic_simulation.py License: Apache License 2.0 | 6 votes |
def _eigen_components(self): # projector onto subspace spanned by basis states with # Hamming weight != 2 zero_component = np.diag( [int(bin(i).count('1') != 2) for i in range(16)]) state_pairs = (('0110', '1001'), ('0101', '1010'), ('0011', '1100')) plus_minus_components = tuple( (-abs(weight) * sign / np.pi, state_swap_eigen_component(state_pair[0], state_pair[1], sign, np.angle(weight))) for weight, state_pair in zip(self.weights, state_pairs) for sign in (-1, 1)) return ((0, zero_component),) + plus_minus_components
Example 9
Project: OpenFermion-Cirq Author: quantumlib File: gradient_hf_test.py License: Apache License 2.0 | 6 votes |
def test_rhf_func_gen(): rhf_objective, molecule, parameters, _, _ = make_h6_1_3() ansatz, energy, _ = rhf_func_generator(rhf_objective) assert np.isclose(molecule.hf_energy, energy(parameters)) ansatz, energy, _, opdm_func = rhf_func_generator( rhf_objective, initial_occ_vec=[1] * 3 + [0] * 3, get_opdm_func=True) assert np.isclose(molecule.hf_energy, energy(parameters)) test_opdm = opdm_func(parameters) u = ansatz(parameters) initial_opdm = np.diag([1] * 3 + [0] * 3) final_odpm = u @ initial_opdm @ u.T assert np.allclose(test_opdm, final_odpm) result = rhf_minimization(rhf_objective, initial_guess=parameters) assert np.allclose(result.x, parameters)
Example 10
Project: graph-neural-networks Author: alelab-upenn File: graphTools.py License: GNU General Public License v3.0 | 6 votes |
def adjacencyToLaplacian(W): """ adjacencyToLaplacian: Computes the Laplacian from an Adjacency matrix Input: W (np.array): adjacency matrix Output: L (np.array): Laplacian matrix """ # Check that the matrix is square assert W.shape[0] == W.shape[1] # Compute the degree vector d = np.sum(W, axis = 1) # And build the degree matrix D = np.diag(d) # Return the Laplacian return D - W
Example 11
Project: graph-neural-networks Author: alelab-upenn File: graphTools.py License: GNU General Public License v3.0 | 6 votes |
def normalizeAdjacency(W): """ NormalizeAdjacency: Computes the degree-normalized adjacency matrix Input: W (np.array): adjacency matrix Output: A (np.array): degree-normalized adjacency matrix """ # Check that the matrix is square assert W.shape[0] == W.shape[1] # Compute the degree vector d = np.sum(W, axis = 1) # Invert the square root of the degree d = 1/np.sqrt(d) # And build the square root inverse degree matrix D = np.diag(d) # Return the Normalized Adjacency return D @ W @ D
Example 12
Project: graph-neural-networks Author: alelab-upenn File: graphTools.py License: GNU General Public License v3.0 | 6 votes |
def normalizeLaplacian(L): """ NormalizeLaplacian: Computes the degree-normalized Laplacian matrix Input: L (np.array): Laplacian matrix Output: normL (np.array): degree-normalized Laplacian matrix """ # Check that the matrix is square assert L.shape[0] == L.shape[1] # Compute the degree vector (diagonal elements of L) d = np.diag(L) # Invert the square root of the degree d = 1/np.sqrt(d) # And build the square root inverse degree matrix D = np.diag(d) # Return the Normalized Laplacian return D @ L @ D
Example 13
Project: transferlearning Author: jindongwang File: EasyTL.py License: MIT License | 6 votes |
def get_ma_dist(A, B): Y = A.copy() X = B.copy() S = np.cov(X.T) try: SI = np.linalg.inv(S) except: print("Singular Matrix: using np.linalg.pinv") SI = np.linalg.pinv(S) mu = np.mean(X, axis=0) diff = Y - mu Dct_c = np.diag(diff @ SI @ diff.T) return Dct_c
Example 14
Project: zca Author: mwv File: zca.py License: GNU General Public License v3.0 | 6 votes |
def fit(self, X, y=None): """Compute the mean, whitening and dewhitening matrices. Parameters ---------- X : array-like with shape [n_samples, n_features] The data used to compute the mean, whitening and dewhitening matrices. """ X = check_array(X, accept_sparse=None, copy=self.copy, ensure_2d=True) X = as_float_array(X, copy=self.copy) self.mean_ = X.mean(axis=0) X_ = X - self.mean_ cov = np.dot(X_.T, X_) / (X_.shape[0]-1) U, S, _ = linalg.svd(cov) s = np.sqrt(S.clip(self.regularization)) s_inv = np.diag(1./s) s = np.diag(s) self.whiten_ = np.dot(np.dot(U, s_inv), U.T) self.dewhiten_ = np.dot(np.dot(U, s), U.T) return self
Example 15
Project: sparse-subspace-clustering-python Author: abhinav4192 File: SpectralClustering.py License: MIT License | 6 votes |
def SpectralClustering(CKSym, n): # This is direct port of JHU vision lab code. Could probably use sklearn SpectralClustering. CKSym = CKSym.astype(float) N, _ = CKSym.shape MAXiter = 1000 # Maximum number of iterations for KMeans REPlic = 20 # Number of replications for KMeans DN = np.diag(np.divide(1, np.sqrt(np.sum(CKSym, axis=0) + np.finfo(float).eps))) LapN = identity(N).toarray().astype(float) - np.matmul(np.matmul(DN, CKSym), DN) _, _, vN = np.linalg.svd(LapN) vN = vN.T kerN = vN[:, N - n:N] normN = np.sqrt(np.sum(np.square(kerN), axis=1)) kerNS = np.divide(kerN, normN.reshape(len(normN), 1) + np.finfo(float).eps) km = KMeans(n_clusters=n, n_init=REPlic, max_iter=MAXiter, n_jobs=-1).fit(kerNS) return km.labels_
Example 16
Project: tsn-pytorch Author: yjxiong File: utils.py License: BSD 2-Clause "Simplified" License | 6 votes |
def class_accuracy(prediction, label): cf = confusion_matrix(prediction, label) cls_cnt = cf.sum(axis=1) cls_hit = np.diag(cf) cls_acc = cls_hit / cls_cnt.astype(float) mean_cls_acc = cls_acc.mean() return cls_acc, mean_cls_acc
Example 17
Project: ZIFA Author: epierson9 File: ZIFA.py License: MIT License | 6 votes |
def invertFast(A, d): """ given an array A of shape d x k and a d x 1 vector d, computes (A * A.T + diag(d)) ^{-1} Checked. """ assert(A.shape[0] == d.shape[0]) assert(d.shape[1] == 1) k = A.shape[1] A = np.array(A) d_vec = np.array(d) d_inv = np.array(1 / d_vec[:, 0]) inv_d_squared = np.dot(np.atleast_2d(d_inv).T, np.atleast_2d(d_inv)) M = np.diag(d_inv) - inv_d_squared * np.dot(np.dot(A, np.linalg.inv(np.eye(k, k) + np.dot(A.T, mult_diag(d_inv, A)))), A.T) return M
Example 18
Project: LanczosNetwork Author: lrjconan File: spectral_graph_partition.py License: MIT License | 6 votes |
def get_L_cluster_cut(L, node_label): adj = L - np.diag(np.diag(L)) adj[adj != 0] = 1.0 num_nodes = adj.shape[0] idx_row, idx_col = np.meshgrid(range(num_nodes), range(num_nodes)) idx_row, idx_col = idx_row.flatten().astype( np.int64), idx_col.flatten().astype(np.int64) mask = (node_label[idx_row] == node_label[idx_col]).reshape( num_nodes, num_nodes).astype(np.float) adj_cluster = adj * mask adj_cut = adj - adj_cluster L_cut = get_laplacian(adj_cut, graph_laplacian_type='L4') L_cluster = get_laplacian(adj_cluster, graph_laplacian_type='L4') return L_cluster, L_cut
Example 19
Project: pyscf Author: pyscf File: 22-density.py License: Apache License 2.0 | 6 votes |
def tda_denisty_matrix(td, state_id): ''' Taking the TDA amplitudes as the CIS coefficients, calculate the density matrix (in AO basis) of the excited states ''' cis_t1 = td.xy[state_id][0] dm_oo =-np.einsum('ia,ka->ik', cis_t1.conj(), cis_t1) dm_vv = np.einsum('ia,ic->ac', cis_t1, cis_t1.conj()) # The ground state density matrix in mo_basis mf = td._scf dm = np.diag(mf.mo_occ) # Add CIS contribution nocc = cis_t1.shape[0] dm[:nocc,:nocc] += dm_oo * 2 dm[nocc:,nocc:] += dm_vv * 2 # Transform density matrix to AO basis mo = mf.mo_coeff dm = np.einsum('pi,ij,qj->pq', mo, dm, mo.conj()) return dm # Density matrix for the 3rd excited state
Example 20
Project: pyscf Author: pyscf File: test_uks.py License: Apache License 2.0 | 6 votes |
def test_pp_UKS(self): cell = pbcgto.Cell() cell.unit = 'A' cell.atom = ''' Si 2.715348700 2.715348700 0.000000000; Si 2.715348700 0.000000000 2.715348700; ''' cell.basis = 'gth-szv' cell.pseudo = 'gth-pade' Lx = Ly = Lz = 5.430697500 cell.a = np.diag([Lx,Ly,Lz]) cell.mesh = np.array([17]*3) cell.verbose = 5 cell.output = '/dev/null' cell.build() mf = pbcdft.UKS(cell) mf.xc = 'blyp' self.assertAlmostEqual(mf.scf(), -7.6058004283213396, 8) mf.xc = 'lda,vwn' self.assertAlmostEqual(mf.scf(), -7.6162130840535092, 8)
Example 21
Project: pyscf Author: pyscf File: eom_kccsd_ghf.py License: Apache License 2.0 | 6 votes |
def get_init_guess(self, kshift, nroots=1, koopmans=False, diag=None): size = self.vector_size() dtype = getattr(diag, 'dtype', np.complex) nroots = min(nroots, size) guess = [] if koopmans: for n in self.nonzero_vpadding[kshift][:nroots]: g = np.zeros(int(size), dtype=dtype) g[n] = 1.0 g = self.mask_frozen(g, kshift, const=0.0) guess.append(g) else: idx = diag.argsort()[:nroots] for i in idx: g = np.zeros(int(size), dtype=dtype) g[i] = 1.0 g = self.mask_frozen(g, kshift, const=0.0) guess.append(g) return guess
Example 22
Project: pyscf Author: pyscf File: eom_kccsd_ghf.py License: Apache License 2.0 | 6 votes |
def get_init_guess(self, kshift, nroots=1, koopmans=False, diag=None, **kwargs): """Initial guess vectors of R coefficients""" size = self.vector_size(kshift) dtype = getattr(diag, 'dtype', np.complex) nroots = min(nroots, size) guess = [] # TODO do Koopmans later if koopmans: raise NotImplementedError else: idx = diag.argsort()[:nroots] for i in idx: g = np.zeros(int(size), dtype=dtype) g[i] = 1.0 # TODO do mask_frozen later guess.append(g) return guess
Example 23
Project: libTLDA Author: wmkouw File: test_suba.py License: MIT License | 5 votes |
def test_subspace_alignment(): """Test the alignment between datasets.""" X = rnd.randn(100, 10) Z = np.dot(rnd.randn(100, 10), np.diag(np.arange(1, 11))) clf = SubspaceAlignedClassifier() V, CX, CZ = clf.subspace_alignment(X, Z, subspace_dim=3) assert not np.any(np.isnan(V)) assert CX.shape[1] == 3 assert CZ.shape[1] == 3
Example 24
Project: FRIDA Author: LCAV File: point_cloud.py License: MIT License | 5 votes |
def EDM(self): ''' Computes the EDM corresponding to the marker set ''' if self.X is None: raise ValueError('No marker set') G = np.dot(self.X.T, self.X) return np.outer(np.ones(self.m), np.diag(G)) \ - 2*G + np.outer(np.diag(G), np.ones(self.m))
Example 25
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: acc_fc.py License: Apache License 2.0 | 5 votes |
def fc_decomposition(model, args): W = model.arg_params[args.layer+'_weight'].asnumpy() b = model.arg_params[args.layer+'_bias'].asnumpy() W = W.reshape((W.shape[0],-1)) b = b.reshape((b.shape[0],-1)) u, s, v = LA.svd(W, full_matrices=False) s = np.diag(s) t = u.dot(s.dot(v)) rk = args.K P = u[:,:rk] Q = s[:rk,:rk].dot(v[:rk,:]) name1 = args.layer + '_red' name2 = args.layer + '_rec' def sym_handle(data, node): W1, W2 = Q, P sym1 = mx.symbol.FullyConnected(data=data, num_hidden=W1.shape[0], no_bias=True, name=name1) sym2 = mx.symbol.FullyConnected(data=sym1, num_hidden=W2.shape[0], no_bias=False, name=name2) return sym2 def arg_handle(arg_shape_dic, arg_params): W1, W2 = Q, P W1 = W1.reshape(arg_shape_dic[name1+'_weight']) weight1 = mx.ndarray.array(W1) W2 = W2.reshape(arg_shape_dic[name2+'_weight']) b2 = b.reshape(arg_shape_dic[name2+'_bias']) weight2 = mx.ndarray.array(W2) bias2 = mx.ndarray.array(b2) arg_params[name1 + '_weight'] = weight1 arg_params[name2 + '_weight'] = weight2 arg_params[name2 + '_bias'] = bias2 new_model = utils.replace_conv_layer(args.layer, model, sym_handle, arg_handle) return new_model
Example 26
Project: PolarSeg Author: edwardzhou130 File: test_pretrain.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def per_class_iu(hist): return np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist))
Example 27
Project: PolarSeg Author: edwardzhou130 File: train.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def per_class_iu(hist): return np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist))
Example 28
Project: OpenFermion-Cirq Author: quantumlib File: common_gates.py License: Apache License 2.0 | 5 votes |
def _eigen_components(self): # yapf: disable return [(0, np.diag([1, 0, 0, 1])), (-0.5, np.array([[0, 0, 0, 0], [0, 0.5, 0.5, 0], [0, 0.5, 0.5, 0], [0, 0, 0, 0]])), (+0.5, np.array([[0, 0, 0, 0], [0, 0.5, -0.5, 0], [0, -0.5, 0.5, 0], [0, 0, 0, 0]]))] # yapf: enable
Example 29
Project: OpenFermion-Cirq Author: quantumlib File: common_gates.py License: Apache License 2.0 | 5 votes |
def _eigen_components(self): # yapf: disable return [(0, np.diag([1, 0, 0, 1])), (-0.5, np.array([[0, 0, 0, 0], [0, 0.5, -0.5j, 0], [0, 0.5j, 0.5, 0], [0, 0, 0, 0]])), (0.5, np.array([[0, 0, 0, 0], [0, 0.5, 0.5j, 0], [0, -0.5j, 0.5, 0], [0, 0, 0, 0]]))] # yapf: enable
Example 30
Project: OpenFermion-Cirq Author: quantumlib File: fermionic_simulation.py License: Apache License 2.0 | 5 votes |
def _eigen_components(self): components = [(0, np.diag([1, 0, 0, 0])), (-self.weights[1] / np.pi, np.diag([0, 0, 0, 1]))] r = abs(self.weights[0]) / np.pi theta = 2 * _arg(self.weights[0]) / np.pi for s in (-1, 1): components.append( (-s * r, np.array([[0, 0, 0, 0], [0, 1, s * 1j**(-theta), 0], [0, s * 1j**(theta), 1, 0], [0, 0, 0, 0]]) / 2)) return components