Suggestions for optimizing my code

drife daranrife at yahoo.com
Fri Aug 5 13:33:51 EDT 2005


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


daranrifeNOS at PAMyahoo.com




More information about the Python-list mailing list