Access to the lower triangle of the matrix matrix?

Okay, so basically you can say that I have a matrix:

matrix([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]) 

Is it easy to get the area under the diagonal when working with numpy matrices? I looked around and found nothing. I can make a standard, for a loop, but is it possible to somehow nullify the performance provided by numpy?

I am working on the calculation of statistics comparing the results of model output with actual results. The data that I have currently received is about 10,000 x 10,000 matrices. I basically try to just summarize these elements.

Is there an easy way to do this?

+4
source share
2 answers
+11
source
 def tri_flat(array): R = array.shape[0] mask = np.asarray(np.invert(np.tri(R,R,dtype=bool)),dtype=float) x,y = mask.nonzero() return array[x,y] 

I myself was looking for a convenience function, but it needs to be done ... not sure if this will become much easier. But if that happens, I would be interested to hear that. Every time you avoid the loop, the angel gets its wings.

-ejh

Quick NB: this avoids the diagonal ... if you want it, and your matrix is โ€‹โ€‹symmetrical, just omit the inversion (elementologically NOT). Otherwise, you will need transposition.

+2
source

Source: https://habr.com/ru/post/1302628/


All Articles