Home > Mobile >  Updating value of numpy array
Updating value of numpy array

Time:02-16

weights = np.zeros(4) 
for row in trainData:
    iterRow= (row[:-1])             
    x=0           
for weight in weights:
    weight  = (y*iterRow[x])
    print("weight is", weight)
    x  =1
print("new weights are" , weights"
  • What I am trying to do is:
  1. create a numpy array that is just [0,0,0,0]
  2. For each row in my training dataset (approx 200), remove the last column
  3. then- for each weight value, add the corresponding value of row that we are currently on and multiply by y (defined earlier in code)

My issue is that the new weights aren't updating correctly, and when I print them they're still [0,0,0,0]. However on the line above where i print "weight is", the updated weight will display there, it just doesn't translate into my numpy array Can anyone help?

CodePudding user response:

That is not how for loops work in Python. for weight in weights creates a new variable called weight at every iteration. weight = (y*iterRow[x]) updates this variable, not the numpy array. Try this instead:

...
for i in range(len(weights)):
    weights[i]  = (y*iterRow[x])
    print("weight is", weights[i])
    x  =1
...
  • Related