# Numpy – Get the Lower Triangular Matrix (With Examples)

The Numpy library in Python comes with a number of useful functions to work with and manipulate the data in arrays. In this tutorial, we will look at how to get the lower triangular matrix from a 2d array in Numpy.

## How to get the lower triangular matrix in Numpy?

You can use the numpy built-in `numpy.tril()` function to get the lower triangular matrix from a 2d Numpy array. Pass the array as an argument to the function.

The following is the syntax –

`numpy.tril(m, k)`

The `numpy.tril()` function takes the following parameters –

1. `m` – The input array for which you want to get the lower triangular matrix. For arrays with dimensions greater than 2, the function will apply to the final two axes.
2. `k` – The diagonal above which to zero the elements. It is `0` (the main diagonal) by default. Diagonals below the main diagonal have `k < 0` and the ones above the main diagonal have `k > 0`.

It returns a numpy array (the lower triangular matrix of the passed array) with elements above the specified diagonal as 0.

Highlighted programs for you

Flatiron School

Flatiron School

Data Science Bootcamp
Product Design UX/UI Bootcamp

University of Maryland Global Campus

University of Maryland Global Campus

Cloud Computing Systems Master's
Digital Forensics & Cyber Investigation Master's

Creighton University

Creighton University

Health Informatics Master's

## Examples

Let’s now look at examples of using the above syntax to get the lower triangular matrix from a 2d array.

First, we will create a Numpy array that we will use throughout this tutorial.

```import numpy as np

# create a 2D numpy array
arr = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]
])
# display the matrix
print(arr)```

Output:

```[[ 1  2  3]
[ 4  5  6]
[ 7  8  9]
[10 11 12]]```

Here, we used the `numpy.array()` function to create a 2d array of shape 4×3 (having 4 rows and 3 columns).

### Example 1 – Get the lower triangular matrix with the default diagonal

Let’s now use the `numpy.tril()` function to get the lower triangular matrix for the 2d array created above. We will use the default diagonal (`k = 0`).

```# get the lower triangular matrix
ltm_arr = np.tril(arr)
# display the matrix
print(ltm_arr)```

Output:

```[[ 1  0  0]
[ 4  5  0]
[ 7  8  9]
[10 11 12]]```

We get the lower triangular matrix as a numpy array. You can see that the values above the main diagonal are zero in the returned matrix.

### Example 2 – Get the lower triangular matrix with a custom diagonal

In the above example, we used the main diagonal to compute our lower triangular matrix.

The `numpy.tril()` function comes with an optional parameter, `k` that you can use to specify the diagonal you want to use for computing the lower triangular matrix.

The below image better illustrates the different values of `k` (representing different diagonals) for our input array.

`k` is `0` by default. The diagonals below the main diagonal have `k < 0` and the diagonals above it have `k > 0`.

Let’s use `k = -1` to get the lower triangular matrix.

```# get the lower triangular matrix
ltm_arr = np.tril(arr, k=-1)
# display the matrix
print(ltm_arr)```

Output:

```[[ 0  0  0]
[ 4  0  0]
[ 7  8  0]
[10 11 12]]```

The resulting lower triangular matrix has values above the diagonal, `k = -1` as zeros.

## Summary

In this tutorial, we looked at how to get the lower triangular matrix of a 2d array in Numpy. The following are the key takeaways from this tutorial.

• Use the `numpy.tril()` function to get the lower triangular matrix of an array. Pass the array as an argument.
• You can specify the diagonal above which you want to keep the values zero using the optional parameter `k`. By default, it represents the main diagonal, `k = 0`.

You might also be interested in –

• 