Home > Enterprise >  Change trace behavior Numpy
Change trace behavior Numpy

Time:12-24

I have this 5x5 eye array which I supposed to calculate the sum from k=1 using 'trace' keyword but turned out I have to change the trace start position, how do I that?.

import numpy as np
# TODO: Trace an eye  

eye_0 = np.eye(5,5, k=1)
trace_eye = np.trace(eye_0)
print(eye_0)
trace_eye 

[[0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]
 [0. 0. 0. 0. 0.]]
0.0

CodePudding user response:

Numpy.trace has an offset parameter:

np.trace(eye_0, offset = 1)

This will return 4 in your case.

  • Related