本文整理汇总了Python中scipy.sparse.tril方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.tril方法的具体用法?Python sparse.tril怎么用?Python sparse.tril使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse
的用法示例。
在下文中一共展示了sparse.tril方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_graph_tool_from_adjacency
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import tril [as 别名]
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
示例2: test_topological_nodes
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import tril [as 别名]
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))
示例3: _triangle_matrix
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import tril [as 别名]
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()
示例4: test_triul
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import tril [as 别名]
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))
示例5: tril
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import tril [as 别名]
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)
示例6: get_adjacency_sparse
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import tril [as 别名]
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