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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19710602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:24:49  来源:igfitidea点击:

Concatenate sparse matrices in Python using SciPy/Numpy

pythonnumpyscipysparse-matrix

提问by PascalVKooten

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.

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

采纳答案by Saullo G. P. Castro

You can use the scipy.sparse.hstack:

您可以使用scipy.sparse.hstack

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

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

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