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 upper triangular matrix from a 2d array in Numpy.

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

You can use the numpy built-in `numpy.triu()`

function to get the upper triangular matrix from a 2d Numpy array. Pass the array as an argument to the function.

The following is the syntax –

numpy.triu(m, k)

The `numpy.tril()`

function takes the following parameters –

`m`

– The input array for which you want to get the upper triangular matrix. For arrays with dimensions greater than 2, the function will apply to the final two axes.`k`

– The diagonal below 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 upper triangular matrix of the passed array) with elements below the specified diagonal as 0.

## Examples

Let’s now look at examples of using the above syntax to get the upper 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:

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

[[ 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 upper triangular matrix with the default diagonal

Let’s now use the `numpy.triu()`

function to get the upper triangular matrix for the 2d array created above. We will use the default diagonal (`k = 0`

).

# get the upper triangular matrix utm_arr = np.triu(arr) # display the matrix print(utm_arr)

Output:

[[1 2 3] [0 5 6] [0 0 9] [0 0 0]]

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

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

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

The `numpy.triu()`

function comes with an optional parameter, `k`

that you can use to specify the diagonal you want to use for computing the upper 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 upper triangular matrix.

# get the upper triangular matrix utm_arr = np.triu(arr, k=-1) # display the matrix print(utm_arr)

Output:

[[ 1 2 3] [ 4 5 6] [ 0 8 9] [ 0 0 12]]

The resulting upper triangular matrix has values below the diagonal, `k = -1`

as zeros.

## Summary

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

- Use the
`numpy.triu()`

function to get the upper triangular matrix of an array. Pass the array as an argument. - You can specify the diagonal below 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 –

- Numpy – Set All Non Zero Values to Zero
- Get the First N Rows of a 2D Numpy Array
- Numpy – Remove Duplicates From Array

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**