- 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
Reduce a multi-dimensional array along axis 1 in Numpy
To reduce a multi-dimensional array, use the np.ufunc.reduce() method in Python Numpy. Here, we have used add.reduce() to reduce it to the addition of elements. The axis is set using the "axis" parameter. Axis or axes along which a reduction is performed.
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 (or ufunc for short) is a function that operates on ndarrays in an element-byelement 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 multi-dimensional array −
arr = np.arange(27).reshape((3,3,3))
Display the array −
print("Array...
", arr)
Get the type of the array −
print("
Our Array type...
", arr.dtype)
Get the dimensions of the Array −
print("
Our Array Dimensions...
",arr.ndim)
To reduce a multi-dimensional array, use the np.ufunc.reduce() method in Python Numpy. Here, we have used multiply.reduce() to reduce it to the multiplication of elements. The axis is set using the "axis" parameter. Axis or axes along which a reduction is performed:
print("
Result (multiplication)...
",np.multiply.reduce(arr, axis = 1))
To reduce a multi-dimensional array, use the np.ufunc.reduce() method in Python Numpy. Here, we have used add.reduce() to reduce it to the addition of element. The axis is set using the "axis" parameter. Axis or axes along which a reduction is performed −
print("
Result (addition)...
",np.add.reduce(arr, axis = 1))
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 multi-dimensional array arr = np.arange(27).reshape((3,3,3)) # Display the array print("Array...
", arr) # Get the type of the array print("
Our Array type...
", arr.dtype) # Get the dimensions of the Array print("
Our Array Dimensions...
",arr.ndim) # To reduce a multi-dimensional array, use the np.ufunc.reduce() method in Python Numpy # Here, we have used multiply.reduce() to reduce it to the multiplication of elements elements # The axis is set using the "axis" parameter # Axis or axes along which a reduction is performed print("
Result (multiplication)...
",np.multiply.reduce(arr, axis = 1)) # To reduce a multi-dimensional array, use the np.ufunc.reduce() method in Python Numpy # Here, we have used add.reduce() to reduce it to the addition of elements # The axis is set using the "axis" parameter # Axis or axes along which a reduction is performed print("
Result (addition)...
",np.add.reduce(arr, axis = 1))
Output
Array... [[[ 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]]] Our Array type... int64 Our Array Dimensions... 3 Result (multiplication)... [[ 0 28 80] [ 1620 2080 2618] [ 9072 10450 11960]] Result (addition)... [[ 9 12 15] [36 39 42] [63 66 69]]
- Related Articles
- Reduce a multi-dimensional array along given axis in Numpy
- Reduce a multi-dimensional array along negative axis in Numpy
- Reduce a multi-dimensional array and add elements along negative axis in Numpy
- Reduce a multi-dimensional array and multiply elements along specific axis in Numpy
- Reduce a multi-dimensional array and multiply elements along axis 0 in Numpy
- Reduce a multi-dimensional array and add elements along specific axis in Numpy
- Reduce a multi-dimensional array and add elements along axis 0 in Numpy
- Apply accumulate for a multi-dimensional array along axis 1 in Numpy
- Reduce a multi-dimensional array in Numpy
- Apply accumulate for a multi-dimensional array along an axis in Numpy
- Apply accumulate for a multi-dimensional array along axis 0 in Numpy
- Reduce a multi-dimensional array and multiply elements in Numpy
- Reduce a multi-dimensional array and add elements in Numpy
- Repeat elements of a masked array along axis 1 in NumPy
- Sort the masked array in-place along axis 1 in NumPy
