简体   繁体   English

python(scipy):调整稀疏矩阵的大小

[英]python (scipy): Resizing a sparse matrix

I'm having trouble resizing a matrix - the set_shape function seems to have no effect: 我在调整矩阵大小时遇到​​麻烦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>

Anyone else come across this? 还有其他人遇到吗?

I also tried doing this by hand, ie 我也尝试过手动操作,即

>>> M._shape = new_shape
>>> M.data = np.concatenate(M.data, np.empty((0,0), dtype=np.float32))

but that throws up an error: 但这引发了一个错误:

*** TypeError: only length-1 arrays can be converted to Python scalars

or 要么

>>> M.data = np.concatenate(M.data, [])
*** TypeError: an integer is required

For info: 有关信息:

  • Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) Python 2.6.5(r265:79063,2010年4月16日,13:57:41)
  • scipy 0.11.0.dev-03f9e4a scipy 0.11.0.dev-03f9e4a

If you just want to add a row of zeros at the end: 如果只想在末尾添加零行:

>>> 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM