本文整理汇总了Python中scipy.sparse.isspmatrix_coo方法的典型用法代码示例。如果您正苦于以下问题:Python sparse.isspmatrix_coo方法的具体用法?Python sparse.isspmatrix_coo怎么用?Python sparse.isspmatrix_coo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.sparse
的用法示例。
在下文中一共展示了sparse.isspmatrix_coo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sparse_to_tuple
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def sparse_to_tuple(sparse_mx):
"""Convert sparse matrix to tuple representation."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
示例2: sparse_to_tuple
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def sparse_to_tuple(sparse_mx):
"""Convert sparse matrix to tuple representation."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
示例3: sparse_to_tuple
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def sparse_to_tuple(sparse_mx):
"""Convert sparse matrix to tuple representation."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
# All of these will need to be sorted:
sort_indices = np.lexsort(np.rot90(coords))
return coords[sort_indices], values[sort_indices], shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
示例4: sparse_to_tuple
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def sparse_to_tuple(sparse_mx, insert_batch=False):
"""Convert sparse matrix to tuple representation."""
"""Set insert_batch=True if you want to insert a batch dimension."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
if insert_batch:
coords = np.vstack((np.zeros(mx.row.shape[0]), mx.row, mx.col)).transpose()
values = mx.data
shape = (1,) + mx.shape
else:
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
示例5: sparse_to_tuple
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def sparse_to_tuple(sparse_mx):
"""Convert sparse matrix to tuple representation."""
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
示例6: sparse_to_tuple
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def sparse_to_tuple(sparse_mx):
def to_tuple(mx):
if not sp.isspmatrix_coo(mx):
mx = mx.tocoo()
coords = np.vstack((mx.row, mx.col)).transpose()
values = mx.data
shape = mx.shape
return coords, values, shape
if isinstance(sparse_mx, list):
for i in range(len(sparse_mx)):
sparse_mx[i] = to_tuple(sparse_mx[i])
else:
sparse_mx = to_tuple(sparse_mx)
return sparse_mx
示例7: sparse_to_tuple
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def sparse_to_tuple(sparse_mx):
""" change of format for sparse matrix. This format is used
for the feed_dict where sparse matrices need to be linked to placeholders
representing sparse matrices. """
if not sp.isspmatrix_coo(sparse_mx):
sparse_mx = sparse_mx.tocoo()
coords = np.vstack((sparse_mx.row, sparse_mx.col)).transpose()
values = sparse_mx.data
shape = sparse_mx.shape
return coords, values, shape
示例8: sparse_to_tuple
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def sparse_to_tuple(sparse_matrix):
if not sp.isspmatrix_coo(sparse_matrix):
sparse_matrix = sparse_matrix.tocoo()
indices = np.vstack((sparse_matrix.row, sparse_matrix.col)).transpose()
values = sparse_matrix.data
shape = sparse_matrix.shape
return indices, values, shape
示例9: sparse_to_tuple
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def sparse_to_tuple(sparse_mx):
if not sp.isspmatrix_coo(sparse_mx):
sparse_mx = sparse_mx.tocoo()
coords = np.vstack((sparse_mx.row, sparse_mx.col)).transpose()
values = sparse_mx.data
shape = sparse_mx.shape
return coords, values, shape
示例10: test_sparse_dtm
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def test_sparse_dtm(docs, pass_vocab):
if pass_vocab:
vocab = vocabulary(docs, sort=True)
dtm = sparse_dtm(docs, vocab)
else:
dtm, vocab = sparse_dtm(docs)
assert isspmatrix_coo(dtm)
assert dtm.shape == (len(docs), len(vocab))
assert vocab == vocabulary(docs, sort=True)
示例11: test_add_dummy_feature_coo
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def test_add_dummy_feature_coo():
X = sparse.coo_matrix([[1, 0], [0, 1], [0, 1]])
X = add_dummy_feature(X)
assert sparse.isspmatrix_coo(X), X
assert_array_equal(X.toarray(), [[1, 1, 0], [1, 0, 1], [1, 0, 1]])
示例12: test_coo
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def test_coo(self):
x = sparse.coo_matrix(
(cupy.array([0], 'f'),
(cupy.array([0], 'i'), cupy.array([0], 'i'))),
shape=(1, 1), dtype='f')
self.assertTrue(sparse.isspmatrix_coo(x))
示例13: test_csr
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def test_csr(self):
x = sparse.csr_matrix(
(cupy.array([], 'f'),
cupy.array([], 'i'),
cupy.array([0], 'i')),
shape=(0, 0), dtype='f')
self.assertFalse(sparse.isspmatrix_coo(x))
示例14: save_sparse
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def save_sparse(sparse_matrix, output_filename):
assert sparse.issparse(sparse_matrix)
if sparse.isspmatrix_coo(sparse_matrix):
coo = sparse_matrix
else:
coo = sparse_matrix.tocoo()
row = coo.row
col = coo.col
data = coo.data
shape = coo.shape
np.savez(output_filename, row=row, col=col, data=data, shape=shape)
示例15: preprocess_adj_bias
# 需要导入模块: from scipy import sparse [as 别名]
# 或者: from scipy.sparse import isspmatrix_coo [as 别名]
def preprocess_adj_bias(adj):
num_nodes = adj.shape[0]
adj = adj + sp.eye(num_nodes) # self-loop
adj[adj > 0.0] = 1.0
if not sp.isspmatrix_coo(adj):
adj = adj.tocoo()
adj = adj.astype(np.float32)
indices = np.vstack((adj.col, adj.row)).transpose()
return indices, adj.data, adj.shape