2017-12-18 241 views
0

我有一個2D numpy的陣列,其看起來像這樣,創建從2D numpy的陣列的COO矩陣

[[3, 4, 5, 6], [4, 5, 6, 7], [9, 10, 3, 5]] 

我使用以下代碼轉換成COO矩陣這樣的:

# Flatten 2D array 
data = np.asarray(twod_array).flatten() 
row = np.arange(0, len(data)) 
col = np.arange(0, len(row)) 
# Make COO matrix 
mat = coo_matrix((data, (row, col)), shape=(len(row), len(row))) 

是這是將2D numpy數組轉換爲COO矩陣的正確方法?

編輯

我所試圖做的就是這一點,我有一個coloumn和項目的其他部分。

parts         item 
processor, display, sensor   temp. monitoring system 
fan baldes, motor, sensor    motion detecting fan 
     .          . 
     .          . 

我已轉換的數據對上面的數字,使得它們可以被進一步處理。

parts   items 
1, 2, 3  1 
4, 5, 3  2 

所以現在我想把上面的數據輸入到LightFM中,所以我創建了一個這樣的2D數組。

[[1, 2, 3, 1], [4, 5, 3, 2]] 

但由於LightFM的擬合方法只需要在形狀np.float32 coo_matrix [n_users,n_items]其是含有用戶 - 項目交互的矩陣。我使用上述方法轉換2D陣列。

+0

你爲什麼覺得這是不正確的?這是否會導致錯誤? –

+0

@cᴏʟᴅsᴘᴇᴇᴅ沒有錯誤。我正在使用它來訓練LightFM模型,並且模型生成的建議非常奇怪。 –

+0

您可以使用'mat.A'來檢查。你的預期產出是多少? – Akavall

回答

1
In [301]: A = np.array([[3, 4, 5, 6], [4, 5, 6, 7], [9, 10, 3, 5]]) 
In [302]: A 
Out[302]: 
array([[ 3, 4, 5, 6], 
     [ 4, 5, 6, 7], 
     [ 9, 10, 3, 5]]) 

你創建一個矩陣的方式:

In [305]: data =A.flatten() 
In [306]: M = sparse.coo_matrix((data,(np.arange(len(data)),np.arange(len(data)) 
    ...:))) 
In [307]: M 
Out[307]: 
<12x12 sparse matrix of type '<class 'numpy.int32'>' 
    with 12 stored elements in COOrdinate format> 

print(M)會顯示這些12個值與他們的座標。

如果它不是太大,我喜歡將矩陣顯示爲數組。 M.A是短切的M.toarray()

In [308]: M.A 
Out[308]: 
array([[ 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [ 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [ 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
     [ 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0], 
     [ 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0], 
     [ 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0], 
     [ 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0], 
     [ 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0], 
     [ 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0], 
     [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0], 
     [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0], 
     [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5]]) 

看對角 - 這是原始數組的12個值。那是你要的嗎? A的原始3x4佈局完全丟失。它可能是這12個數字的1d列表。

或者你可以只傳遞數組到稀疏的構造,生產原

In [309]: M1 = sparse.coo_matrix(A) 
In [310]: M1 
Out[310]: 
<3x4 sparse matrix of type '<class 'numpy.int32'>' 
    with 12 stored elements in COOrdinate format> 
In [311]: M1.A 
Out[311]: 
array([[ 3, 4, 5, 6], 
     [ 4, 5, 6, 7], 
     [ 9, 10, 3, 5]]) 

取而代之的是12×12對角線的稀疏副本,這是沒有任何0的一個3x4的陣列。這更有意義,如果A已經有很多0。

你真的知道你需要什麼樣的稀疏矩陣嗎?

+0

我試過這個,但我得到了'ValueError(「並非所有的估計參數都是有限的,」 ValueError:並非所有的估計參數都是有限的,你的模型可能已經發散。學習率或規範化特徵值和樣本權重「。 –

+0

請參閱編輯 –