在pycharm中编码时出现此错误:TypeError:'int'对象不可下标 而且不知道如何摆脱它。 这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
def AF(x):
if x >= 0:
return 1
else:
return -1
Q1 = 3
Q2 = 1
Xi = ([[-1, -1], [0, 1], [1, -1], [0, 0]])
T = np.concatenate((np.ones((1, Q1)), -1 * np.ones((1, Q2))), axis=1)
T = np.squeeze(T)
W1 = np.zeros(2)
W2 = np.ones(2)
b1 = 0
b2 = 0
N = Q1 + Q2
k = 0
for i in range(4):
net1=np.matmul(W1,Xi[i])+b1
y1=AF(net1)
net2=np.matmul(W2,y1[i])+b2
if AF(net2)!= T[i]:
if T[i]==1:
J=np.argmax(net1)
b[J] = b[J]+ (1-net1[J])
W[i,J] = W[i,J]+Matmul((1-net1[J]), Xi[i,J])
elif T[i]==-1:
for K in range(3):
b[K] = b[K] + (-1 - net2[k])
W[i, K] = W[i, K]+Matmul((-1-net2[K]), Xi[i, K])
#xx1 = np.arange(-3, 3)
#xx2 = (-b - W[0] * xx1) / W[1]
#print('Weight=', W, 'Bias=', b)
plt.plot(Xi[T == -1, 0], Xi[T == -1, 1], 'go')
plt.plot(Xi[T == 1, 0], Xi[T == 1, 1], 'r^')
plt.plot(xx1, xx2, 'g')
我得到这个错误:
TypeError: 'int' object is not subscriptable
请帮助我解决此错误!
答案 0 :(得分:1)
在行
net2 = np.matmul(W2, y1[i]) + b2
您尝试下标y1
的下标y1 = AF(net1)
,该结果总是返回int
。