当前位置: 首页>>代码示例>>Python>>正文


Python sparse.tril方法代码示例

本文整理汇总了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 
开发者ID:colomemaria,项目名称:epiScanpy,代码行数:19,代码来源:utils.py

示例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)) 
开发者ID:dmlc,项目名称:dgl,代码行数:31,代码来源:test_traversal.py

示例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() 
开发者ID:Qiskit,项目名称:qiskit-aqua,代码行数:6,代码来源:quadratic_expression.py

示例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)) 
开发者ID:pydata,项目名称:sparse,代码行数:8,代码来源:test_coo.py

示例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) 
开发者ID:pydata,项目名称:sparse,代码行数:43,代码来源:common.py

示例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 
开发者ID:igraph,项目名称:python-igraph,代码行数:31,代码来源:__init__.py


注:本文中的scipy.sparse.tril方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。