Python scipy.sparse.coo_matrix() Examples
The following are 30
code examples of scipy.sparse.coo_matrix().
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: test_pickle.py From dgl with Apache License 2.0 | 6 votes |
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)
Example #2
Source File: prod_basis.py From pyscf with Apache License 2.0 | 6 votes |
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))
Example #3
Source File: sample.py From DropEdge with MIT License | 6 votes |
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
Example #4
Source File: sample.py From DropEdge with MIT License | 6 votes |
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
Example #5
Source File: m_vnucele_coo_coulomb.py From pyscf with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: test_transform.py From dgl with Apache License 2.0 | 6 votes |
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)
Example #7
Source File: test_heterograph.py From dgl with Apache License 2.0 | 6 votes |
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
Example #8
Source File: test_heterograph.py From dgl with Apache License 2.0 | 6 votes |
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
Example #9
Source File: test_pickle.py From dgl with Apache License 2.0 | 6 votes |
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)
Example #10
Source File: mmio.py From lambda-packs with MIT License | 6 votes |
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) # -----------------------------------------------------------------------------
Example #11
Source File: common.py From news-popularity-prediction with Apache License 2.0 | 6 votes |
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
Example #12
Source File: utils.py From GraRep with GNU General Public License v3.0 | 6 votes |
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
Example #13
Source File: categorical.py From Kaggler with MIT License | 6 votes |
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
Example #14
Source File: test_heterograph.py From dgl with Apache License 2.0 | 5 votes |
def create_test_heterograph(index_dtype): # test heterograph from the docstring, plus a user -- wishes -- game relation # 3 users, 2 games, 2 developers # metagraph: # ('user', 'follows', 'user'), # ('user', 'plays', 'game'), # ('user', 'wishes', 'game'), # ('developer', 'develops', 'game')]) 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', index_dtype=index_dtype) plays_g = dgl.bipartite(plays_spmat, 'user', 'plays', 'game', index_dtype=index_dtype) wishes_g = dgl.bipartite(wishes_nx, 'user', 'wishes', 'game', index_dtype=index_dtype) develops_g = dgl.bipartite([(0, 0), (1, 1)], 'developer', 'develops', 'game', index_dtype=index_dtype) assert follows_g._idtype_str == index_dtype assert plays_g._idtype_str == index_dtype assert wishes_g._idtype_str == index_dtype assert develops_g._idtype_str == index_dtype g = dgl.hetero_from_relations([follows_g, plays_g, wishes_g, develops_g]) assert g._idtype_str == index_dtype return g
Example #15
Source File: tensor.py From dgl with Apache License 2.0 | 5 votes |
def sparse_matrix(data, index, shape, force_format=False): fmt = index[0] if fmt == 'coo': i = index[1][0,:] j = index[1][1,:] return sp.coo_matrix((data, (i, j)), shape=shape) elif fmt == 'csr': indices = index[1] indptr = index[2] return sp.csr_matrix((data, indices, indptr), shape=shape) else: raise TypeError('Invalid format: %s.' % fmt)
Example #16
Source File: test_graph.py From dgl with Apache License 2.0 | 5 votes |
def scipy_coo_input(): src, dst = edge_pair_input() return sp.coo_matrix((np.ones((20,)), (src, dst)), shape=(10,10))
Example #17
Source File: mmio.py From lambda-packs with MIT License | 5 votes |
def read(self, 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 object. Returns ------- a : ndarray or coo_matrix Dense or sparse matrix depending on the matrix format in the Matrix Market file. """ stream, close_it = self._open(source) try: self._parse_header(stream) return self._parse_body(stream) finally: if close_it: stream.close() # -------------------------------------------------------------------------
Example #18
Source File: test_graph.py From dgl with Apache License 2.0 | 5 votes |
def scipy_csr_input(): src, dst = edge_pair_input() csr = sp.coo_matrix((np.ones((20,)), (src, dst)), shape=(10,10)).tocsr() csr.sort_indices() # src = [0 0 0 1 1 2 2 3 3 4 4 4 4 5 5 6 7 7 7 9] # dst = [4 6 9 3 5 3 7 5 8 1 3 4 9 1 9 6 2 8 9 2] return csr
Example #19
Source File: normalization.py From DropEdge with MIT License | 5 votes |
def random_walk_laplacian(adj): adj = sp.coo_matrix(adj) row_sum = np.array(adj.sum(1)) d_inv = np.power(row_sum, -1.0).flatten() d_mat = sp.diags(d_inv) return (sp.eye(adj.shape[0]) - d_mat.dot(adj)).tocoo()
Example #20
Source File: normalization.py From DropEdge with MIT License | 5 votes |
def normalized_adjacency(adj): adj = sp.coo_matrix(adj) row_sum = np.array(adj.sum(1)) d_inv_sqrt = np.power(row_sum, -0.5).flatten() d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0. d_mat_inv_sqrt = sp.diags(d_inv_sqrt) return (d_mat_inv_sqrt.dot(adj).dot(d_mat_inv_sqrt)).tocoo()
Example #21
Source File: data.py From dgl with Apache License 2.0 | 5 votes |
def _generate_dec_graph(self, rating_pairs): ones = np.ones_like(rating_pairs[0]) user_movie_ratings_coo = sp.coo_matrix( (ones, rating_pairs), shape=(self.num_user, self.num_movie), dtype=np.float32) return dgl.bipartite(user_movie_ratings_coo, 'user', 'rate', 'movie')
Example #22
Source File: normalization.py From DropEdge with MIT License | 5 votes |
def bingge_norm_adjacency(adj): adj = adj + sp.eye(adj.shape[0]) adj = sp.coo_matrix(adj) row_sum = np.array(adj.sum(1)) d_inv_sqrt = np.power(row_sum, -0.5).flatten() d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0. d_mat_inv_sqrt = sp.diags(d_inv_sqrt) return (d_mat_inv_sqrt.dot(adj).dot(d_mat_inv_sqrt) + sp.eye(adj.shape[0])).tocoo()
Example #23
Source File: normalization.py From DropEdge with MIT License | 5 votes |
def gcn(adj): adj = sp.coo_matrix(adj) row_sum = np.array(adj.sum(1)) d_inv_sqrt = np.power(row_sum, -0.5).flatten() d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0. d_mat_inv_sqrt = sp.diags(d_inv_sqrt) return (sp.eye(adj.shape[0]) + d_mat_inv_sqrt.dot(adj).dot(d_mat_inv_sqrt)).tocoo()
Example #24
Source File: normalization.py From DropEdge with MIT License | 5 votes |
def laplacian(adj): adj = sp.coo_matrix(adj) row_sum = np.array(adj.sum(1)).flatten() d_mat = sp.diags(row_sum) return (d_mat - adj).tocoo()
Example #25
Source File: normalization.py From DropEdge with MIT License | 5 votes |
def normalized_laplacian(adj): adj = sp.coo_matrix(adj) row_sum = np.array(adj.sum(1)) d_inv_sqrt = np.power(row_sum, -0.5).flatten() d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0. d_mat_inv_sqrt = sp.diags(d_inv_sqrt) return (sp.eye(adj.shape[0]) - d_mat_inv_sqrt.dot(adj).dot(d_mat_inv_sqrt)).tocoo()
Example #26
Source File: test_split.py From mars with Apache License 2.0 | 5 votes |
def testTrainTestSplitSparse(self): # check that train_test_split converts scipy sparse matrices # to csr, as stated in the documentation X = np.arange(100).reshape((10, 10)) sparse_types = [sps.csr_matrix, sps.csc_matrix, sps.coo_matrix] for InputFeatureType in sparse_types: X_s = InputFeatureType(X) for x in (X_s, mt.tensor(X_s, chunk_size=(2, 5))): X_train, X_test = train_test_split(x) assert isinstance(X_train.fetch(), SparseNDArray) assert isinstance(X_test.fetch(), SparseNDArray)
Example #27
Source File: test_validation.py From mars with Apache License 2.0 | 5 votes |
def test_check_array_complex_data_error(_): X = mt.array([[1 + 2j, 3 + 4j, 5 + 7j], [2 + 3j, 4 + 5j, 6 + 7j]]) assert_raises_regex( ValueError, "Complex data not supported", check_array, X) # list of lists X = [[1 + 2j, 3 + 4j, 5 + 7j], [2 + 3j, 4 + 5j, 6 + 7j]] assert_raises_regex( ValueError, "Complex data not supported", check_array, X) # tuple of tuples X = ((1 + 2j, 3 + 4j, 5 + 7j), (2 + 3j, 4 + 5j, 6 + 7j)) assert_raises_regex( ValueError, "Complex data not supported", check_array, X) # list of np arrays X = [mt.array([1 + 2j, 3 + 4j, 5 + 7j]), mt.array([2 + 3j, 4 + 5j, 6 + 7j])] assert_raises_regex( ValueError, "Complex data not supported", check_array, X) # tuple of np arrays X = (mt.array([1 + 2j, 3 + 4j, 5 + 7j]), mt.array([2 + 3j, 4 + 5j, 6 + 7j])) assert_raises_regex( ValueError, "Complex data not supported", check_array, X) # dataframe X = MockDataFrame( mt.array([[1 + 2j, 3 + 4j, 5 + 7j], [2 + 3j, 4 + 5j, 6 + 7j]])) assert_raises_regex( ValueError, "Complex data not supported", check_array, X) # sparse matrix X = sp.coo_matrix([[0, 1 + 2j], [0, 0]]) assert_raises_regex( ValueError, "Complex data not supported", check_array, X)
Example #28
Source File: dataserializer.py From mars with Apache License 2.0 | 5 votes |
def _deserialize_sparse_nd_array(serialized): data, indices, indptr, shape = serialized empty_arr = np.zeros(0, dtype=data.dtype) target_csr = sps.coo_matrix((empty_arr, (empty_arr,) * 2), dtype=data.dtype, shape=shape if len(shape) > 1 else (1, shape[0])).tocsr() target_csr.data, target_csr.indices, target_csr.indptr = data, indices, indptr return SparseNDArray(target_csr, shape=shape)
Example #29
Source File: csr.py From skan with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _pixel_graph(image, steps, distances, num_edges, height=None): row = np.empty(num_edges, dtype=int) col = np.empty(num_edges, dtype=int) data = np.empty(num_edges, dtype=float) if height is None: k = _write_pixel_graph(image, steps, distances, row, col, data) else: k = _write_pixel_graph_height(image, height, steps, distances, row, col, data) graph = sparse.coo_matrix((data[:k], (row[:k], col[:k]))).tocsr() return graph
Example #30
Source File: make_vizualization_json.py From news-popularity-prediction with Apache License 2.0 | 5 votes |
def make_graph_json(graph): graph_json = list() graph = spsp.csr_matrix(graph) graph.sum_duplicates() graph = spsp.coo_matrix(graph) for i, j, k in zip(graph.row, graph.col, graph.data): # graph_json.append((str(int(i)), str(int(j)), str(int(k)))) graph_json.append((str(int(i)), str(int(j)))) return graph_json