[英]Creating a array of 1s and 0s using another array
所以我有兩個 arrays (行和列) row = [0, 1, 0, 2, 0, 1, 3, 1, 3, 1, 2, 4]
和column = [0, 0, 1, 1, 2, 2, 2, 3, 3, 0, 1, 4]
我想使用行和列 arrays 將值“1”插入另一個二維數組(向量)
vectors =
[[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.],
[0. 0. 0. 0. 0.]]
所以我想要的 output 是:
vectors =
[[1. 1. 1. 0. 0.],
[1. 0. 1. 1. 0.],
[0. 1. 0. 0. 0.],
[0. 0. 1. 1. 0.],
[0. 0. 0. 0. 1.]]
抱歉,如果我的解釋不好,這是我第一次使用 python 和 Stackoverflow。
我想你可以試試這個:
for r, c in zip(row, column):
vectors[r][c] = 1
只需使用row
和column
向量作為索引:
>>> vectors[row, column] = 1
>>> vectors
array([[1., 1., 1., 0., 0.],
[1., 0., 1., 1., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 1., 0.],
[0., 0., 0., 0., 1.]])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.