是否可以在 python 中堆叠稀疏和密集的 numpy 数组?我知道这可以使用 vstack/hstack 为密集的 numpy 数组完成。我想将一些列添加到稀疏矩阵中以增加特征向量的数量
问问题
4976 次
1 回答
11
是的,您可以使用scipy.sparse.vstack
and scipy.sparse.hstack
,就像使用numpy.vstack
andnumpy.hstack
用于密集数组一样。
例子:
from scipy.sparse import coo_matrix
m = coo_matrix(np.array([[0,0,1],[1,0,0],[1,0,0]]))
a = np.ones(m.shape)
与np.vstack
:
np.vstack((a,m))
#ValueError: all the input array dimensions except for the concatenation axis must match exactly
与scipy.sparse.vstack
:
scipy.sparse.vstack((a,m))
#<6x3 sparse matrix of type '<type 'numpy.float64'>'
# with 12 stored elements in COOrdinate format>
于 2013-08-24T11:13:10.910 回答