我想使用8列的数组N和df1.shape [0]行,我想比较数组状态和antoher数组动作的值,并在适当的位置增加N的值[i,j]根据状态i的值和动作j的值,如下所示,但我得到了这个错误的任何建议:
ValueError Traceback (most recent call last)
<ipython-input-79-66d1d4ab2df8> in <module>()
84 elif(state[i]==4 and action[i]==0):
85 #access column 6 of N represents state1 for action 0
---> 86 N[i:6]= N[i-1:6]+1
87 else:
88 #access column 7 of N represents state1 for action 1
ValueError: could not broadcast input array from shape (2,8) into shape (1,8)
这是我的代码
N = np.zeros((df1.shape[0], 8))
#for i in range(1,df1.shape[0],1):
#N[df1['state'][i],df1['action'][i]]=N[state[i-1],action[i-1]]+1
for i in range(1,df1.shape[0],1):
if(state[i]==1 and action[i]==0):
#access column 0 of N represents state1 for action 0
N[i,0] = N[i-1,0]+1
elif(state[i]==1 and action[i]==1):
#access column 1 of N represents state1 for action 1
N[i,1] = N[i-1,1]+1
elif(state[i]==2 and action[i]==0):
#access column 2 of N represents state1 for action 0
N[i,2] = N[i-1,2]+1
elif(state[i]==2 and action[i]==1):
#access column 3 of N represents state1 for action 1
N[i,3] = N[i-1,3]+1
elif(state[i]==3 and action[i]==0):
#access column 4 of N represents state1 for action 0
N[i,4] = N[i-1,4]+1
elif(state[i]==3 and action[i]==1):
#access column 5 of N represents state1 for action 1
N[i,5]= N[i-1,5]+1
elif(state[i]==4 and action[i]==0):
#access column 6 of N represents state1 for action 0
N[i,6]= N[i-1,6]+1
else:
#access column 7 of N represents state1 for action 1
N[i,7]= N[i-1,7]+1