Matthew
Matthew

Reputation: 502

Numpy addition to an element of an array

I am coding a neural network in python, and need to adjust my weights. In order to do so, I need to add my change variable to an element of my weights array. However, I don't know how to do this. The code would look like:

weights = numpy.array([1, 2, 3])
change = 1
weights[0]+= change
print(weights)
-- [2, 2, 3]

I have tried this, but it does not seem to work. Thanks in advance for any answers.

Upvotes: 0

Views: 76

Answers (1)

sam-pyt
sam-pyt

Reputation: 1047

If you're trying to add your variable 'change' to just the first element of the weights array, then your code works fine. if you are trying to add 'change' to all elements of the weights array, simply put

weights=numpy.array([1,2,3])
change=1
weights+=change
print(weights)

this code will add the change to all the elements. I'm assuming that this is what you're trying to do because that would make the most sense in the context of a neural network. if this is not your problem, be more specific on what you are trying to do.

Upvotes: 1

Related Questions