How to improve the performance of this Python code?

xxx222

Is there any way I can improve the Python code I attached below? It seems too slow for me now.

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]
Divakar

Just add C_abs with its transposed version -

G = C_abs + C_abs.T

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

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

The first input on the right side is C_abs[i,j], which has the same iterators involved as on the left side of the assignment - G[i,j]. So, for a vectorized solution, we will use it without change as the first input. The second input on the right side is C_abs[j,i] and its iterators are flipped version of the iterators on the left side - G[i,j]. This flipping in the entire array context would be the transpose of C_abs. Therefore, put together, we will add C_abs with its own transposed version to get the desired output in a vectorized manner.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to improve the performance of this Python code?

From Dev

Improve Python code performance

From Dev

how to improve Performance for this code?

From Dev

How to improve the performance of this piece of code?

From Dev

How to improve performance of code with Sink?

From Dev

How can I improve the performance of the below python code

From Dev

How do I improve the performance of the following code in python

From Dev

How to improve python dict performance?

From Dev

How can I improve the performance of this code?

From Dev

How to improve Python code speed

From Java

How to improve Spark performance?

From Dev

How to improve randomForest performance?

From Dev

How to improve performance of this algorithm?

From Dev

How to improve ClojureScript performance

From Dev

How to improve ILIKE Performance?

From Dev

How to improve performance with NSXMLParser

From Dev

How to improve Solr performance?

From Dev

How to improve Unity Performance?

From Dev

How to improve performance of this query

From Dev

How can i improve the code? packets send per second performance should improve. i am a beginner

From Dev

How can I improve this code in python?

From Dev

How to improve time of python code with database queries

From Dev

How to improve Python iteration performance over large files

From Dev

How to improve Python iteration performance over large files

From Dev

Python - How to improve performance of subsetting tuple key dictionaries through for loop?

From Dev

How to improve MongoDB insert performance

From Dev

How to improve insert performance in cassandra?

From Dev

How to improve performance of query execution

From Dev

How to avoid aliasing and improve performance?

Related Related

HotTag

Archive