Python numpy trace() method is used to find the sum of diagonals of the array. If we have a 2-D array like “arr” =

00 | 01 | 02 |

10 | 11 | 12 |

20 | 21 | 22 |

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().

**np.trace**

The **np.trace()** is a built-in **numpy** library method that returns the sum along the 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, 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.

**Syntax**

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

**Parameters**

**arr:**Input_Array, whose diagonal sum we had to find**offset:**Offset of the diagonal from the main diagonal. Its value can be both positive and negative. The default is 0.**axis1,axis2:**Axes to be used as the first and second axis of the 2D sub-arrays from which the diagonals should be taken. The default values are the first two axes of*a*.**dtype****:**Determines the data type of the returned array and the accumulator where the items are summed. If the dtype has the value None and*a*is of integer type of precision less than the default integer precision, then the default integer precision is used. Otherwise, the precision is the same as that of*a*.**out**: Array into which the output is placed. Its type is preserved and must be of the right shape to hold the output.

**Return value**

The numpy trace() function returns the nd-array of int. If an array is larger than 2 Dimensions, ** **then an array of sums along diagonals is returned.

**Programming Example**

**Program to find the sum of diagonal elements of the 2D array.**

# Importing numpy import numpy as np # declaring the array arr = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) print("The array is: \n", arr) # Finding sum of diagonal elements outs = np.trace(arr) print("Output is: \n", outs)

**Output**

The array is: [[1. 2. 3.] [4. 5. 6.] [7. 8. 9.]] Output is: 15.0

**Explanation**

In this program. we have taken an array named ‘arr’ whose elements are

arr= ( [1.0,2.0,3.0],

** **[4.0,5.0,6.0],

[7.0,8.0,9.0] ).

Diagonal elements are 1.0,5.0,9.0, So the sum of all the diagonal elements will be 15.0. Then we passed a complete array as the parameter inside the **np.trace()** method and stored its return value.

Afterward, we printed the output, i.e., the sum, which comes out to be 15.0. Hence we can conclude that numpy.trace() method is used to find the sum of diagonal elements of an array.

**Find a sum of diagonal elements of 3D numpy array.**

See the following code.

# Importing numpy import numpy as np # declaring the array arr = np.array([[[1, 2], [2, 3], [3, 4]], [ [4, 5], [4, 5], [3, 4]], [[5, 6], [4, 5], [4, 5]]]) print("The array is: \n", arr) print("Shape of the array is: ", np.shape(arr)) # Finding sum of diagonal elements outs = np.trace(arr) print("Output is: \n", outs)

**Output**

The array is: [[[1 2] [2 3] [3 4]] [[4 5] [4 5] [3 4]] [[5 6] [4 5] [4 5]]] Shape of the array is: (3, 3, 2) Output is: [ 9 12]

**Explanation**

In this program. we have taken a 3-D array named ‘arr’ whose elements are arr

= ( [ [1,2] , [2,3] , [3,4] ], ** **[ [4,5] , [4,5] , [3,4] ], [ [5,6] , [4,5] , [4,5] ] )

Diagonal elements are an array Of ( [1,2], [4,5], [4,5] ), So the sum of all the diagonal elements will also be in the form of an array which is [9,12]. Then we passed the complete array as the parameter inside the np.trace() method and stored its return value inside the variable.

Afterward, we printed the output, i.e., the output variable, which came out to be [9,12]. Hence we can conclude that numpy.trace() method is used to find the sum of diagonal elements of an array.

That is it for the Numpy trace() method.