How do you perform element-wise addition of two NumPy arrays?

How do you perform element-wise addition of two NumPy arrays?

a) By using the + operator
b) By using the add() function
c) By using a loop to add corresponding elements
d) By using the concatenate() function

Answer:

a) By using the + operator

Explanation:

You can perform element-wise addition of two NumPy arrays using the + operator. This operation adds corresponding elements from both arrays and returns a new array with the results.

import numpy as np

# Creating two NumPy arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

# Element-wise addition
result = arr1 + arr2
print(result)  # Output: [5 7 9]

In this example, the corresponding elements of arr1 and arr2 are added together to produce the result array [5, 7, 9].

Element-wise operations are a key feature of NumPy, enabling efficient and concise mathematical operations on arrays without the need for explicit loops.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top