NumPy add()

The add() function performs element-wise addition of two arrays.

Example

import numpy as np

# create two arrays
array1 = np.array([1, 2, 3])  
array2 = np.array([4, 5, 6])  

# perform element-wise addition of the two arrays result = np.add(array1, array2)
print(result) # Output: [5 7 9]

add() Syntax

The syntax of add() is:

numpy.add(x1, x2, out = None, where = True, dtype = None)

add() Arguments

The add() function takes following arguments:

  • x1 and x2 - two input arrays or scalars to be added
  • out (optional) - the output array where the result will be stored
  • where (optional) - a boolean array or condition specifying which elements to add
  • dtype (optional) - data type of the output array

add() Return Value

The add() function returns the array containing the sum of corresponding element(s) from two arrays — x1 and x2.


Example 1: Add NumPy Array by scalar (Single Value)

import numpy as np

# create an array
array1 = np.array([1, 2, 3])

# add a scalar value to the array result = np.add(array1, 10)
print(result)

Output

[11 12 13]

Here, the np.add() function is used to add a scalar value of 10 to each element of the array1 array.


Example 2: Use of out and where in add()

import numpy as np

# create two input arrays
array1 = np.array([1, 2, 3, 5])
array2 = np.array([10, 20, 30, 50])

# create a boolean array to specify the condition for element selection
condition = np.array([True, False, True, True])

# create an empty array to store the subtracted values
result = np.empty_like(array1)

# add elements in array1 and array2 based on values in the condition array and # store the sum in the result array np.add(array1, array2, where=condition, out=result)
print(result)

Output

[11  0 33 55]

The output shows the result of the addition operation, where the elements from array1 and array2 are added together only when the corresponding condition in the condition array is True.

The second element in result is 0 because the corresponding condition value is False, and therefore, the addition does not take place for that element.

Here, out=result specifies that the output of np.add() should be stored in the result array


Example 3: Use of dtype Argument in add()

import numpy as np

# create two arrays
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])

# perform addition with floating-point data type resultFloat = np.add(array1, array2, dtype=np.float64)
# perform addition with integer data type resultInt = np.add(array1, array2, dtype=np.int32)
# print the result with floating-point data type print("Floating-point result:") print(resultFloat) # print the result with integer data type print("Integer result:") print(resultInt)

Output

Floating-point result:
[5. 7. 9.]
Integer result:
[5 7 9]

Here, by specifying the desired dtype, we can control the data type of the output array according to our requirements.

Here, we have specified the data type of the output array with the dtype argument.

Note: To learn more about the dtype argument, please visit NumPy Data Types.