[SciPy-user] Need Advice With Arrays and Calculating Eigenvectors

Rich Shepard rshepard@appl-ecosys....
Sun Feb 25 15:08:52 CST 2007

On Sun, 25 Feb 2007, Rich Shepard wrote:

>   I have a function that does part of the above. I know that it's highly
> crude, inefficient, and not taking advantage of python functional coding features
> such as introspection. That's because I'm not yet sure how to code it
> better.

   Would still appreciate suggestions for tightening it up. The latest
version is this:

def weightcalc():
   # First: average for each position by category
   meanvotes = []
   stmt1 = """select cat, pos, avg(pr1), avg(pr2), avg(pr3), avg(pr4), avg(pr5),
              avg(pr6), avg(pr7), avg(pr8), avg(pr9), avg(pr10), avg(pr11),
              avg(pr12), avg(pr13), avg(pr14), avg(pr15), avg(pr16),
              avg(pr17), avg(pr18), avg(pr19), avg(pr20), avg(pr21), avg(pr22),
              avg(pr23), avg(pr24), avg(pr25), avg(pr26), avg(pr27), avg(pr28)
              from voting group by cat, pos"""
   prefbar = appData.cur.fetchall()
   # print prefbar

   # Now, average for all positions within each category
   ec = []
   en = []
   ep = []
   nc = []
   nn = []
   np = []
   sc = []
   sn = []
   sp = []
   catEco = []
   catNat = []
   catSoc = []
   diag = identity(8, dtype=float)

   for item in prefbar:
     if item[0] == 'eco' and item[1] == 'con':
     if item[0] == 'eco' and item[1] == 'neu':
     if item[0] == 'eco' and item[1] == 'pro':

     if item[0] == 'nat' and item[1] == 'con':
     if item[0] == 'nat' and item[1] == 'neu':
     if item[0] == 'nat' and item[1] == 'pro':

     if item[0] == 'soc' and item[1] == 'con':
     if item[0] == 'soc' and item[1] == 'neu':
     if item[0] == 'soc' and item[1] == 'pro':

   # three lists, each of three tuples. Need to be converted to arrays and averaged.
   catEco.append(ec + en + ep)
   catNat.append(nc + nn + np)
   catSoc.append(sc + sn + sp)

   # here are the numpy arrays
   aEco = array(catEco, dtype=float)
   aNat = array(catNat, dtype=float)
   aSoc = array(catSoc, dtype=float)

   # here are the numpy arrays of averages
   barEco = average(aEco, axis=1)
   barNat = average(aNat, axis=1)
   barSoc = average(aSoc, axis=1)

   Got all this worked out by reading the book and trial-and-error.

   Next step is to convert each of barEco, barNat, and barSoc into
symmetrical matrices with unit diagonals. Each of these arrays holds the
values to the right of the diagonal in the symmetrical matrices; the
matching cells to the left of the diagonal are (1.0 / right cell value).


Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.        |          Accelerator(TM)
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863

More information about the SciPy-user mailing list