Suggestions for optimizing my code

D

drife

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


(e-mail address removed)
 
R

Robert Kern

drife said:
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.

import MLab
import Numeric as N

def find_max(R):
U = MLab.triu(R)
n = U.shape[0]
# 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
(e-mail address removed)

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top