Python scipy.sparse.tril() Examples
The following are 6
code examples of scipy.sparse.tril().
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 also want to check out all available functions/classes of the module
scipy.sparse
, or try the search function
.
Example #1
Source File: utils.py From epiScanpy with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_graph_tool_from_adjacency(adjacency, directed=None): """Get graph_tool graph from adjacency matrix.""" import graph_tool as gt adjacency_edge_list = adjacency if not directed: from scipy.sparse import tril adjacency_edge_list = tril(adjacency) g = gt.Graph(directed=directed) g.add_vertex(adjacency.shape[0]) # this adds adjacency.shap[0] vertices g.add_edge_list(np.transpose(adjacency_edge_list.nonzero())) weights = g.new_edge_property('double') for e in g.edges(): # graph_tool uses the following convention, # which is opposite to the rest of scanpy weights[e] = adjacency[int(e.source()), int(e.target())] g.edge_properties['weight'] = weights return g
Example #2
Source File: test_traversal.py From dgl with Apache License 2.0 | 5 votes |
def test_topological_nodes(index_dtype, n=100): g = dgl.DGLGraph() a = sp.random(n, n, 3 / n, data_rvs=lambda n: np.ones(n)) b = sp.tril(a, -1).tocoo() g.from_scipy_sparse_matrix(b) if index_dtype == 'int32': g = dgl.graph(g.edges()).int() else: g = dgl.graph(g.edges()).long() layers_dgl = dgl.topological_nodes_generator(g) adjmat = g.adjacency_matrix() def tensor_topo_traverse(): n = g.number_of_nodes() mask = F.copy_to(F.ones((n, 1)), F.cpu()) degree = F.spmm(adjmat, mask) while F.reduce_sum(mask) != 0.: v = F.astype((degree == 0.), F.float32) v = v * mask mask = mask - v frontier = F.copy_to(F.nonzero_1d(F.squeeze(v, 1)), F.cpu()) yield frontier degree -= F.spmm(adjmat, v) layers_spmv = list(tensor_topo_traverse()) assert len(layers_dgl) == len(layers_spmv) assert all(toset(x) == toset(y) for x, y in zip(layers_dgl, layers_spmv))
Example #3
Source File: quadratic_expression.py From qiskit-aqua with Apache License 2.0 | 5 votes |
def _triangle_matrix(mat: dok_matrix) -> dok_matrix: lower = tril(mat, -1, format='dok') # `todok` is necessary because subtraction results in other format return (mat + lower.transpose() - lower).todok()
Example #4
Source File: test_coo.py From sparse with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_triul(shape, k): s = sparse.random(shape, density=0.5) x = s.todense() assert_eq(np.triu(x, k), sparse.triu(s, k)) assert_eq(np.tril(x, k), sparse.tril(s, k))
Example #5
Source File: common.py From sparse with BSD 3-Clause "New" or "Revised" License | 5 votes |
def tril(x, k=0): """ Returns an array with all elements above the k-th diagonal set to zero. Parameters ---------- x : COO The input array. k : int, optional The diagonal above which elements are set to zero. The default is zero, which corresponds to the main diagonal. Returns ------- COO The output lower-triangular matrix. Raises ------ ValueError If :code:`x` doesn't have zero fill-values. See Also -------- numpy.tril : NumPy equivalent function """ from .core import COO check_zero_fill_value(x) if not x.ndim >= 2: raise NotImplementedError( "sparse.tril is not implemented for scalars or 1-D arrays." ) mask = x.coords[-2] + k >= x.coords[-1] coords = x.coords[:, mask] data = x.data[mask] return COO(coords, data, shape=x.shape, has_duplicates=False, sorted=True)
Example #6
Source File: __init__.py From python-igraph with GNU General Public License v2.0 | 5 votes |
def get_adjacency_sparse(self, attribute=None): """Returns the adjacency matrix of a graph as scipy csr matrix. @param attribute: if C{None}, returns the ordinary adjacency matrix. When the name of a valid edge attribute is given here, the matrix returned will contain the default value at the places where there is no edge or the value of the given attribute where there is an edge. @return: the adjacency matrix as a L{scipy.sparse.csr_matrix}.""" try: from scipy import sparse except ImportError: raise ImportError('You should install scipy package in order to use this function') import numpy as np edges = self.get_edgelist() if attribute is None: weights = [1] * len(edges) else: if attribute not in self.es.attribute_names(): raise ValueError("Attribute does not exist") weights = self.es[attribute] N = self.vcount() mtx = sparse.csr_matrix((weights, zip(*edges)), shape=(N, N)) if not self.is_directed(): mtx = mtx + sparse.triu(mtx, 1).T + sparse.tril(mtx, -1).T return mtx