我在调整矩阵大小时遇到了麻烦 - 似乎是set_shape
函数
没有效果:
>>> M
<14x3562 sparse matrix of type '<type 'numpy.float32'>'
with 6136 stored elements in LInked List format>
>>> new_shape = (15,3562)
>>> M.set_shape(new_shape)
>>> M
<14x3562 sparse matrix of type '<type 'numpy.float32'>'
with 6136 stored elements in LInked List format>
其他人遇到过这个?
我也尝试过这样做,即
>>> M._shape = new_shape
>>> M.data = np.concatenate(M.data, np.empty((0,0), dtype=np.float32))
但是会引发错误:
*** TypeError: only length-1 arrays can be converted to Python scalars
或
>>> M.data = np.concatenate(M.data, [])
*** TypeError: an integer is required
有关信息:
答案 0 :(得分:5)
如果你只想在最后添加一行零:
>>> M = sp.lil_matrix((14, 3562))
>>> sp.vstack([M, sp.lil_matrix((1, 3562))])
<15x3562 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in COOrdinate format>