我试图用另一个矩阵的元素替换矩阵的二维元素。
让我分享一下代码,以便您更好地理解它:
import numpy as np
zeros = np.zeros([1,4,4,1])
#zeros.shape gives (1,4,4,1)
my_matrix = np.random.rand(4,4)
#my_matrix.shape gives (4,4)
for i in range(zeros.shape[0]):
for j in range(zeros.shape[3]):
zeros[i,:,:,j] = my_matrix
print(zeros)
>>>
array([[[[0.42637021],
[0.99836782],
[0.74446878],
[0.73064106]],
[[0.20887195],
[0.2531161 ],
[0.4798265 ],
[0.8722573 ]],
[[0.23242607],
[0.31000575],
[0.98238762],
[0.43288297]],
[[0.84283148],
[0.66117088],
[0.41227247],
[0.93539199]]]])
现在,代码运行良好。但是,我在想是否有更好的方法,使用矩阵并避免循环来做到这一点。
谢谢
答案 0 :(得分:1)
试试
with model:
...
samp = fit.sample(1500)
idata = az.from_pymc3(samp)
az.summary(idata.posterior[["b"]].sel({"b_dim_0": [0]}))
答案 1 :(得分:0)
如果您想保留 zeros
的原始形状,请尝试:
zeros[0, :, :, 0] = my_matrix