當我想要將矢量分配給矩陣中列的一部分時,它失敗。這裏是一個小例子:numpy無法將值分配給列的一部分
>>> a = np.random.randn(3,4)
>>> a
array([[ 0.12263942, -0.21972043, -0.97263544, 0.10609172],
[ 0.27012565, 1.0006549 , 0.80596588, -0.2954019 ],
[-0.28402912, -2.62405207, -0.52560166, 0.07325703]])
>>> x
array([-0.33707461, 0.66196137])
>>> a[np.array([0,2])][:,2] = x
>>> a[np.array([0,2])][:,2]
array([-0.97263544, -0.52560166])
但這個工程:
>>> a = np.random.randn(3,4)
>>> a
array([[ 0.45379867, 0.38782073, -0.01432557, -1.03457707],
[ 1.27650459, 0.4480737 , -1.31625142, 2.00037792],
[ 0.77420762, 1.20131941, -0.98937721, -0.04921255]])
>>> x2 = np.random.randn(3)
>>> x2
array([-1.82217731, -0.72454628, -0.41954297])
>>> a[:,2] = x2
>>> a[:,2]
array([-1.82217731, -0.72454628, -0.41954297])
爲什麼第一個例子不能工作?我錯過了什麼嗎?
聲音['relevant'(https://開頭stackoverflow.com/a/44262976/3293881)。 – Divakar