Python: performing operations on numpy arrays
Its very easy to do math operations in numpy arrays
Its very easy to do math operations in numpy arrays
Add a number to each element of an array
In this case we added “1” to each element of the array>>> import numpy as np
>>> array1 = np.array([1,2,3])
>>> print(array1 + 1)
[2 3 4]
Doing operations between arrays
In this case we added each element of the arraysarray1 = np.array([1,2,3])
array2 = np.array([1,2,3])
print(array1+array2)
[2 4 6]
Note: math operations need to be performed in arrays that have the same shape, the following will throw an errorarray1 = np.array([1,2])
array2 = np.array([1,2,3])
print(array1+array2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: operands could not be broadcast together with shapes (2,) (3,)