好it's晚了,我不能再解決最簡單的問題:替換「零柱」與值從numpy的陣列
我有「零列」的矩陣,這些列應改爲
從另一陣列(同一列的索引),具有相同的列數的值:
a=np.array([[2,0,0,0],[1,0,2,0],[1,0,5,0]])
b=np.array([0.3,0.4,0.6,0.8])
結果應該是:
c=np.array([[2,0.4,0,0.8],[1,0.4,2,0.8],[1,0.4,5,0.8]])
我想:
#searches for an entire zero-column indexes
wildcard_cols = np.nonzero(a.sum(axis=0) == 0)
我得到:
out: wildcard_cols=array([[1, 3]], dtype=int64)# that is right
的話,我想從這個輸出獲取列表遍歷列表中的項目
wildcard_cols=np.asarray(wildcard_cols)
wildcard_cols=wildcard_cols.tolist()
,但我得到列表中的列表(?) out = [[1,3]]
所以我不能做:
for item in wildcard_cols:
a[:,item]=b[item]
#this does not work because i want to change every value in the column
我想也許複雜的,但也許有人找到一個快速的解決方案......
爲什麼你用'0.3'和'0.8'填充?如果它們對齊,它會不會是「0.4」和「0.8」? – DSM
你是對的,我編輯了問題 – Hiatus