2015-08-09 328 views
4

scipy.sparse.hstack((1, [2]))scipy.sparse.hstack((1, [2]))很好,但不是scipy.sparse.hstack(([1], [2]))。爲什麼會這樣?scipy.sparse.hstack(([1],[2])) - >「ValueError:blocks must be 2-D」。爲什麼?

這裏是發生了什麼事我的系統上的跟蹤:


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' 
>>> 
+0

我想你的意思是'hstack(([1],2))'起作用。 – hpaulj

+0

@hpaulj謝謝,修復! –

+0

我們有沒有回答你的問題? – rayryeng

回答

3

編碼細節:

def hstack(blocks ...): 
    return bmat([blocks], ...) 

def bmat(blocks, ...): 
    blocks = np.asarray(blocks, dtype='object') 
    if blocks.ndim != 2: 
     raise ValueError('blocks must be 2-D') 
    (continue) 

所以想你的替代品(記住額外[]):

In [392]: np.asarray([(1,2)],dtype=object) 
Out[392]: array([[1, 2]], dtype=object) 

In [393]: np.asarray([(1,[2])],dtype=object) 
Out[393]: array([[1, [2]]], dtype=object) 

In [394]: np.asarray([([1],[2])],dtype=object) 
Out[394]: 
array([[[1], 
     [2]]], dtype=object) 

In [395]: _.shape 
Out[395]: (1, 2, 1) 

最後一種情況(你的問題的情況下)失敗,因爲結果是3D。

隨着2點稀疏矩陣(預期輸入):

In [402]: np.asarray([[a,a]], dtype=object) 
Out[402]: 
array([[ <1x1 sparse matrix of type '<class 'numpy.int32'>' 
    with 1 stored elements in COOrdinate format>, 
     <1x1 sparse matrix of type '<class 'numpy.int32'>' 
    with 1 stored elements in COOrdinate format>]], dtype=object) 

In [403]: _.shape 
Out[403]: (1, 2) 

hstack被趁着bmat格式的,通過轉動矩陣的列表轉換矩陣的嵌套(2d)的列表。 bmat旨在將稀疏矩陣的二維數組組合成一個更大的矩陣。跳過首先製作這些稀疏矩陣的步驟可能會或可能不會工作。代碼和文檔不作任何承諾。

3

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> 

有趣的是,如果你試圖做你的hstack,或numpy.hstack茂密的版本做了什麼,那麼它是完全可以接受的:

In [48]: import numpy as np 

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

....東西弄髒了稀疏矩陣表示¯\_(ツ)_/¯

相關問題