- 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
Return the Upper triangle of an array and zero elements just above the main diagonal in Numpy
To return the upper triangle of an array, use the numpy.triu() method in Python Numpy −
- The 1st parameter is the input array
- The 2nd parameter is the 'k' i.e. the diagonal above which to zero elements.
Here, k = 0 (the default) is the main diagonal, k < 0 is below it and k > 0 is above.' Array. The k = 2 value is to zero elements just above the main diagonal.
The function returns a copy of an array with the elements below the k-th diagonal zeroed. For arrays with ndim exceeding 2, triu will apply to the final two axes.
Steps
At first, import the required library −
import numpy as np
Create a 2d array −
arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]])
Displaying our array −
print("Array...
",arr)
Get the datatype −
print("
Array datatype...
",arr.dtype)
Get the dimensions of the Array −
print("
Array Dimensions...
",arr.ndim)
Get the shape of the Array −
print("
Our Array Shape...
",arr.shape)
Get the number of elements of the Array −
print("
Elements in the Array...
",arr.size)
Return the upper triangle of an array using the numpy.triu() method in Python Numpy. The 2nd parameter is the 'k' i.e. the diagonal above which to zero elements. The k = 2 value is to zero elements just above the main diagonal −
print("
Result...
",np.triu(arr, k = 2))
Example
import numpy as np # Create a 2d array arr = np.array([[36, 36, 78, 88], [92, 81, 98, 45], [22, 67, 54, 69 ], [69, 80, 80, 99]]) # Displaying our array print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size) # To return the upper triangle of an array, use the numpy.triu() method in Python Numpy # The 1st parameter is the input array # The 2nd parameter is the 'k' i.e. the diagonal above which to zero elements. # k = 0 (the default) is the main diagonal, k < 0 is below it and k > 0 is above.' array # The k = 2 value is to zero elements just above the main diagonal print("
Result...
",np.triu(arr, k = 2))
Output
Array... [[36 36 78 88] [92 81 98 45] [22 67 54 69] [69 80 80 99]] Array datatype... int64 Array Dimensions... 2 Our Array Shape... (4, 4) Elements in the Array... 16 Result... [[ 0 0 78 88] [ 0 0 0 45] [ 0 0 0 0] [ 0 0 0 0]]
- Related Articles
- Return the Lower triangle of an array and zero elements just below the main diagonal in Numpy
- Return the Upper triangle of an array and set the diagonal above to zero elements in Numpy
- Return the Upper triangle of an array and zero the main diagonal as well in Numpy
- Return the Lower triangle of an array and set the diagonal above which to zero elements in Numpy
- Return the Lower triangle of an array and zero the main diagonal as well in Numpy
- Return the Upper triangle of an array in Numpy
- Create an array with zero above the main diagonal forming a lower triangular matrix in Numpy
- Create an array with ones above the main diagonal and zeros elsewhere in Numpy
- Return specified diagonals and set the offset of the diagonal from the main diagonal in Numpy
- Return a 2-D array with ones on the upper diagonal and zeros elsewhere in Numpy
- Return the Lower triangle of an array in Numpy
- Create an array with ones below the main diagonal and zeros elsewhere in Numpy
- Create a two-dimensional array with the flattened input as an upper diagonal in Numpy
- Return the floor of the array elements in Numpy
- Return the indices of unmasked elements that are not zero in NumPy
