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


Python sparse.coo_matrix方法代码示例

本文整理汇总了Python中scipy.sparse.coo_matrix方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.coo_matrix方法的具体用法?Python sparse.coo_matrix怎么用?Python sparse.coo_matrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在scipy.sparse的用法示例。


在下文中一共展示了sparse.coo_matrix方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: transform

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def transform(self, X):
        """Encode categorical columns into sparse matrix with one-hot-encoding.

        Args:
            X (pandas.DataFrame): categorical columns to encode

        Returns:
            (scipy.sparse.coo_matrix): sparse matrix encoding categorical
                                       variables into dummy variables
        """

        for i, col in enumerate(X.columns):
            X_col = self._transform_col(X[col], i)
            if X_col is not None:
                if i == 0:
                    X_new = X_col
                else:
                    X_new = sparse.hstack((X_new, X_col))

            logger.debug('{} --> {} features'.format(
                col, self.label_encoder.label_maxes[i])
            )

        return X_new 
开发者ID:jeongyoonlee,项目名称:Kaggler,代码行数:26,代码来源:categorical.py

示例2: create_inverse_degree_matrix

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def create_inverse_degree_matrix(edges):
    """
    Creating an inverse degree matrix from an edge list.
    :param edges: Edge list.
    :return D_1: Inverse degree matrix.
    """
    graph = nx.from_edgelist(edges)
    ind = range(len(graph.nodes()))
    degs = [1.0/graph.degree(node) for node in range(graph.number_of_nodes())]

    D_1 = sparse.coo_matrix((degs, (ind, ind)),
                            shape=(graph.number_of_nodes(),
                            graph.number_of_nodes()),
                            dtype=np.float32)

    return D_1 
开发者ID:benedekrozemberczki,项目名称:GraRep,代码行数:18,代码来源:utils.py

示例3: vnucele_coo_coulomb

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def vnucele_coo_coulomb(sv, **kw):
  """
  Computes the matrix elements defined by 
    Vne = f(r) sum_a   Z_a/|r-R_a|  g(r)
  Args:
    sv : (System Variables), this must have arrays of coordinates and species, etc
  Returns:
    matrix elements
  """
  from numpy import einsum, dot
  from scipy.sparse import coo_matrix
  g = sv.build_3dgrid_ae(**kw)
  ca2o = sv.comp_aos_den(g.coords)
  vnuc = sv.comp_vnuc_coulomb(g.coords)
  vnuc_w = g.weights*vnuc
  cb2vo = einsum('co,c->co', ca2o, vnuc_w)
  vne = dot(ca2o.T,cb2vo)
  return coo_matrix(vne) 
开发者ID:pyscf,项目名称:pyscf,代码行数:20,代码来源:m_vnucele_coo_coulomb.py

示例4: get_da2cc_sparse

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def get_da2cc_sparse(self, dtype=np.float64, sparseformat=coo_matrix):
    """ Returns Conversion Coefficients as sparse COO matrix """

    nfdp,nfap = self.dpc2s[-1],self.c2s[-1]
    nnz = self.get_da2cc_nnz()
    irow,icol,data = zeros(nnz, dtype=int),zeros(nnz, dtype=int), zeros(nnz, dtype=dtype) # Start to construct coo matrix

    inz = 0
    for atom, [sd,fd,pt] in enumerate(zip(self.dpc2s,self.dpc2s[1:],self.dpc2t)):
      if pt!=1: continue
      for d in range(sd,fd): 
        irow[inz],icol[inz],data[inz] = d,d,1.0
        inz+=1

    for atom, [sd,fd,pt,spp] in enumerate(zip(self.dpc2s,self.dpc2s[1:],self.dpc2t,self.dpc2sp)):
      if pt==1: continue
      inf = self.bp2info[spp]
      for c,ls,lf in zip(inf.cc2a, inf.cc2s, inf.cc2s[1:]): 
        for d in range(sd,fd):
          for a in range(self.c2s[c],self.c2s[c+1]):
            irow[inz],icol[inz],data[inz] = d,a,inf.cc[d-sd,a-self.c2s[c]+ls]
            inz+=1
    return sparseformat((data,(irow,icol)), dtype=dtype, shape=(nfdp, nfap)) 
开发者ID:pyscf,项目名称:pyscf,代码行数:25,代码来源:prod_basis.py

示例5: get_degree_directed

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def get_degree_directed(graph):
    graph = spsp.coo_matrix(graph)

    out_degree_vector = np.zeros(graph.shape[0], dtype=np.float64)
    out_weighted_degree_vector = np.zeros(graph.shape[0], dtype=np.float64)

    in_degree_vector = np.zeros(graph.shape[0], dtype=np.float64)
    in_weighted_degree_vector = np.zeros(graph.shape[0], dtype=np.float64)

    for i, j, d in zip(graph.row, graph.col, graph.data):
        out_degree_vector[i] += 1.0
        in_degree_vector[j] += 1.0

        out_weighted_degree_vector[i] += d
        in_weighted_degree_vector[j] += d

    total_degree_vector = out_degree_vector + in_degree_vector
    total_weighted_degree_vector = out_weighted_degree_vector + in_weighted_degree_vector

    return out_degree_vector,\
           out_weighted_degree_vector, \
           in_degree_vector, \
           in_weighted_degree_vector, \
           total_degree_vector,\
           total_weighted_degree_vector 
开发者ID:MKLab-ITI,项目名称:news-popularity-prediction,代码行数:27,代码来源:common.py

示例6: randomedge_sampler

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def randomedge_sampler(self, percent, normalization, cuda):
        """
        Randomly drop edge and preserve percent% edges.
        """
        "Opt here"
        if percent >= 1.0:
            return self.stub_sampler(normalization, cuda)
        
        nnz = self.train_adj.nnz
        perm = np.random.permutation(nnz)
        preserve_nnz = int(nnz*percent)
        perm = perm[:preserve_nnz]
        r_adj = sp.coo_matrix((self.train_adj.data[perm],
                               (self.train_adj.row[perm],
                                self.train_adj.col[perm])),
                              shape=self.train_adj.shape)
        r_adj = self._preprocess_adj(normalization, r_adj, cuda)
        fea = self._preprocess_fea(self.train_features, cuda)
        return r_adj, fea 
开发者ID:DropEdge,项目名称:DropEdge,代码行数:21,代码来源:sample.py

示例7: degree_sampler

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def degree_sampler(self, percent, normalization, cuda):
        """
        Randomly drop edge wrt degree (high degree, low probility).
        """
        if percent >= 0:
            return self.stub_sampler(normalization, cuda)
        if self.degree_p is None:
            degree_adj = self.train_adj.multiply(self.degree)
            self.degree_p = degree_adj.data / (1.0 * np.sum(degree_adj.data))
        # degree_adj = degree_adj.multi degree_adj.sum()
        nnz = self.train_adj.nnz
        preserve_nnz = int(nnz * percent)
        perm = np.random.choice(nnz, preserve_nnz, replace=False, p=self.degree_p)
        r_adj = sp.coo_matrix((self.train_adj.data[perm],
                               (self.train_adj.row[perm],
                                self.train_adj.col[perm])),
                              shape=self.train_adj.shape)
        r_adj = self._preprocess_adj(normalization, r_adj, cuda)
        fea = self._preprocess_fea(self.train_features, cuda)
        return r_adj, fea 
开发者ID:DropEdge,项目名称:DropEdge,代码行数:22,代码来源:sample.py

示例8: test_cast

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def test_cast():
    m = spsp.coo_matrix(([1, 1], ([0, 1], [1, 2])), (4, 4))
    g = dgl.DGLGraph(m, readonly=True)
    gsrc, gdst = g.edges(order='eid')
    ndata = F.randn((4, 5))
    edata = F.randn((2, 4))
    g.ndata['x'] = ndata
    g.edata['y'] = edata

    hg = dgl.as_heterograph(g, 'A', 'AA')
    assert hg.ntypes == ['A']
    assert hg.etypes == ['AA']
    assert hg.canonical_etypes == [('A', 'AA', 'A')]
    assert hg.number_of_nodes() == 4
    assert hg.number_of_edges() == 2
    hgsrc, hgdst = hg.edges(order='eid')
    assert F.array_equal(gsrc, hgsrc)
    assert F.array_equal(gdst, hgdst)

    g2 = dgl.as_immutable_graph(hg)
    assert g2.number_of_nodes() == 4
    assert g2.number_of_edges() == 2
    g2src, g2dst = hg.edges(order='eid')
    assert F.array_equal(g2src, gsrc)
    assert F.array_equal(g2dst, gdst) 
开发者ID:dmlc,项目名称:dgl,代码行数:27,代码来源:test_transform.py

示例9: create_test_heterograph2

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def create_test_heterograph2(index_dtype):
    plays_spmat = ssp.coo_matrix(([1, 1, 1, 1], ([0, 1, 2, 1], [0, 0, 1, 1])))
    wishes_nx = nx.DiGraph()
    wishes_nx.add_nodes_from(['u0', 'u1', 'u2'], bipartite=0)
    wishes_nx.add_nodes_from(['g0', 'g1'], bipartite=1)
    wishes_nx.add_edge('u0', 'g1', id=0)
    wishes_nx.add_edge('u2', 'g0', id=1)
    develops_g = dgl.bipartite([(0, 0), (1, 1)], 'developer', 'develops', 'game')

    g = dgl.heterograph({
        ('user', 'follows', 'user'): [(0, 1), (1, 2)],
        ('user', 'plays', 'game'): plays_spmat,
        ('user', 'wishes', 'game'): wishes_nx,
        ('developer', 'develops', 'game'): develops_g,
        })
    return g 
开发者ID:dmlc,项目名称:dgl,代码行数:18,代码来源:test_heterograph.py

示例10: create_test_heterograph3

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def create_test_heterograph3(index_dtype):
    plays_spmat = ssp.coo_matrix(([1, 1, 1, 1], ([0, 1, 2, 1], [0, 0, 1, 1])))
    wishes_nx = nx.DiGraph()
    wishes_nx.add_nodes_from(['u0', 'u1', 'u2'], bipartite=0)
    wishes_nx.add_nodes_from(['g0', 'g1'], bipartite=1)
    wishes_nx.add_edge('u0', 'g1', id=0)
    wishes_nx.add_edge('u2', 'g0', id=1)

    follows_g = dgl.graph([(0, 1), (1, 2)], 'user', 'follows', _restrict_format='coo')
    plays_g = dgl.bipartite(
        [(0, 0), (1, 0), (2, 1), (1, 1)], 'user', 'plays', 'game', _restrict_format='coo')
    wishes_g = dgl.bipartite([(0, 1), (2, 0)], 'user', 'wishes', 'game', _restrict_format='coo')
    develops_g = dgl.bipartite(
        [(0, 0), (1, 1)], 'developer', 'develops', 'game', _restrict_format='coo')
    g = dgl.hetero_from_relations([follows_g, plays_g, wishes_g, develops_g])
    return g 
开发者ID:dmlc,项目名称:dgl,代码行数:18,代码来源:test_heterograph.py

示例11: test_pickling_heterograph

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def test_pickling_heterograph():
    # copied from test_heterograph.create_test_heterograph()
    plays_spmat = ssp.coo_matrix(([1, 1, 1, 1], ([0, 1, 2, 1], [0, 0, 1, 1])))
    wishes_nx = nx.DiGraph()
    wishes_nx.add_nodes_from(['u0', 'u1', 'u2'], bipartite=0)
    wishes_nx.add_nodes_from(['g0', 'g1'], bipartite=1)
    wishes_nx.add_edge('u0', 'g1', id=0)
    wishes_nx.add_edge('u2', 'g0', id=1)

    follows_g = dgl.graph([(0, 1), (1, 2)], 'user', 'follows')
    plays_g = dgl.bipartite(plays_spmat, 'user', 'plays', 'game')
    wishes_g = dgl.bipartite(wishes_nx, 'user', 'wishes', 'game')
    develops_g = dgl.bipartite([(0, 0), (1, 1)], 'developer', 'develops', 'game')
    g = dgl.hetero_from_relations([follows_g, plays_g, wishes_g, develops_g])

    g.nodes['user'].data['u_h'] = F.randn((3, 4))
    g.nodes['game'].data['g_h'] = F.randn((2, 5))
    g.edges['plays'].data['p_h'] = F.randn((4, 6))

    new_g = _reconstruct_pickle(g)
    _assert_is_identical_hetero(g, new_g) 
开发者ID:dmlc,项目名称:dgl,代码行数:23,代码来源:test_pickle.py

示例12: test_pickling_heterograph_index_compatibility

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def test_pickling_heterograph_index_compatibility():
    plays_spmat = ssp.coo_matrix(([1, 1, 1, 1], ([0, 1, 2, 1], [0, 0, 1, 1])))
    wishes_nx = nx.DiGraph()
    wishes_nx.add_nodes_from(['u0', 'u1', 'u2'], bipartite=0)
    wishes_nx.add_nodes_from(['g0', 'g1'], bipartite=1)
    wishes_nx.add_edge('u0', 'g1', id=0)
    wishes_nx.add_edge('u2', 'g0', id=1)

    follows_g = dgl.graph([(0, 1), (1, 2)], 'user', 'follows')
    plays_g = dgl.bipartite(plays_spmat, 'user', 'plays', 'game')
    wishes_g = dgl.bipartite(wishes_nx, 'user', 'wishes', 'game')
    develops_g = dgl.bipartite([(0, 0), (1, 1)], 'developer', 'develops', 'game')
    g = dgl.hetero_from_relations([follows_g, plays_g, wishes_g, develops_g])

    with open("tests/compute/hetero_pickle_old.pkl", "rb") as f:
        gi = pickle.load(f)
        f.close()
    new_g = dgl.DGLHeteroGraph(gi, g.ntypes, g.etypes)
    _assert_is_identical_hetero(g, new_g) 
开发者ID:dmlc,项目名称:dgl,代码行数:21,代码来源:test_pickle.py

示例13: mmread

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def mmread(source):
    """
    Reads the contents of a Matrix Market file-like 'source' into a matrix.

    Parameters
    ----------
    source : str or file-like
        Matrix Market filename (extensions .mtx, .mtz.gz)
        or open file-like object.

    Returns
    -------
    a : ndarray or coo_matrix
        Dense or sparse matrix depending on the matrix format in the
        Matrix Market file.
    """
    return MMFile().read(source)

# ----------------------------------------------------------------------------- 
开发者ID:ryfeus,项目名称:lambda-packs,代码行数:21,代码来源:mmio.py

示例14: _transform_col

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def _transform_col(self, x, i):
        """Encode one categorical column into sparse matrix with one-hot-encoding.

        Args:
            x (pandas.Series): a categorical column to encode
            i (int): column index

        Returns:
            (scipy.sparse.coo_matrix): sparse matrix encoding a categorical
                                       variable into dummy variables
        """

        labels = self.label_encoder._transform_col(x, i)
        label_max = self.label_encoder.label_maxes[i]

        # build row and column index for non-zero values of a sparse matrix
        index = np.array(range(len(labels)))
        i = index[labels > 0]
        j = labels[labels > 0] - 1  # column index starts from 0

        if len(i) > 0:
            return sparse.coo_matrix((np.ones_like(i), (i, j)),
                                     shape=(x.shape[0], label_max))
        else:
            # if there is no non-zero value, return no matrix
            return None 
开发者ID:jeongyoonlee,项目名称:Kaggler,代码行数:28,代码来源:categorical.py

示例15: normalize_adjacency

# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import coo_matrix [as 别名]
def normalize_adjacency(edges):
    """
    Method to calculate a sparse degree normalized adjacency matrix.
    :param edges: Edge list of graph.
    :return A: Normalized adjacency matrix.
    """
    D_1 = create_inverse_degree_matrix(edges)
    index_1 = [edge[0] for edge in edges] + [edge[1] for edge in edges]
    index_2 = [edge[1] for edge in edges] + [edge[0] for edge in edges]
    values = [1.0 for edge in edges] + [1.0 for edge in edges]
    A = sparse.coo_matrix((values, (index_1, index_2)),
                          shape=D_1.shape,
                          dtype=np.float32)
    A = A.dot(D_1)
    return A 
开发者ID:benedekrozemberczki,项目名称:GraRep,代码行数:17,代码来源:utils.py


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