python(scipy):调整稀疏矩阵的大小 [英] python (scipy): Resizing a sparse matrix

查看:347
本文介绍了python(scipy):调整稀疏矩阵的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法调整矩阵的大小-set_shape函数似乎 无效:

I'm having trouble resizing a matrix - the set_shape function seems to have no effect:

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

还有其他人遇到吗?

我也尝试过手动操作,即

I also tried doing this by hand, i.e.

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

有关信息:

  • Python 2.6.5(r265:79063,2010年4月16日,13:57:41)
  • 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>

这篇关于python(scipy):调整稀疏矩阵的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆