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

Johannes Loehnert a.u.r.e.l.i.a.n@gmx....
Tue Feb 27 02:23:10 CST 2007

```Hi,

> >   #### 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?

No. data is a dict containing the three main keys (eco, nat, soc). Each maps
to a dict containing the subkeys (con, neu, pro). Each of those maps to a
list of data. E.g. for

item = [('eco', 'con', 1, 2, 3), ('eco', 'con', 4,5,6), ('eco', 'neu', 7,8,9),
('nat', 'neu', 10,11,12)]

the result would be

data == {'eco': {'con': [(1,2,3), (4,5,6)],
'neu': [(7,8,9)]},
'nat': {'neu': [(10,11,12)]}}.

So with data['eco']['con'] you get back [(1,2,3), (4,5,6)].

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

So you get only one data row for each category/subcategory combination? Or can
there be multiple?

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

what do you mean by item? A float number?
>
>    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?

Well, it ought to do exactly what your code did. averages['eco'] == barEco
IIANM.

Johannes
```