

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reduce array's dimension by one in Numpy
To reduce array’s dimension by one, use the np.ufunc.reduce() method in Python Numpy. Here, we have used multiply.reduce() to reduce it to the multiplication of all the elements.
The numpy.ufunc has functions that operate element by element on whole arrays. The ufuncs are written in C (for speed) and linked into Python with NumPy’s ufunc facility. A universal function (ufunc) is a function that operates on ndarrays in an element-by-element fashion, supporting array broadcasting, type casting, and several other standard features. That is, a ufunc is a “vectorized” wrapper for a function that takes a fixed number of specific inputs and produces a fixed number of specific outputs.
Steps
At first, import the required library −
import numpy as np
Create a 1D array −
arr = np.array([7, 14, 21, 28, 35])
Display the array −
print("Array...\n", arr)
Get the type of the array −
print("\nOur Array type...\n", arr.dtype)
Get the dimensions of the Array −
print("\nOur Array Dimensions...\n",arr.ndim)
To reduce array’s dimension by one, use the np.ufunc.reduce() method in Python Numpy. Here, we have used multiply.reduce() to reduce it to the multiplication of all the elements −
print("\nResult (multiplication)...\n",np.multiply.reduce(arr))
To reduce array’s dimension by one, use the np.ufunc.reduce() method in Python Numpy. Here, we have used add.reduce() to reduce it to the addition of all the elements −
print("\nResult (addition)...\n",np.add.reduce(arr))
Example
import numpy as np # The numpy.ufunc has functions that operate element by element on whole arrays. # ufuncs are written in C (for speed) and linked into Python with NumPy’s ufunc facility # Create a 1D array arr = np.array([7, 14, 21, 28, 35]) # Display the array print("Array...\n", arr) # Get the type of the array print("\nOur Array type...\n", arr.dtype) # Get the dimensions of the Array print("\nOur Array Dimensions...\n",arr.ndim) # To reduce array’s dimension by one, use the np.ufunc.reduce() method in Python Numpy # Here, we have used multiply.reduce() to reduce it to the multiplication of all the elements print("\nResult (multiplication)...\n",np.multiply.reduce(arr)) # To reduce array’s dimension by one, use the np.ufunc.reduce() method in Python Numpy # Here, we have used add.reduce() to reduce it to the addition of all the elements print("\nResult (addition)...\n",np.add.reduce(arr))
Output
Array... [ 7 14 21 28 35] Our Array type... int64 Our Array Dimensions... 1 Result (addition)... 105
- Related Questions & Answers
- Reduce array’s dimension by multiplying all the elements in Numpy
- Reduce array's dimension by adding all the elements in Numpy
- Reduce array's dimension by one but initialize the reduction with a different value in Numpy
- Reduce array's dimension by adding elements but initialize the reduction with a different value in Numpy
- Return a copy of the masked array collapsed into one dimension in Numpy
- Reduce a multi-dimensional array in Numpy
- Convert inputs to arrays with at least one dimension in Numpy
- Return a copy of the masked array collapsed into one dimension in column-major order in Numpy
- Return a copy of the masked array collapsed into one dimension in row-major order in Numpy
- Reduce a multi-dimensional array along given axis in Numpy
- Reduce a multi-dimensional array along axis 1 in Numpy
- Reduce a multi-dimensional array along negative axis in Numpy
- Reduce a multi-dimensional array and multiply elements in Numpy
- Reduce a multi-dimensional array and add elements in Numpy
- Return a copy of the masked array collapsed into one dimension in the order the elements occur in memory in Numpy