2015-11-07 162 views
0

我有矩陣GxGy這兩種稀疏類型的coo。無法將稀疏矩陣轉換爲稠密矩陣

我執行以下操作他們:

A = np.hstack((Gx.transpose(),Gy.transpose())) 
B = np.vstack((Gx,Gy)) 

L = np.dot(A,B) 

我想以可視化的解決方案,C所以我使用C.toarray()和C.todense(),但得到的答覆是:

In [391]: C 
Out[391]: 
    array([ <16x16 sparse matrix of type '<type 'numpy.float64'>' 
with 64 stored elements in Compressed Sparse Row format>], dtype=object) 


In [392]: C.toarray() 
Traceback (most recent call last): 
    File "<ipython-input-390-86c90f8dce51>", line 1, in <module> 
    C.toarray() 
AttributeError: 'numpy.ndarray' object has no attribute 'toarray' 

我怎麼能看到矩陣C密集形式?

回答

3

來自:

array([ <16x16 sparse matrix of type '<type 'numpy.float64'>' 

在壓縮稀疏行格式64種存儲的元素>],D型細胞=對象)

我推斷C是1個元件密集陣列與dtype=object。那一個元素是一個稀疏矩陣。

因此,我希望

C[0].toarray() 

會工作。如錯誤所述,numpy陣列沒有toarray方法。但在這種情況下,它的元素呢。

由於GxGy稀疏,那麼你需要使用的hstackvstack,而不是numpy版本的稀疏版本。檢查AB的類型。我是那些numpy數組,而不是稀疏矩陣。


看,當我使用np.hstack與一對夫婦稀疏矩陣發生了什麼:

In [70]: M=sparse.csr_matrix([[0,1,2],[2,3,4]]) 
In [71]: np.hstack([M,M]) 
/usr/lib/python3/dist-packages/scipy/sparse/compressed.py:298: SparseEfficiencyWarning: Comparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead. 
    "using <, >, or !=, instead.", SparseEfficiencyWarning) 
Out[71]: 
array([ <2x3 sparse matrix of type '<class 'numpy.int32'>' 
    with 5 stored elements in Compressed Sparse Row format>, 
     <2x3 sparse matrix of type '<class 'numpy.int32'>' 
    with 5 stored elements in Compressed Sparse Row format>], dtype=object) 

結果不疏,而是密集的2組稀疏的元素。