

- 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
Return the Lower triangle of an array in Numpy
To return the lower triangle of an array, use the numpy.tril() method in Python Numpy. The 1st parameter is the input array. The function returns a copy of an array with elements above the k-th diagonal zeroed. For arrays with ndim exceeding 2, tril will apply to the final two axes.
The k is 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.
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]])
Display the array −
print("Array...\n",arr)
Get the datatype −
print("\nArray datatype...\n",arr.dtype)
Get the dimensions of the Array −
print("\nArray Dimensions...\n",arr.ndim)
Get the shape of the array −
print("\nOur Array Shape...\n",arr.shape)
Get the number of elements of the Array −
print("\nElements in the Array...\n",arr.size)
To return the lower triangle of an array, use the numpy.tril() method. The 1st parameter is the input array −
print("\nResult...\n",np.tril(arr))
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...\n",arr) # Get the datatype print("\nArray datatype...\n",arr.dtype) # Get the dimensions of the Array print("\nArray Dimensions...\n",arr.ndim) # Get the shape of the Array print("\nOur Array Shape...\n",arr.shape) # Get the number of elements of the Array print("\nElements in the Array...\n",arr.size) # To return the lower triangle of an array, use the numpy.tril() method in Python Numpy # The 1st parameter is the input array print("\nResult...\n",np.tril(arr))
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... [[36 0 0 0] [92 81 0 0] [22 67 54 0] [69 80 80 99]]
- Related Questions & Answers
- Return the Upper triangle of an array in Numpy
- Return the Lower triangle of an array and zero the main diagonal as well in Numpy
- Return the Lower triangle of an array and zero elements just below the main diagonal in Numpy
- Return the Lower triangle of an array and set the diagonal above which to zero elements in Numpy
- Return the Upper triangle of an array and zero the main diagonal as well 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 elements just above the main diagonal in Numpy
- Sum of upper triangle and lower triangle in C++
- Return the cube-root of an array elementwise in Numpy
- Return the identity array in Numpy
- Return the transpose of the masked array in NumPy
- Return the floor of the array elements in Numpy
- Return the length of the masked array in Numpy
- Return a 2-D array with ones on the lower diagonal and zeros elsewhere in Numpy
- Return a copy of the masked array in NumPy
Advertisements