【发布时间】:2013-08-27 09:28:48
【问题描述】:
是否可以在 python 中堆叠稀疏和密集的 numpy 数组?我知道这可以使用 vstack/hstack 为密集的 numpy 数组完成。我有一些列想添加到稀疏矩阵中以增加特征向量的数量
【问题讨论】:
标签: python arrays numpy matrix scipy
是否可以在 python 中堆叠稀疏和密集的 numpy 数组?我知道这可以使用 vstack/hstack 为密集的 numpy 数组完成。我有一些列想添加到稀疏矩阵中以增加特征向量的数量
【问题讨论】:
标签: python arrays numpy matrix scipy
是的,您可以使用scipy.sparse.vstack 和scipy.sparse.hstack,就像对密集数组使用numpy.vstack 和numpy.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>
【讨论】: