本文整理匯總了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