Concatenate sparse matrices in Python using SciPy/Numpy

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

What would be the most efficient way to concatenate sparse matrices in Python using SciPy/Numpy?

Here I used the following:

>>> np.hstack((X, X2)) array([ '         with 1135520 stored elements in Compressed Sparse Row format>,         '         with 1135520 stored elements in Compressed Sparse Row format>],         dtype=object) 

I would like to use both predictors in a regression, but the current format is obviously not what I'm looking for. Would it be possible to get the following:

    '      with 2271040 stored elements in Compressed Sparse Row format> 

It is too large to be converted to a deep format.

回答1:

You can use the scipy.sparse.hstack:

from scipy.sparse import hstack hstack((X, X2)) 

Using the numpy.hstack will create an array with two sparse matrix objects.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!