Numpy recarray.trace() function | Python Last Updated : 16 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record arrays allow the fields to be accessed as members of the array, using arr.a and arr.b. numpy.recarray.trace() function is used to return the sum along diagonals of the array. Syntax : numpy.recarray.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None)Parameters:offset : [int, optional] Offset of the diagonal from the main diagonal. Can be both positive and negative. Defaults to 0. axis1 : [int, optional] Axes to be used as the first and second axis of the 2-D sub-arrays from which the diagonals should be taken. axis2 : [int, optional] Axes to be used as the first and second axis of the 2-D sub-arrays from which the diagonals should be taken. dtype : [optional] Determines the data-type of the returned array. out : [ndarray, optional] A location into which the result is stored. -> If provided, it must have a shape that the inputs broadcast to. -> If not provided or None, a freshly-allocated array is returned. Return : [ndarray] sum of a diagonal elements of the record array.Code #1 : Python import numpy as geek # creating input array with 2 different field arr = geek.array([[(5.0, 2), (3.0, -4), (6.0, 9)], [(9.0, 1), (5.0, 4), (-12.0, -7)]], dtype =[('a', float), ('b', int)]) # convert it to a record array, # using arr.view(np.recarray) rec_arr = arr.view(geek.recarray) print("Record array of float: ", rec_arr.a) print("Record array of int: ", rec_arr.b) # applying recarray.trace methods # to float record array out_arr = rec_arr.a.trace() print ("Output float traced array : ", out_arr) # applying recarray.trace methods # to int record array out_arr = rec_arr.b.trace() print ("Output int traced array : ", out_arr) OutputRecord array of float: [[ 5. 3. 6.] [ 9. 5. -12.]] Record array of int: [[ 2 -4 9] [ 1 4 -7]] Output float traced array : 10.0 Output int traced array : 6 Comment More infoAdvertise with us Next Article Numpy recarray.repeat() function | Python J jana_sayantan Follow Improve Article Tags : Python Python-numpy Python numpy-arrayManipulation Practice Tags : python Similar Reads Numpy recarray.var() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 3 min read Numpy recarray.repeat() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 4 min read Numpy recarray.tobytes() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 3 min read Numpy recarray.tostring() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 3 min read Numpy recarray.tolist() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 3 min read Numpy recarray.swapaxes() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array 3 min read Like