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

Johannes Loehnert a.u.r.e.l.i.a.n@gmx....
Mon Feb 26 02:17:02 CST 2007


Hi,

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

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

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.

Johannes


def weightcalc():
   # First: average for each position by category
   meanvotes = []     #### Do you use this?
   #### not much can be done about this
   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

   #### 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:])

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


More information about the SciPy-user mailing list