0
I’m new using Python and I don’t know why the error is:
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
Code:
# 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))
Can you tell me which line is 51?
– Jéf Bueno
You passed
weights[i]
within the functionperceptron
for the functioncalculatePotencial
, andweights[i]
is a number, not a list– Jefferson Quesado
By the way, don’t you think it’s better to keep the value of weights in a class?
– Jefferson Quesado
About perceptron: https://answall.com/q/221004/64969
– Jefferson Quesado
@Gabrielaugusto, realized the mistake?
– Jefferson Quesado
@Jeffersonquesado thanks, I realized the mistake yes. About saving in a class, it would be interesting yes, for now I’m only doing some tests and a simpler algorithm, because it is still my first program in python as well. But thank you!
– Gabriel Augusto