- 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

# Create an array with ones above the main diagonal and zeros elsewhere in Numpy

To create an array with ones above the main diagonal and zeros elsewhere, use the **numpy.tri()** method in Python Numpy

The 1st parameter is the number of rows in the array

The 2nd parameter is the number of columns in the array

The 3rd parameter 'k' is the sub-diagonal at and below which the array is filled.

The k = 0 is the main diagonal, while k < 0 is below it, and k > 0 is above. The default is 0. The tri() function returns an array with its lower triangle filled with ones and zero elsewhere; in other words T[i,j] == 1 for j <= i + k, 0 otherwise.

## Steps

At first, import the required library −

import numpy as np

Now, create an array with ones above the main diagonal and zeros elsewhere, use the numpy.tri() method −

arr = np.tri(4, 4, k = 1)

Display the 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)

## Example

import numpy as np # To create an array with ones above the main diagonal and zeros elsewhere, use the numpy.tri() method in Python Numpy # The 1st parameter is the number of rows in the array # The 2nd parameter is the number of columns in the array # The 3rd parameter 'k' is the sub-diagonal at and below which the array is filled. # The k = 0 is the main diagonal, while k < 0 is below it, and k > 0 is above. The default is 0. arr = np.tri(4, 4, k = 1) # 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)

## Output

Array... [[1. 1. 0. 0.] [1. 1. 1. 0.] [1. 1. 1. 1.] [1. 1. 1. 1.]] Array datatype... float64 Array Dimensions... 2 Our Array Shape... (4, 4) Elements in the Array... 16

- Related Articles
- Create an array with ones below the main diagonal and zeros elsewhere in Numpy
- Create an array with ones at and below the given diagonal and zeros elsewhere in Numpy
- Create an array with ones at and below the given diagonal and zeros elsewhere with a different output type in Numpy
- Return a 2-D array with ones on the diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the upper diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the lower diagonal and zeros elsewhere in Numpy
- Return a 2-D array with ones on the diagonal and zeros elsewhere but set a different datatype in Numpy
- Return a 2-D array with ones on the diagonal and zeros elsewhere and also set the number of columns in Numpy
- Create an array with zero above the main diagonal forming a lower triangular matrix in Numpy
- Return the Upper triangle of an array and zero elements just above the main diagonal 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 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 Upper triangle of an array and set the diagonal above to zero elements in Numpy
- Create a two-dimensional array with the flattened input as an upper diagonal in Numpy