How to improve the performance of this Python code?

Is there a way to improve the Python code that I cited below? Now it seems to me too slow.

C_abs = abs(C)
_, n = C_abs.shape

G = np.zeros((n, n))
for i in xrange(n):
    for j in xrange(n):
        G[i,j] = C_abs[i,j]+C_abs[j,i]
+4
source share
1 answer

Just add C_abswith transposed version-

G = C_abs + C_abs.T

To understand, look at the computational part of the code:

G[i,j] = C_abs[i,j]+C_abs[j,i]

- C_abs[i,j], , - G[i,j]. , . - C_abs[j,i], - G[i,j]. C_abs. , , C_abs , .

+6

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


All Articles