本文整理匯總了Python中scipy.sparse.isspmatrix_lil方法的典型用法代碼示例。如果您正苦於以下問題:Python sparse.isspmatrix_lil方法的具體用法?Python sparse.isspmatrix_lil怎麽用?Python sparse.isspmatrix_lil使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy.sparse
的用法示例。
在下文中一共展示了sparse.isspmatrix_lil方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sparse_remove_row
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import isspmatrix_lil [as 別名]
def sparse_remove_row(X, to_remove):
""" Delete rows from a sparse matrix
Parameters
----------
X : scipy.sparse matrix
to_remove : a list of row indices to be removed.
Returns
-------
Y : scipy.sparse matrix
"""
if not sps.isspmatrix_lil(X):
X = X.tolil()
to_keep = [i for i in iter(range(0, X.shape[0])) if i not in to_remove]
Y = sps.vstack([X.getrowview(i) for i in to_keep])
return Y
示例2: is_list_of_lil
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import isspmatrix_lil [as 別名]
def is_list_of_lil(z):
return isinstance(z, list) and sparse.isspmatrix_lil(z[0])
示例3: is_lil
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import isspmatrix_lil [as 別名]
def is_lil(z):
return sparse.isspmatrix_lil(z)
示例4: _fix_connectivity
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import isspmatrix_lil [as 別名]
def _fix_connectivity(X, connectivity, affinity):
"""
Fixes the connectivity matrix
- copies it
- makes it symmetric
- converts it to LIL if necessary
- completes it if necessary
"""
n_samples = X.shape[0]
if (connectivity.shape[0] != n_samples or
connectivity.shape[1] != n_samples):
raise ValueError('Wrong shape for connectivity matrix: %s '
'when X is %s' % (connectivity.shape, X.shape))
# Make the connectivity matrix symmetric:
connectivity = connectivity + connectivity.T
# Convert connectivity matrix to LIL
if not sparse.isspmatrix_lil(connectivity):
if not sparse.isspmatrix(connectivity):
connectivity = sparse.lil_matrix(connectivity)
else:
connectivity = connectivity.tolil()
# Compute the number of nodes
n_connected_components, labels = connected_components(connectivity)
if n_connected_components > 1:
warnings.warn("the number of connected components of the "
"connectivity matrix is %d > 1. Completing it to avoid "
"stopping the tree early." % n_connected_components,
stacklevel=2)
# XXX: Can we do without completing the matrix?
for i in range(n_connected_components):
idx_i = np.where(labels == i)[0]
Xi = X[idx_i]
for j in range(i):
idx_j = np.where(labels == j)[0]
Xj = X[idx_j]
D = pairwise_distances(Xi, Xj, metric=affinity)
ii, jj = np.where(D == np.min(D))
ii = ii[0]
jj = jj[0]
connectivity[idx_i[ii], idx_j[jj]] = True
connectivity[idx_j[jj], idx_i[ii]] = True
return connectivity, n_connected_components
示例5: _fix_connectivity
# 需要導入模塊: from scipy import sparse [as 別名]
# 或者: from scipy.sparse import isspmatrix_lil [as 別名]
def _fix_connectivity(X, connectivity, affinity):
"""
Fixes the connectivity matrix
- copies it
- makes it symmetric
- converts it to LIL if necessary
- completes it if necessary
"""
n_samples = X.shape[0]
if (connectivity.shape[0] != n_samples or
connectivity.shape[1] != n_samples):
raise ValueError('Wrong shape for connectivity matrix: %s '
'when X is %s' % (connectivity.shape, X.shape))
# Make the connectivity matrix symmetric:
connectivity = connectivity + connectivity.T
# Convert connectivity matrix to LIL
if not sparse.isspmatrix_lil(connectivity):
if not sparse.isspmatrix(connectivity):
connectivity = sparse.lil_matrix(connectivity)
else:
connectivity = connectivity.tolil()
# Compute the number of nodes
n_components, labels = connected_components(connectivity)
if n_components > 1:
warnings.warn("the number of connected components of the "
"connectivity matrix is %d > 1. Completing it to avoid "
"stopping the tree early." % n_components,
stacklevel=2)
# XXX: Can we do without completing the matrix?
for i in xrange(n_components):
idx_i = np.where(labels == i)[0]
Xi = X[idx_i]
for j in xrange(i):
idx_j = np.where(labels == j)[0]
Xj = X[idx_j]
D = pairwise_distances(Xi, Xj, metric=affinity)
ii, jj = np.where(D == np.min(D))
ii = ii[0]
jj = jj[0]
connectivity[idx_i[ii], idx_j[jj]] = True
connectivity[idx_j[jj], idx_i[ii]] = True
return connectivity, n_components
###############################################################################
# Hierarchical tree building functions