使用SciPy / Numpy在Python中连接稀疏矩阵

时间:2020-12-26 21:42:20

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

使用SciPy / Numpy在Python中连接稀疏矩阵的最有效方法是什么?

Here I used the following:

我在这里使用了以下内容:

>>> np.hstack((X, X2))
array([ <49998x70000 sparse matrix of type '<class 'numpy.float64'>'
        with 1135520 stored elements in Compressed Sparse Row format>,
        <49998x70000 sparse matrix of type '<class 'numpy.int64'>'
        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:

我想在回归中使用两个预测变量,但目前的格式显然不是我想要的。是否有可能获得以下内容:

    <49998x1400000 sparse matrix of type '<class 'numpy.float64'>'
     with 2271040 stored elements in Compressed Sparse Row format>

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

它太大而无法转换为深层格式。

1 个解决方案

#1


41  

You can use the scipy.sparse.hstack:

您可以使用scipy.sparse.hstack:

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

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

使用numpy.hstack将创建一个包含两个稀疏矩阵对象的数组。

#1


41  

You can use the scipy.sparse.hstack:

您可以使用scipy.sparse.hstack:

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

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

使用numpy.hstack将创建一个包含两个稀疏矩阵对象的数组。