Sou novo utilizando Python e não sei porque está dando o erro:
Traceback (most recent call last): File "C:\Users\Gabriel\Desktop\Perceptron.py", line 51, in print(perceptron(max_it, errors, alpha, inputs, outputs)) File "C:\Users\Gabriel\Desktop\Perceptron.py", line 45, in perceptron outputs.append(calculatePotential(inputs[i], weights[i], bias)) File "C:\Users\Gabriel\Desktop\Perceptron.py", line 15, in calculatePotential u = inputs[0] * weights[0] + inputs[1] * weights[1] + bias TypeError: 'int' object is not subscriptable
Código:
# Perceptron em Python
# Gabriel Augusto - 14-11-2017
#Variables
max_it = 3 #epoque
errors = [1, 0, 0, 0]
inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]
labels = [-1, -1, -1, 1]
outputs = []
alpha = 0.1
#activation potential
def calculatePotential(inputs, weights, bias):
u = inputs[0] * weights[0] + inputs[1] * weights[1] + bias
if (u <= 0):
return -1
else:
return 1
def startWeights(max_it):
weights = []
for x in range(0, max_it):
weights.append(0)
return weights
def sumErrors(errors):
sumErrors = 0
for i,j in enumerate(errors):
sumErrors += j
return sumErrors
#function Perceptron
def perceptron(max_it, erros, alpha, inputs, outputs):
#start weights
weights = startWeights(max_it)
bias = 0
t = 1
#while (t < max_it and sumErrors(errors) > 0):
for i in range(4):
outputs.append(calculatePotential(inputs[i], weights[i], bias))
return outputs
print(perceptron(max_it, errors, alpha, inputs, outputs))
weights[i]
dentro da funçãoperceptron
para a funçãocalculatePotencial
, eweights[i]
é um número, não uma lista