Return a 2-D array with ones on the upper diagonal and zeros elsewhere in Numpy

NumpyServer Side ProgrammingProgramming

The numpy.eye() returns a 2-D array with 1’s as the diagonal and 0’s elsewhere. Here, the 1st parameter means the "Number of rows in the output" i.e. 4 means 4x4 array. The 2nd parameter is the number of columns in the output. If None, defaults to the 1st parameter i.e. 4x4 here. The 3rd parameter i.e. K is the Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. We have set the upper diagonal with a positive value K.

The function eye() returns an array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one. The dtype is the data-type of the returned array. The order suggests whether the output should be stored in row-major (C-style) or column-major (Fortran-style) order in memory.

The like parameter is a reference object to allow the creation of arrays which are not NumPy arrays. If an array-like passed in as like supports the __array_function__ protocol, the result will be defined by it. In this case, it ensures the creation of an array object compatible with that passed in via this argument.

Steps

At first, import the required library −

import numpy as np

Create a 2d array. The numpy.eye() returns a 2-D array with 1’s as the diagonal and 0’s elsewhere. The 3rd parameter i.e. K is the Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. We have set the upper diagonal with a positive value K −

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

Display the array −

print("Array...\n", arr)

Get the type of the array −

print("\nArray type...\n", arr.dtype)

Get the shape of the array −

print("\nArray shape...\n", arr.shape)

Get the dimensions of the Array −

print("\nArray Dimensions...\n",arr.ndim)

Get the number of elements in the Array −

print("\nArray (count of elements)...\n",arr.size)

Example

import numpy as np

# Create a 2d array
# The numpy.eye() returns a 2-D array with 1’s as the diagonal and 0’s elsewhere.
# Here, the 1st parameter means the "Number of rows in the output" i.e. 4 means 4x4 array
# The 2nd parameter is the number of columns in the output. If None, defaults to the 1st parameter i.e. 4x4 here.
# The 3rd parameter i.e. K is the Index of the diagonal: 0 (the default) refers to the main diagonal,
# a positive value refers to an upper diagonal,
# and a negative value to a lower diagonal.
# We have set the upper diagonal with a positive value K
arr = np.eye(4, k = 1)

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nArray type...\n", arr.dtype)

# Get the shape of the array
print("\nArray shape...\n", arr.shape)

# Get the dimensions of the Array
print("\nArray Dimensions...\n",arr.ndim)

# Get the number of elements of the Array
print("\nArray (count of elements)...\n",arr.size)

Output

Array...
[[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]
[0. 0. 0. 0.]]

Array type...
float64

Array shape...
(4, 4)

Array Dimensions...
2

Array (count of elements)...
16
raja
Updated on 10-Feb-2022 06:46:48

Advertisements