# Suggestions for optimizing my code

Discussion in 'Python' started by drife, Aug 5, 2005.

1. ### drifeGuest

Hello,

I am looking for suggestions for how I might optimize my
code (make it execute faster), and make it more streamlined/
elegent. But before describing my code, I want to state that
I am not a computer scientist (I am an atmospheric scientist),
and have but a rudimentary understanding of OO principles.

My problem is this: I want to find the maximum off-diagonal
element of a correlation matrix, and the -position- of that
element within the matrix. In case you are unfamiliar with
correlation matrices they are square and symmetric, and contain
the mutual correlations among data collected at different
points in space or time.

I write much Python code and absolutely love the language. To
do the task outlined above I twiddled around with the "argsort"
methods available in both Numeric and numarry. In the end, I
decided to accomplish this through the following algorithm.

<----

import MLab
import Numeric as N

def FindMax( R):
"""Find row (column) where max off-diagonal element
occurs in matrx [R].
"""
# First get elements in the lower triangle of [R],
# since the diagonal elements are uninformative and
# the upper triangle contains redundant information.
Y = MLab.tril(R)

offMax = -9999.
rowMax = 0
colMax = 0

for row in range(len(Y)):
for col in range(0,row):
if Y[row][col] > offMax:
offMax = Y[row][col]
rowMax = row
colMax = col

return (rowMax, colMax, offMax)

---->

Now, this algorithm will work sufficiently fast on "small" sized
matrices, but I worry that performance will not scale well when
the dimensions of the matrix grow "large" (say 1000-1500 elements
on a side, or larger).

So onto my question. Could someone please provide me with a suggestion
for making this code more efficient and elegant? Again, I have but
a rudimentary understanding of OO principles.

Thanks very much for your help,

Daran

drife, Aug 5, 2005

2. ### Wojciech MulaGuest

In this case you should use 'xrange' instead 'range'.

w.

Wojciech Mula, Aug 5, 2005

3. ### Robert KernGuest

import MLab
import Numeric as N

def find_max(R):
U = MLab.triu(R)
n = U.shape
# set the diagonal elements to 0.0, too
U.flat[::n+1] = 0.0
k = N.argmax(U.flat)
i, j = divmod(k, n)
return i, j, R[i,j]

--
Robert Kern

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Robert Kern, Aug 5, 2005