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

Rich Shepard rshepard@appl-ecosys....
Mon Feb 26 14:39:06 CST 2007

```On Mon, 26 Feb 2007, Johannes Loehnert wrote:

> here's your code as I would have written it. :-) My comments are those
> starting with four #'s.

Thank you, Johannes.

> As a side remark, usually one uses four spaces for intendation. I hope I did
> not mix it up.

Yes, I know that's the standard for python code contributed to projects.
I've used two space tabs for indenting C code for a couple of decades now,
and I find it easier to read. Since this code is being used by us, we format
for our convenience.

> An alternative approach to what I did below would be to map the keys ('eco',
>    'nat', 'soc') to integers and use lists instead of dicts.

That can be done for this function. But, if dicts work, that's OK, too.

> def weightcalc():
>   # First: average for each position by category
>   meanvotes = []     #### Do you use this?

No, I meant to take that out, and have.

>   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()
>   # print prefbar

This is the source of the data: a SQLite3 database table.

>   #### using a dict saves us from creating all the lists
>   data = {}

>   for item in prefbar:
>      #### create dict entries on demand
>      if item[0] not in data:
>         data[item[0]] = {}
>      if item[1] not in data[item[0]]:
>         data[item[0]][item[1]] = []
>
>      #### append to list
>      data[item[0]][item[1]].append(item[2:])

The above seems to do the opposite of what I need. 'prefbar' is a list of
tuples, and the first two items of each tuple are strings. I want to remove
those strings and have only the reals. Doesn't the above just copy prefbar
to data?

>   catarrays = {}
>   averages = {}
>   for key in ['eco', 'nat', 'soc']:
>      catarrays[key] = []
>      for subkey in ['con', 'neu', 'pro']
>         #### btw. I don't understand why you throw con, neu, pro in one list
>         #### now after sorting them out in advance.

Let me try to explain. I have 9 sets of data as records in the database.
The sets are eco/con, eco/neu, eco/pro, nat/con, nat/neu, nat/pro, soc/con,
soc/neu, and soc/pro.

First, I need to average the 28 items in each of those 9 sets.
Second, I need to average the three average values for each of the 28
items within the main sets of eco, nat, and soc.

Results can be skewed if only an single, overall average of the 28 items
is calculated in a single step.

>         try:
>            catarrays[key].append(data[key][subkey])
>         except KeyError:
>             #### data[key][subkey] was not set
>             print 'No data for %s,%s'%(key, subkey)
>      #### convert to array
>      catarrays[key] = array(catarrays[key])
>      #### average
>      averages[key] = average(catarrays(key), axis=1)

This seems to be taking the averages in one step. We need them to be in
two steps. Am I mis-reading this?

Thanks,

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