gpt4 book ai didi

python - scipy.sparse.hstack(([1], [2])) -> "ValueError: blocks must be 2-D"。为什么?

转载 作者:太空狗 更新时间:2023-10-30 00:40:03 30 4
gpt4 key购买 nike

scipy.sparse.hstack((1, [2]))scipy.sparse.hstack((1, [2])) 运行良好,但是不是 scipy.sparse.hstack(([1], [2]))。为什么会这样?

这是我系统上发生的事情的痕迹:


C:\Anaconda>python
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.
1500 64 bit (AMD64)] on win32
>>> import scipy.sparse
>>> scipy.sparse.hstack((1, [2]))
<1x2 sparse matrix of type '<type 'numpy.int32'>'
with 2 stored elements in COOrdinate format>
>>> scipy.sparse.hstack((1, 2))
<1x2 sparse matrix of type '<type 'numpy.int32'>'
with 2 stored elements in COOrdinate format>
>>> scipy.sparse.hstack(([1], [2]))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda\lib\site-packages\scipy\sparse\construct.py", line 456, in h
stack
return bmat([blocks], format=format, dtype=dtype)
File "C:\Anaconda\lib\site-packages\scipy\sparse\construct.py", line 539, in b
mat
raise ValueError('blocks must be 2-D')
ValueError: blocks must be 2-D
>>> scipy.version.full_version
'0.16.0'
>>>

最佳答案

scipy.sparse.hstack((1, [2]))的第一种情况下,数字1被解释为标量值,数字2被解释为稠密矩阵,因此,当您将这两件事结合在一起时,数据类型会被强制转换为标量,您可以正常将其与 scipy.sparse.hstack 结合起来。

这里有一些更多的测试来证明对于多个值也是如此:

In [31]: scipy.sparse.hstack((1,2,[3],[4]))
Out[31]:
<1x4 sparse matrix of type '<type 'numpy.int64'>'
with 4 stored elements in COOrdinate format>

In [32]: scipy.sparse.hstack((1,2,[3],[4],5,6))
Out[32]:
<1x6 sparse matrix of type '<type 'numpy.int64'>'
with 6 stored elements in COOrdinate format>

In [33]: scipy.sparse.hstack((1,[2],[3],[4],5,[6],7))
Out[33]:
<1x7 sparse matrix of type '<type 'numpy.int64'>'

如您所见,如果 hstack 中至少有一个标量,这似乎可行。

但是,当您尝试执行 scipy.sparse.hstack(([1],[2])) 的第二种情况时,它们不再都是标量,而是都是稠密的矩阵,并且您不能将 scipy.sparse.hstack 与纯稠密矩阵一起使用。

重现:

In [34]: scipy.sparse.hstack(([1],[2]))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-45-cd79952b2e14> in <module>()
----> 1 scipy.sparse.hstack(([1],[2]))

/usr/local/lib/python2.7/site-packages/scipy/sparse/construct.pyc in hstack(blocks, format, dtype)
451
452 """
--> 453 return bmat([blocks], format=format, dtype=dtype)
454
455

/usr/local/lib/python2.7/site-packages/scipy/sparse/construct.pyc in bmat(blocks, format, dtype)
531
532 if blocks.ndim != 2:
--> 533 raise ValueError('blocks must be 2-D')
534
535 M,N = blocks.shape

ValueError: blocks must be 2-D

有关更多信息,请参阅此帖子:Scipy error with sparse hstack

因此,如果你想成功地使用这两个矩阵,你必须先使它们稀疏化,然后再合并它们:

In [36]: A = scipy.sparse.coo_matrix([1])

In [37]: B = scipy.sparse.coo_matrix([2])

In [38]: C = scipy.sparse.hstack([A, B])

In [39]: C
Out[39]:
<1x2 sparse matrix of type '<type 'numpy.int64'>'
with 2 stored elements in COOrdinate format>

有趣的是,如果您尝试使用密集版本的 hstacknumpy.hstack,那么它是完全可以接受的:

In [48]: import numpy as np

In [49]: np.hstack((1, [2]))
Out[49]: array([1, 2])

.... 稀疏矩阵表示的事情搞砸了 ¯\_(ツ)_/¯

关于python - scipy.sparse.hstack(([1], [2])) -> "ValueError: blocks must be 2-D"。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31900567/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com