![]() |
VOOZH | about |
NumPy, the Python powerhouse for scientific computing, provides an array of tools to efficiently manipulate and analyze data. Among its key functionalities lies numpy.add() a potent function that performs element-wise addition on NumPy arrays.
Syntax : numpy.add(arr1, arr2, /, out=None, *, where=True, casting=’same_kind’, order=’K’, dtype=None, subok=True[, signature, extobj], ufunc ‘add’)
Parameters :
- arr1 : [array_like or scalar] Input array.
- arr2 : [array_like or scalar] Input array.out : [ndarray, optional] A location into which the result is stored. -> If provided, it must have a shape that the inputs broadcast to. -> If not provided or None, a freshly-allocated array is returned.
- where : [array_like, optional] Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone.
- **kwargs :Allows to pass keyword variable length of argument to a function. Used when we want to handle named argument in a function.
Return : [ndarray or scalar] The sum of arr1 and arr2, element-wise. Returns a scalar if both arr1 and arr2 are scalars.
NumPy's numpy.add() is a function that performs element-wise addition on NumPy arrays. This means it adds the corresponding elements between two arrays, element by element, instead of treating them as single values. numpy.add() function is used when we want to compute the addition of two arrays. It adds arguments element-wise. If the shape of two arrays is not the same, that is arr1. shape != arr2.shape, they must be broadcastable to a common shape (which may be the shape of one or the other).
Here are the different example of Add Elements in Numpy Array using numpy.add() with different example below:
In this example, we have two scalar values, num1 and num2. The np.add() function is used to add these two scalar values, and the result is printed. The function performs element-wise addition, and the output is the sum of the two scalars.
Output
1st Input number : 10 2nd Input number : 15 output number after addition : 25
add with One Array and One ScalarHere, we have a NumPy array array1 and a scalar value scalar. The np.add() function is applied to add the scalar to each element of the array. This demonstrates the broadcasting capability of NumPy, where the scalar is automatically broadcasted to match the shape of the array.
Output
Result of adding array and scalar: [13 11 16]NOTE: In-place addition: You can also use the += operator to perform in-place addition of two arrays or a scalar and an array. This modifies the first array instead of creating a new one.
The numpy.add() function is a part of the NumPy library in Python, and can be used to add two arrays element-wise. Here's In this example, we have two NumPy arrays, array1 and array2, of the same size. The np.add() function is applied to add the corresponding elements of the two arrays. The result is a new array with the sum of the corresponding elements.
Output
[5 7 9]Here, we have a 2D NumPy array (matrix) and a 1D NumPy array (vector). The np.add() function is used to add the vector to each row of the matrix, taking advantage of NumPy's broadcasting feature. The vector is automatically extended to match the size of the matrix, allowing the addition to be performed element-wise.
Output
Result of adding a vector to a matrix via broadcasting:
[[ 5 -1 14]
[-3 8 9]]