- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add arguments element-wise with different shapes in Numpy
To add arguments element-wise with different shapes, use the numpy.add() method in Python Numpy. The out is 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. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.
The condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default out=None, locations within it where the condition is False will remain uninitialized.
NumPy offers comprehensive mathematical functions, random number generators, linear algebra routines, Fourier transforms, and more. It supports a wide range of hardware and computing platforms, and plays well with distributed, GPU, and sparse array libraries.
Steps
At first, import the required library −
import numpy as np
Create two arrays with different shapes −
arr1 = np.arange(27.0).reshape((3, 3, 3)) arr2 = np.arange(9.0).reshape((3, 3))
Display the arrays −
print("Array 1...
", arr1) print("
Array 2...
", arr2)
Get the type of the arrays −
print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype)
Get the dimensions of the Arrays −
print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim)
Get the shape of the Arrays −
print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape)
To add arguments element-wise with different shapes, use the numpy.add() method in Python Numpy −
print("
Result (adding element-wise)...
",np.multiply(arr1, arr2))
Example
import numpy as np # Create two arrays with different shapes arr1 = np.arange(27.0).reshape((3, 3, 3)) arr2 = np.arange(9.0).reshape((3, 3)) # Display the arrays print("Array 1...
", arr1) print("
Array 2...
", arr2) # Get the type of the arrays print("
Our Array 1 type...
", arr1.dtype) print("
Our Array 2 type...
", arr2.dtype) # Get the dimensions of the Arrays print("
Our Array 1 Dimensions...
",arr1.ndim) print("
Our Array 2 Dimensions...
",arr2.ndim) # Get the shape of the Arrays print("
Our Array 1 Shape...
",arr1.shape) print("
Our Array 2 Shape...
",arr2.shape) # To add arguments element-wise with different shapes, use the numpy.add() method in Python Numpy print("
Result (adding element-wise)...
",np.multiply(arr1, arr2))
Output
Array 1... [[[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.]] [[ 9. 10. 11.] [12. 13. 14.] [15. 16. 17.]] [[18. 19. 20.] [21. 22. 23.] [24. 25. 26.]]] Array 2... [[0. 1. 2.] [3. 4. 5.] [6. 7. 8.]] Our Array 1 type... float64 Our Array 2 type... float64 Our Array 1 Dimensions... 3 Our Array 2 Dimensions... 2 Our Array 1 Shape... (3, 3, 3) Our Array 2 Shape... (3, 3) Result (adding element-wise)... [[[ 0. 1. 4.] [ 9. 16. 25.] [ 36. 49. 64.]] [[ 0. 10. 22.] [ 36. 52. 70.] [ 90. 112. 136.]] [[ 0. 19. 40.] [ 63. 88. 115.] [144. 175. 208.]]]
- Related Articles
- Multiply arguments element-wise with different shapes in Numpy
- Subtract arguments element-wise with different shapes in Numpy
- Add arguments element-wise and display the result in a different type in Numpy
- Subtract arguments element-wise in Numpy
- True Divide arguments element-wise in Numpy
- Subtract arguments element-wise and display the result in a different type in Numpy
- Divide arguments element-wise and display the result in a different type in Numpy
- True Divide arguments element-wise and display the result in a different type in Numpy
- Test element-wise for NaN in Numpy
- Return the inner product of two masked arrays with different shapes in Numpy
- Return the outer product of two masked arrays with different shapes in Numpy
- Calculate the absolute value element-wise in Numpy
- Return element-wise string multiple concatenation in Numpy
- Return the element-wise remainder of division with modulo operation in Numpy
- Return the element-wise remainder of division with fmod() operation in Numpy
