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

Rich Shepard rshepard@appl-ecosys....
Sun Feb 25 13:10:34 CST 2007

```On Thu, 22 Feb 2007, Rich Shepard wrote:

>   As a newcomer to python, NumPy, and SciPy I need to learn how to most
> efficiently manipulate data. Today's need is reducing a list of tuples to
> three smaller lists of tuples, creating a symmetrical matrix from each list,
> and calculating the Eigenvector of each of the three symmetrical matrices.

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.

First, a python message when I invoke the application. In this module, I
have 'from scipi import *' per Travis' book. What I see as the application

Overwriting info=<function info at 0xb60e2614> from scipy.misc (was
<function info at 0xb60b5d84> from numpy.lib.utils)

This doesn't seem to harm anything, but perhaps it needs fixing.

Second, here's the function (followed by the output of the print
statements):

def weightcalc():

# First: average for each position by category
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"""
appData.cur.execute(stmt1)
prefbar = appData.cur.fetchall()

# 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':
ec.append(item[2:])
if item[0] == 'eco' and item[1] == 'neu':
en.append(item[2:])
if item[0] == 'eco' and item[1] == 'pro':
ep.append(item[2:])

if item[0] == 'nat' and item[1] == 'con':
nc.append(item[2:])
if item[0] == 'nat' and item[1] == 'neu':
nn.append(item[2:])
if item[0] == 'nat' and item[1] == 'pro':
np.append(item[2:])

if item[0] == 'soc' and item[1] == 'con':
sc.append(item[2:])
if item[0] == 'soc' and item[1] == 'neu':
sn.append(item[2:])
if item[0] == 'soc' and item[1] == 'pro':
sp.append(item[2:])

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

and here is the output of catEco:

[[(2.4884848484848487, 3.3123939393939401, 3.144090909090909,
2.5676060606060607, 3.2095151515151517, 3.4157878787878788,
2.5132727272727275, 2.7514242424242425, 2.9628787878787879,
2.446939393939394, 2.7069393939393938, 3.1676666666666669,
2.8530303030303035, 2.6058484848484853, 3.0955454545454546,
2.6283939393939395, 2.4350606060606061, 3.2610303030303034,
2.3926969696969698, 2.4951212121212123, 2.5276666666666676,
2.668848484848485, 3.4265757575757578, 2.9714545454545456,
2.8431818181818187, 3.0674545454545461, 2.8712727272727272,
2.1262424242424243), (2.0477142857142856, 1.0064285714285715,
3.1869285714285711, 3.5895000000000001, 3.9467142857142861,
3.2696428571428569, 2.9104285714285716, 2.5850714285714282,
4.8555714285714293, 3.3554999999999997, 2.3430714285714282,
3.5795714285714282, 1.3627857142857143, 0.83778571428571436,
2.4744999999999999, 2.8067142857142855, 3.143642857142857,
2.4637857142857138, 3.7382142857142857, 3.2875000000000001,
2.1167857142857143, 3.5459285714285715, 3.5667142857142857,
3.1280714285714284, 3.580428571428572, 1.0882857142857143,
3.0217142857142858, 3.8292857142857142), (2.3360769230769227,
2.0547692307692311, 2.8591538461538457, 2.4986923076923073,
2.809769230769231, 2.2041538461538464, 3.9557692307692309,
3.1109230769230769, 1.8777692307692309, 1.6783846153846156,
2.4337692307692307, 1.8520769230769232, 4.0975384615384618,
3.3513846153846147, 1.9008461538461536, 2.9993846153846158,
1.8076923076923079, 2.6881538461538463, 2.453615384615385,
3.5579999999999998, 1.2396153846153848, 3.8225384615384614,
2.8304615384615386, 2.6258461538461537, 2.2387692307692308,
3.381615384615384, 2.8569999999999998, 2.9676153846153848)]]

Where I am stuck is making catEco (and the other two lists) NumPy arrays,
and calculating the average of the three values in the same position within
each tuple. Also, I need no more than 2 decimal places for each value, but
I don't know where to place a format specifier.

Please suggest how to both improve the function's structure and produce a
ndarray[] that is the average tuple values in each list.

Rich

--
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
```