使用 scipy.sparse.toarray() 的属性错误

AttributeError using scipy.sparse.toarray()

import scipy as sp
import numpy as np

a=sp.sparse.coo_matrix(np.random.randint(0,9,[4,5]))
b=sp.sparse.coo_matrix(np.random.randint(0,9,[4,2]))
sp.hstack([a,b]).toarray()

给我

AttributeError: 'numpy.ndarray' object has no attribute 'toarray'

你能帮我解决我的愚蠢错误吗?

sp.hstack(即 numpy.hstack)是普通的 dense hstack,它不会正确组合稀疏数组。它已经构建了一个 1D numpy 数组(对象 dtype;换句话说,它只是包装了 Python 级别的对象并将它们塞在那里。)你想要 scipy.sparse.hstack:

In [332]: sp.hstack([a, b])
Out[332]: 
array([<4x5 sparse matrix of type '<class 'numpy.int64'>'
    with 17 stored elements in COOrdinate format>,
       <4x2 sparse matrix of type '<class 'numpy.int64'>'
    with 7 stored elements in COOrdinate format>], dtype=object)

In [333]: sp.hstack([a, b]).toarray()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-333-4f469eb98b12> in <module>()
----> 1 sp.hstack([a, b]).toarray()

AttributeError: 'numpy.ndarray' object has no attribute 'toarray'

In [334]: sp.sparse.hstack([a, b])
Out[334]: 
<4x7 sparse matrix of type '<class 'numpy.int64'>'
    with 24 stored elements in COOrdinate format>

In [335]: sp.sparse.hstack([a, b]).toarray()
Out[335]: 
array([[3, 2, 7, 0, 5, 5, 1],
       [7, 1, 2, 1, 7, 0, 8],
       [6, 1, 6, 1, 8, 6, 2],
       [7, 6, 0, 5, 0, 8, 8]], dtype=int64)