我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.triu()。
def _init_coefs(X, method='corrcoef'): if method == 'corrcoef': return np.corrcoef(X, rowvar=False), 1.0 elif method == 'cov': init_cov = np.cov(X, rowvar=False) return init_cov, np.max(np.abs(np.triu(init_cov))) elif method == 'spearman': return spearman_correlation(X, rowvar=False), 1.0 elif method == 'kendalltau': return kendalltau_correlation(X, rowvar=False), 1.0 elif callable(method): return method(X) else: raise ValueError( ("initialize_method must be 'corrcoef' or 'cov', " "passed \'{}\' .".format(method)) )
def test_tril_triu_ndim3(): for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']: a = np.array([ [[1, 1], [1, 1]], [[1, 1], [1, 0]], [[1, 1], [0, 0]], ], dtype=dtype) a_tril_desired = np.array([ [[1, 0], [1, 1]], [[1, 0], [1, 0]], [[1, 0], [0, 0]], ], dtype=dtype) a_triu_desired = np.array([ [[1, 1], [0, 1]], [[1, 1], [0, 0]], [[1, 1], [0, 0]], ], dtype=dtype) a_triu_observed = np.triu(a) a_tril_observed = np.tril(a) yield assert_array_equal, a_triu_observed, a_triu_desired yield assert_array_equal, a_tril_observed, a_tril_desired yield assert_equal, a_triu_observed.dtype, a.dtype yield assert_equal, a_tril_observed.dtype, a.dtype
def test_tril_triu_dtype(): # Issue 4916 # tril and triu should return the same dtype as input for c in np.typecodes['All']: if c == 'V': continue arr = np.zeros((3, 3), dtype=c) assert_equal(np.triu(arr).dtype, arr.dtype) assert_equal(np.tril(arr).dtype, arr.dtype) # check special cases arr = np.array([['2001-01-01T12:00', '2002-02-03T13:56'], ['2004-01-01T12:00', '2003-01-03T13:45']], dtype='datetime64') assert_equal(np.triu(arr).dtype, arr.dtype) assert_equal(np.tril(arr).dtype, arr.dtype) arr = np.zeros((3,3), dtype='f4,f4') assert_equal(np.triu(arr).dtype, arr.dtype) assert_equal(np.tril(arr).dtype, arr.dtype)
def potential_numba_array(cluster): d = distances_numba_array(cluster) # Original: dtri = np.triu(d) # np.triu is not supported; so write my own loop to clear the # lower triangle for i in range(d.shape[0]): for j in range(d.shape[1]): if i > j: d[i, j] = 0 # Original: lj_numba_array(d[d > 1e-6]).sum() # d[d > 1e-6] is not supported due to the indexing with boolean # array. Replace with custom loop. energy = 0.0 for v in d.flat: if v > 1e-6: energy += lj_numba_array(v) return energy
def pairwise_expansion(x, func, reflexive=True): """Computes func(xi, xj) over all possible indices i and j, where func is an arbitrary function if reflexive == False, only pairs with i != j are considered """ x_height, x_width = x.shape if reflexive: k = 0 else: k = 1 mask = numpy.triu(numpy.ones((x_width, x_width)), k) > 0.5 # mask = mask.reshape((1,x_width,x_width)) y1 = x.reshape(x_height, x_width, 1) y2 = x.reshape(x_height, 1, x_width) yexp = func(y1, y2) # print "yexp.shape=", yexp.shape # print "mask.shape=", mask.shape out = yexp[:, mask] # print "out.shape=", out.shape # yexp.reshape((x_height, N*N)) return out
def products_2(x, func, k=0): """Computes func(xi, xj) over all possible indices i and j constrained to j >= i+k. func is an arbitrary function, and k >= 0 is an integer """ x_height, x_width = x.shape mask = numpy.triu(numpy.ones((x_width, x_width)), k) > 0.5 z1 = x.reshape(x_height, x_width, 1) z2 = x.reshape(x_height, 1, x_width) yexp = func(z1, z2) # twice computation, but performance gain due to lack of loops out = yexp[:, mask] return out
def tangent_space(covmats, Cref): """Project a set of covariance matrices in the tangent space according to the given reference point Cref :param covmats: Covariance matrices set, Ntrials X Nchannels X Nchannels :param Cref: The reference covariance matrix :returns: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2) """ Nt, Ne, Ne = covmats.shape Cm12 = invsqrtm(Cref) idx = numpy.triu_indices_from(Cref) T = numpy.empty((Nt, Ne * (Ne + 1) / 2)) coeffs = ( numpy.sqrt(2) * numpy.triu( numpy.ones( (Ne, Ne)), 1) + numpy.eye(Ne))[idx] for index in range(Nt): tmp = numpy.dot(numpy.dot(Cm12, covmats[index, :, :]), Cm12) tmp = logm(tmp) T[index, :] = numpy.multiply(coeffs, tmp[idx]) return T
def untangent_space(T, Cref): """Project a set of Tangent space vectors in the manifold according to the given reference point Cref :param T: the Tangent space , a matrix of Ntrials X (Nchannels*(Nchannels+1)/2) :param Cref: The reference covariance matrix :returns: A set of Covariance matrix, Ntrials X Nchannels X Nchannels """ Nt, Nd = T.shape Ne = int((numpy.sqrt(1 + 8 * Nd) - 1) / 2) C12 = sqrtm(Cref) idx = numpy.triu_indices_from(Cref) covmats = numpy.empty((Nt, Ne, Ne)) covmats[:, idx[0], idx[1]] = T for i in range(Nt): covmats[i] = numpy.diag(numpy.diag(covmats[i])) + numpy.triu( covmats[i], 1) / numpy.sqrt(2) + numpy.triu(covmats[i], 1).T / numpy.sqrt(2) covmats[i] = expm(covmats[i]) covmats[i] = numpy.dot(numpy.dot(C12, covmats[i]), C12) return covmats
def has_approx_support(m, m_hat, prob=0.01): """Returns 1 if model selection error is less than or equal to prob rate, 0 else. NOTE: why does np.nonzero/np.flatnonzero create so much problems? """ m_nz = np.flatnonzero(np.triu(m, 1)) m_hat_nz = np.flatnonzero(np.triu(m_hat, 1)) upper_diagonal_mask = np.flatnonzero(np.triu(np.ones(m.shape), 1)) not_m_nz = np.setdiff1d(upper_diagonal_mask, m_nz) intersection = np.in1d(m_hat_nz, m_nz) # true positives not_intersection = np.in1d(m_hat_nz, not_m_nz) # false positives true_positive_rate = 0.0 if len(m_nz): true_positive_rate = 1. * np.sum(intersection) / len(m_nz) true_negative_rate = 1. - true_positive_rate false_positive_rate = 0.0 if len(not_m_nz): false_positive_rate = 1. * np.sum(not_intersection) / len(not_m_nz) return int(np.less_equal(true_negative_rate + false_positive_rate, prob))
def read_mongodb_matrix(tickers, matrix_name): mis = MatrixItem.objects(i__in = tickers, j__in = tickers, matrix_name = matrix_name) n = len(tickers) available_tickers = set([mi.i for mi in mis]) np.random.seed(n) a = np.absolute(np.random.normal(0, 0.001, [n, n])) a_triu = np.triu(a, k=0) a_tril = np.tril(a, k=0) a_diag = np.diag(np.diag(a)) a_sym_triu = a_triu + a_triu.T - a_diag matrix = pd.DataFrame(a_sym_triu, index = tickers, columns = tickers) for mi in mis: if abs(mi.v) > 10: mi.v = 0.001 matrix.set_value(mi.i, mi.j, mi.v) matrix.set_value(mi.j, mi.i, mi.v) matrix = matrix.round(6) return matrix
def test_preserve_trace_ground_state(self, dm): dm.hadamard(2) assert np.allclose(dm.trace(), 1) dm.hadamard(4) assert np.allclose(dm.trace(), 1) dm.hadamard(0) assert np.allclose(dm.trace(), 1) # @pytest.mark.skip # def test_squares_to_one(self, dm_random): # dm = dm_random # a0 = dm.to_array() # dm.hadamard(4) # dm.hadamard(4) # # dm.hadamard(2) # # dm.hadamard(2) # # dm.hadamard(0) # # dm.hadamard(0) # a1 = dm.to_array() # assert np.allclose(np.triu(a0), np.triu(a1))
def make_symmetric_lower(mat): ''' Copies the matrix entries below the main diagonal to the upper triangle half of the matrix. Leaves the diagonal unchanged. Returns a `NumPy` matrix object. **mat** : `numpy.matrix` A lower diagonal matrix. returns : `numpy.matrix` The lower triangle matrix. ''' # extract lower triangle from matrix (including diagonal) tmp_mat = np.tril(mat) # if the matrix given wasn't a lower triangle matrix, raise an error if (mat != tmp_mat).all(): raise Exception('Matrix to symmetrize is not a lower diagonal matrix.') # add its transpose to itself, zeroing the diagonal to avoid doubling tmp_mat += np.triu(tmp_mat.transpose(), 1) return np.asmatrix(tmp_mat)
def _init_topics_assignement(self): dim = (self.J, self.J, 2) alpha_0 = self.alpha_0 # Poisson way #z = np.array( [poisson(alpha_0, size=dim) for dim in data_dims] ) # Random way K = self.K_init z = np.random.randint(0, K, (dim)) if self.likelihood._symmetric: z[:, :, 0] = np.triu(z[:, :, 0]) + np.triu(z[:, :, 0], 1).T z[:, :, 1] = np.triu(z[:, :, 1]) + np.triu(z[:, :, 1], 1).T # LDA way # improve local optima ? #theta_j = dirichlet([1, gmma]) #todo ? return z
def get_data_prop(self): prop = super(frontendNetwork, self).get_data_prop() if self.is_symmetric(): nnz = np.triu(self.data).sum() else: nnz = self.data.sum() _nnz = self.data.sum(axis=1) d = {'instances': self.data.shape[1], 'nnz': nnz, 'nnz_mean': _nnz.mean(), 'nnz_var': _nnz.var(), 'density': self.density(), 'diameter': self.diameter(), 'clustering_coef': self.clustering_coefficient(), 'modularity': self.modularity(), 'communities': self.clusters_len(), 'features': self.get_nfeat(), 'directed': not self.is_symmetric() } prop.update(d) return prop
def setUp(self): self.nwalkers = 100 self.ndim = 5 self.ntemp = 20 self.N = 1000 self.mean = np.zeros(self.ndim) self.cov = 0.5 - np.random.rand(self.ndim ** 2) \ .reshape((self.ndim, self.ndim)) self.cov = np.triu(self.cov) self.cov += self.cov.T - np.diag(self.cov.diagonal()) self.cov = np.dot(self.cov, self.cov) self.icov = np.linalg.inv(self.cov) self.p0 = [0.1 * np.random.randn(self.ndim) for i in range(self.nwalkers)] self.truth = np.random.multivariate_normal(self.mean, self.cov, 100000)
def verify_solve_grad(self, m, n, A_structure, lower, rng): # ensure diagonal elements of A relatively large to avoid numerical # precision issues A_val = (rng.normal(size=(m, m)) * 0.5 + numpy.eye(m)).astype(config.floatX) if A_structure == 'lower_triangular': A_val = numpy.tril(A_val) elif A_structure == 'upper_triangular': A_val = numpy.triu(A_val) if n is None: b_val = rng.normal(size=m).astype(config.floatX) else: b_val = rng.normal(size=(m, n)).astype(config.floatX) eps = None if config.floatX == "float64": eps = 2e-8 solve_op = Solve(A_structure=A_structure, lower=lower) utt.verify_grad(solve_op, [A_val, b_val], 3, rng, eps=eps)
def test_as_spin_response(self): response = self.response_factory() num_samples = 100 num_variables = 200 samples = np.triu(np.ones((num_samples, num_variables))) * 2 - 1 energies = np.zeros((num_samples,)) response.add_samples_from_array(samples, energies) dimod_response = response.as_spin_response() for s, t in zip(response, dimod_response): self.assertEqual(s, t) dimod_response = response.as_spin_response(data_copy=True) for (__, dat), (__, dat0) in zip(response.samples(data=True), dimod_response.samples(data=True)): self.assertNotEqual(id(dat), id(dat0))
def test_as_binary_response(self): response = self.response_factory() num_samples = 100 num_variables = 200 samples = np.triu(np.ones((num_samples, num_variables))) energies = np.zeros((num_samples,)) response.add_samples_from_array(samples, energies) dimod_response = response.as_binary_response() for s, t in zip(response, dimod_response): self.assertEqual(s, t) dimod_response = response.as_binary_response(data_copy=True) for (__, dat), (__, dat0) in zip(response.samples(data=True), dimod_response.samples(data=True)): self.assertNotEqual(id(dat), id(dat0))
def sqrtvc(m): mup=m mdown=mup.transpose() mdown.setdiag(0) mtogether=mup+mdown sums_sq=np.sqrt(mtogether.sum(axis=1)) D_sq = sps.spdiags(1.0/sums_sq.flatten(), [0], mtogether.get_shape()[0], mtogether.get_shape()[1], format='csr') return sps.triu(D_sq.dot(mtogether.dot(D_sq)))
def hichip_add_diagonal(m): mup=m mdown=mup.transpose() mdown.setdiag(0) mtogether=mup+mdown sums=mtogether.sum(axis=1) max_sum=np.max(sums) to_add=1.0*max_sum-1.0*sums to_add_values=[] for i in range(m.shape[0]): to_add_values.append(to_add[i,0]) mtogether.setdiag(np.array(to_add_values)) D = sps.spdiags(1.0/sums.flatten(), [0], mtogether.get_shape()[0], mtogether.get_shape()[1], format='csr') return sps.triu(D.dot(mtogether))
def coverage_norm(m): mup=m mdown=mup.transpose() mdown.setdiag(0) mtogether=mup+mdown sums=mtogether.sum(axis=1) D = sps.spdiags(1.0/sums.flatten(), [0], mtogether.get_shape()[0], mtogether.get_shape()[1], format='csr') return sps.triu(D.dot(mtogether.dot(D))) #assumes matrix is upper triangular
def array_2_coverageVector(m): assert np.allclose(m, np.triu(m)) m_sym=m+m.T-m.diagonal() return m_sym.sum(axis=0)
def subsample_to_depth_array_upperTri(m,seq_depth): m=np.triu(m) subsampled_data=np.zeros(m.shape) depthm=m.sum() assert seq_depth<=depthm subsampling_prob=seq_depth/depthm for i in range(m.shape[0]): for j in range(m.shape[1]): if j<=i: continue n=m[i,j] subsampled_data[i,j]=np.random.binomial(n,subsampling_prob,1)[0] return subsampled_data
def binarize_top(m,q): threshold=mquantiles(np.triu(m).flatten(),q) new_m=copy.deepcopy(m) new_m[new_m<threshold]=0 new_m[new_m>=threshold]=1 return get_sqrtvc(new_m)
def compute_discount(gamma, maxlen): c = numpy.ones((maxlen,)) * gamma c[0] = 1. c = c.cumprod() C = numpy.triu(numpy.repeat(c[None, :], repeats=maxlen, axis=0)) C /= c[:, None] return C
def get_attn_subsequent_mask(seq): assert seq.dim() == 2 attn_shape = (seq.size(0), seq.size(1), seq.size(1)) subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8') subsequent_mask = torch.from_numpy(subsequent_mask) if seq.is_cuda: subsequent_mask = subsequent_mask.cuda() return subsequent_mask
def test_tril_triu_ndim2(): for dtype in np.typecodes['AllFloat'] + np.typecodes['AllInteger']: a = np.ones((2, 2), dtype=dtype) b = np.tril(a) c = np.triu(a) yield assert_array_equal, b, [[1, 0], [1, 1]] yield assert_array_equal, c, b.T # should return the same dtype as the original array yield assert_equal, b.dtype, a.dtype yield assert_equal, c.dtype, a.dtype
def test_tril_triu_with_inf(): # Issue 4859 arr = np.array([[1, 1, np.inf], [1, 1, 1], [np.inf, 1, 1]]) out_tril = np.array([[1, 0, 0], [1, 1, 0], [np.inf, 1, 1]]) out_triu = out_tril.T assert_array_equal(np.triu(arr), out_triu) assert_array_equal(np.tril(arr), out_tril)
def test_mask_indices(): # simple test without offset iu = mask_indices(3, np.triu) a = np.arange(9).reshape(3, 3) yield (assert_array_equal, a[iu], array([0, 1, 2, 4, 5, 8])) # Now with an offset iu1 = mask_indices(3, np.triu, 1) yield (assert_array_equal, a[iu1], array([1, 2, 5]))
def test_dynamic_programming_logic(self): # Test for the dynamic programming part # This test is directly taken from Cormen page 376. arrays = [np.random.random((30, 35)), np.random.random((35, 15)), np.random.random((15, 5)), np.random.random((5, 10)), np.random.random((10, 20)), np.random.random((20, 25))] m_expected = np.array([[0., 15750., 7875., 9375., 11875., 15125.], [0., 0., 2625., 4375., 7125., 10500.], [0., 0., 0., 750., 2500., 5375.], [0., 0., 0., 0., 1000., 3500.], [0., 0., 0., 0., 0., 5000.], [0., 0., 0., 0., 0., 0.]]) s_expected = np.array([[0, 1, 1, 3, 3, 3], [0, 0, 2, 3, 3, 3], [0, 0, 0, 3, 3, 3], [0, 0, 0, 0, 4, 5], [0, 0, 0, 0, 0, 5], [0, 0, 0, 0, 0, 0]], dtype=np.int) s_expected -= 1 # Cormen uses 1-based index, python does not. s, m = _multi_dot_matrix_chain_order(arrays, return_costs=True) # Only the upper triangular part (without the diagonal) is interesting. assert_almost_equal(np.triu(s[:-1, 1:]), np.triu(s_expected[:-1, 1:])) assert_almost_equal(np.triu(m), np.triu(m_expected))
def potential_numpy(cluster): d = distances_numpy(cluster) dtri = np.triu(d) energy = lj_numpy(dtri[dtri > 1e-6]).sum() return energy #### END: numpy
def extract_test_vals(query, target, query_field, target_field, test_df, is_test_df_sym): """ Extract values that has query in the columns and target in the rows. Args: query (string) target (string) query_field (string): name of multiindex level in which to find query target_field (string): name of multiindex level in which to find target test_df (pandas multi-index df) is_test_df_sym (bool): only matters if query == target; set to True to avoid double-counting in the case of a symmetric matrix Returns: vals (numpy array) """ assert query in test_df.columns.get_level_values(query_field), ( "query {} is not in the {} level of the columns of test_df.".format( query, query_field)) assert target in test_df.index.get_level_values(target_field), ( "target {} is not in the {} level of the index of test_df.".format( target, target_field)) # Extract elements where query is in columns and target is in rows target_in_rows_query_in_cols_df = test_df.loc[ test_df.index.get_level_values(target_field) == target, test_df.columns.get_level_values(query_field) == query] # If query == target AND the matrix is symmetric, need to take only triu # of the extracted values in order to avoid double-counting if query == target and is_test_df_sym: mask = np.triu(np.ones(target_in_rows_query_in_cols_df.shape), k=1).astype(np.bool) vals_with_nans = target_in_rows_query_in_cols_df.where(mask).values.flatten() vals = vals_with_nans[~np.isnan(vals_with_nans)] else: vals = target_in_rows_query_in_cols_df.values.flatten() return vals
def get_attn_subsequent_mask(seq): ''' Get an attention mask to avoid using the subsequent info.''' assert seq.dim() == 2 attn_shape = (seq.size(0), seq.size(1), seq.size(1)) subsequent_mask = np.triu(np.ones(attn_shape), k=1).astype('uint8') subsequent_mask = torch.from_numpy(subsequent_mask) if seq.is_cuda: subsequent_mask = subsequent_mask.cuda() return subsequent_mask
def _update_covariance(self, it): self.eigen_decomp_updated = it self.cov[:, :] = np.triu(self.cov) + np.triu(self.cov, 1).T D, B = np.linalg.eigh(self.cov) # HACK: avoid numerical problems D = np.maximum(D, np.finfo(np.float).eps) D = np.diag(np.sqrt(1.0 / D)) self.invsqrtC = B.dot(D).dot(B.T)
def get_bias(length: int): # matrix with lower triangle and main diagonal set to 0, upper triangle set to 1 upper_triangle = np.triu(np.ones((length, length)), k=1) # (1, length, length) bias = -99999999. * np.reshape(upper_triangle, (1, length, length)) return mx.nd.array(bias)