簡體   English   中英

如何將元組對列表列表(索引,值)轉換為二維 numpy 數組

[英]How to convert List of Lists of Tuples- pairs (index,value) into 2D numpy array

有元組列表列表:

[[(0, 0.5), (1, 0.6)], [(4, 0.01), (5, 0.005), (6, 0.002)], [(1,0.7)]]

我需要得到矩陣 X x Y:

x = num of sublists
y = max among second eleme throught all pairs
elem[x,y] = second elem for x sublist if first elem==Y 
0 1 2 3 4 5 6
0.5 0.6 0 0 0 0 0
0 0 0 0 0.01 0.005 0.002
0 0.7 0 0 0 0 0

您可以通過以下方式計算數組的尺寸。 Y維度是子列表的數量

>>> data = [[(0, 0.5), (1, 0.6)], [(4, 0.01), (5, 0.005), (6, 0.002)], [(1,0.7)]]
>>> dim_y = len(data)
>>> dim_y
3

X 維度是所有元組中最大的[0]索引加 1。

>>> dim_x = max(max(i for i,j in sub) for sub in data) + 1
>>> dim_x
7

所以然后用這個大小初始化一個全零的數組

>>> import numpy as np
>>> arr = np.zeros((dim_x, dim_y))
>>> arr
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

現在填充它, enumerate您的子列表以跟蹤 y 索引。 然后對於每個子列表,使用[0]作為 x 索引,使用[1]作為值本身

for y, sub in enumerate(data):
    for x, value in sub:
        arr[x,y] = value

然后應該填充生成的數組(可能想要轉置以看起來像您想要的尺寸)。

>>> arr.T
array([[0.5  , 0.6  , 0.   , 0.   , 0.   , 0.   , 0.   ],
       [0.   , 0.   , 0.   , 0.   , 0.01 , 0.005, 0.002],
       [0.   , 0.7  , 0.   , 0.   , 0.   , 0.   , 0.   ]])

正如我在接受的答案中評論的那樣, data是“參差不齊的”,不能制成數組。

現在,如果數據具有更規則的形式,則可以使用無循環解決方案。 但是轉換成這樣的形式需要同樣的雙循環!

In [814]: [(i,j,v) for i,row in enumerate(data) for j,v in row]
Out[814]: 
[(0, 0, 0.5),
 (0, 1, 0.6),
 (1, 4, 0.01),
 (1, 5, 0.005),
 (1, 6, 0.002),
 (2, 1, 0.7)]

'轉置'並分成3個變量:

In [815]: I,J,V=zip(*_)
In [816]: I,J,V
Out[816]: ((0, 0, 1, 1, 1, 2), (0, 1, 4, 5, 6, 1), (0.5, 0.6, 0.01, 0.005, 0.002, 0.7))

我堅持使用列表轉置,以免將 integer 索引轉換為浮點數。 它也可能更快,因為從列表中創建數組並不是一項耗時的任務。

現在我們可以通過numpy魔法賦值:

In [819]: arr = np.zeros((3,7))
In [820]: arr[I,J]=V
In [821]: arr
Out[821]: 
array([[0.5  , 0.6  , 0.   , 0.   , 0.   , 0.   , 0.   ],
       [0.   , 0.   , 0.   , 0.   , 0.01 , 0.005, 0.002],
       [0.   , 0.7  , 0.   , 0.   , 0.   , 0.   , 0.   ]])

I,J,V也可以用作scipy.sparse.coo_matrix調用的輸入,形成一個稀疏矩陣。

說到稀疏矩陣,下面是arr的稀疏版本:

在列表格式中:

In [822]: from scipy import sparse
In [823]: M = sparse.lil_matrix(arr)
In [824]: M
Out[824]: 
<3x7 sparse matrix of type '<class 'numpy.float64'>'
    with 6 stored elements in List of Lists format>
In [825]: M.A
Out[825]: 
array([[0.5  , 0.6  , 0.   , 0.   , 0.   , 0.   , 0.   ],
       [0.   , 0.   , 0.   , 0.   , 0.01 , 0.005, 0.002],
       [0.   , 0.7  , 0.   , 0.   , 0.   , 0.   , 0.   ]])
In [826]: M.rows
Out[826]: array([list([0, 1]), list([4, 5, 6]), list([1])], dtype=object)
In [827]: M.data
Out[827]: 
array([list([0.5, 0.6]), list([0.01, 0.005, 0.002]), list([0.7])],
      dtype=object)

和更常見的coo格式:

In [828]: Mc=M.tocoo()
In [829]: Mc.row
Out[829]: array([0, 0, 1, 1, 1, 2], dtype=int32)
In [830]: Mc.col
Out[830]: array([0, 1, 4, 5, 6, 1], dtype=int32)
In [831]: Mc.data
Out[831]: array([0.5  , 0.6  , 0.01 , 0.005, 0.002, 0.7  ])

以及用於大多數計算的 csr:

In [832]: Mr=M.tocsr()
In [833]: Mr.data
Out[833]: array([0.5  , 0.6  , 0.01 , 0.005, 0.002, 0.7  ])
In [834]: Mr.indices
Out[834]: array([0, 1, 4, 5, 6, 1], dtype=int32)
In [835]: Mr.indptr
Out[835]: array([0, 2, 5, 6], dtype=int32)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM