NumPy Trace | Matrix Explorer of the Python

Python has a powerful module named Numpy. At the same time, its specialization is in Data science, used in many ways. The in-built functions are versatile. So we provide tutorials for its various parts for you to practice. In this, we will see about numpy trace. The base of it’s working on matrix work, so a new field holds with python.

What numpy trace is ?

Numpy matrix.trace() method, we can find the sum of all the diagonal elements of a matrix by using the matrix.trace() method. This method returns the sum along diagonals of the array. The sum along with diagonal returns for a 2D array with a given offset using this method.

For a 2-D array =

0001
1011
A

And if we had to find the sum of all diagonal elements i.e.(A[00] + A[11]) then we had to use numpy.trace().

As per NumPy.org, when an array has more than two dimensions, we use axes specified by axis1 and axis2. These axes determine the 2-D sub-arrays, then the method finds their traces. The shape of the resulting array is the same as that of a with axis1 and axis2 removed.

Syntax of Numpy Trace

The syntax with all parameters is:

numpy.trace(arr, offset=0, axis1=0, axis2=1, dtype=None, out

But we usually use only:

numpy.trace(arr)
#or
numpy.matrix.trace()
#if array predeclared using numpy
array.trace()

Parameters Used in Numpy Trace

ParameterMandatory or notmatrix
amandatory
offsetoptional
axis1optional
axis2optional
dtypeoptional
outoptional

a : Array_like / matrix

‘a’ is the prime parameter. This input is all that is enough for evaluation—input array or object. The method tests elements of the array. This array is considered matrix and written in that form.

offset

Offset of the diagonal from the main diagonal. Can be both positive and negative. Defaults to 0.

axis1/axis2

 The first and second axis of the 2D sub-arrays is the axes providing the diagonals required. The default values are the first two axes of a. The data type is int

dtype

This parameter determines the data type of the returned array, and the accumulator sums the items. If the dtype has the value None and a is of integer precision less than the default integer precision, then use the default integer precision. Otherwise, the accuracy is the same as that of a.

out

The data type is array_like and optional to use. This parameter makes the output array with the same dimensions as the Input array, placed with the result. It has the same shape as the planned performance and maintains its form. Array sets the output. Parameter preserves its type, and it must be of the right shape to hold the result.

Return Value of numpy trace

Return the sum along diagonals of the array. In the form of ndarray as 

sum_along_diagonals. If a is 2-D, then we get the sum along the diagonal. If a has larger dimensions, then an array of sums along diagonals is returned.

Examples for Understanding

firstly we import numpy as:

import numpy as np

then either we create an array or directly use it with the trace method:

arr = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
arr
array([[1., 2., 3.],
       [4., 5., 6.],
       [7., 8., 9.]])
arr.trace()
15.0
np.trace(arr)
15.0

You may get another type of output if done in the following way:

arr = = np.matrix('[1,2,3; 4,5,6;7,8,9]') 
arr
matrix([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])
arr.trace()
[[15]]

another example:

a = np.arange(24).reshape((2,2,2,3))
a
array([[[[ 0,  1,  2],
         [ 3,  4,  5]],

        [[ 6,  7,  8],
         [ 9, 10, 11]]],


       [[[12, 13, 14],
         [15, 16, 17]],

        [[18, 19, 20],
         [21, 22, 23]]]])
np.trace(a)
array([[18, 20, 22],
       [24, 26, 28]])
np.trace(a).shape
(2, 3)

Now you would understand the trace method easily.

What’s Next?

NumPy is very powerful and incredibly essential for information science in Python. That being true, if you are interested in data science in Python, you really ought to find out more about Python.

You might like our following tutorials on numpy.

Conclusion

Numpy has an extensive range of applications. This method is fun to use and varies places to use. Matrix is not so attractive topic in math but of very use in ML. Trace optimizes the use.

Still have any doubts or questions do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments