我正在嘗試執(zhí)行以下操作:我有一個(gè)形狀為 (Z, M, N) 的三維數(shù)組 X。我有一個(gè)單獨(dú)的一維數(shù)組 Y,形狀為 (M),其中包含整數(shù)值,其中大于 0 的值表示 X 中我希望在 Y 中設(shè)置相應(yīng)值的行。例如,請(qǐng)參閱以下場(chǎng)景:X =[[[0. 0.] [0. 0.]] [[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]]]Y =[0 1]應(yīng)該返回:[[[0. 0.] [1. 1.]] [[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]]]或者什么時(shí)候Y = [0 0],應(yīng)該返回:[[[0. 0.] [0. 0.]] [[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]] [[1. 1.] [1. 1.]]]以下代碼適用于第一種情況,但在第二種情況下會(huì)引發(fā)以下錯(cuò)誤,這似乎是我無(wú)法解決的。我在 Windows 10 Pro 上使用 Python 3.6.8 運(yùn)行它。Traceback (most recent call last): File "blah2.py", line 41, in <module> r[np.where(y>0), :] = y[np.where(y>0)]ValueError: shape mismatch: value array of shape (0,) could not be broadcast to indexing result of shape (1,0,2)import numpy as npx = np.ones((5, 2, 2))y = np.array([0, 0])r = np.zeros((2,2))r[np.where(y>0), :] = y[np.where(y>0)]x[0] = rprint(x)
嘗試用特定值填充整個(gè)行/列時(shí)出現(xiàn)形狀不匹配值錯(cuò)誤
翻翻過(guò)去那場(chǎng)雪
2021-12-21 16:03:41