NumPy列更改值或添加

  • 本文关键字:添加 NumPy python numpy
  • 更新时间 :
  • 英文 :


我写了如下代码:

# weight shape will be as (108, 796)
Weight = np.zeros((xm_a*xm_b,data1_a))
# tt shape will be as (108, 1)
tt = np.reshape(AR[:,:,i].T,(AR_a*AR_b,1),order='F')
for i ...:
...
Weight[:,i] = tt

运行代码时,将出现与最后一行相关的以下错误:

ValueError:无法将输入数组从形状(108,1)广播到形状(108)

那么如何解决这个问题呢?我是python的新手。

根据我对问题的理解,我们可以创建一个示例数据,如:

Weight = np.array([[3, 4], [4, 2], [6, 3]])
tt = np.array([[7], [17], [177]])
现在,我们可以将Weight中的columnar替换为newtt,使用:
for i in range(2):
Weight[:, i] = tt[:, 0]

其中权重将根据tt:

更改如下

new Weight = np。Array ([[7,7], [17,17], [177, 177]])

tt在循环过程中发生变化时,例如:

tt = [np.array([[7], [17], [177]]), np.array([[5], [15], [155]])]

可以用tt列表中的每个子tt替换Weight中的columnar,使用:

for i, j in enumerate(tt):
Weight[:, i] = j[:, 0]

将导致新的重量为:

new Weight = np。Array ([[7,5], [17,15], [177, 155]])