Python numpy trace() method is used to find the sum of diagonals of the array. If we are having a 2-D array like ‘arr’ =

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

The trace() method returns the sum along diagonals of the array. If the array is 2D, the sum along its diagonal with a given offset is returned, i.e., the sum of items a[i,i+offset] for all i.

If an array has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2D sub-arrays whose traces are returned. The shape of a resulting array is the same as that of *a* with **axis1** and **axis2** removed.

#python #numpy

12.25 GEEK