我有兩個 (2000, 10) 矩陣:weight_values包含一組值并weight_indexes包含一組用作新矩陣索引的整數。
我想使用weight_indexes從新的 (2000, 2000) 零矩陣中選擇條目,然后將這些列設定為在值矩陣中找到的值。
例如,這樣做可以讓我得到我想要的:
weights = np.zeros((2000, 2000))
for i in range(weight_indexes.shape[0]):
weights[i, weight_indexes[i]] = weight_values[i]
但是,當我嘗試使用陣列索引執行此操作時,它不起作用。weights使用weight_indexes這樣的索引:
weights[:, weight_indexes[:]]
...而不是從 中選擇適當的列weights,這會創建一個新的 (2000, 2000, 10) 大小的矩陣。
有沒有一些矢量化的方法可以在不使用回圈的情況下做到這一點?
uj5u.com熱心網友回復:
Python 花式索引一起廣播。如果你想設定的每行10個元素weights,使i廣播的形狀相同weight_indexes:
weights[np.arange(len(weight_indexes))[:, None], weight_indexes] = weight_values
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/345529.html
上一篇:C++實作一個SOAP客戶端
下一篇:從參考表生成動態矩陣
