我有一個陣列:
a = np.array([[3701687786, 458299110, 2500872618, 3633119408],
[2377269574, 2599949379, 717229868, 137866584]], dtype=np.uint32)
如何將其轉換為一個形狀陣列,(2, 128)其中128表示每行中所有數字的二進制值(布爾 dtype)?
我可以通過使用串列理解來獲取位元組:
b''.join(struct.pack('I', x) for x in a[0])
但這并不完全正確。我怎樣才能使用 numpy 做到這一點?
uj5u.com熱心網友回復:
您可以使用以下功能按位and執行2:
vals = (a[...,None] & 2**np.arange(32, dtype='uint32')[[None,None,::-1])
out = (vals>0).astype(int).reshape(a.shape[0],-1)
# test
out[0,:32]
輸出:
array([1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1,
0, 1, 1, 1, 1, 0, 1, 0, 1, 0])
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/338745.html
標籤:麻木的
上一篇:取連續行直到特定值熊貓資料框
