Python Forum
Calculate the mean of an array across dimension with lists of different length
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate the mean of an array across dimension with lists of different length
#1
Hello, 

I have a list of lists and I need to calculate the mean across the dimension. The problem is that the sublists are all of different length. For example:

array=[[5,2,1],[2,1],[3,4,2,1]]

So in this case I am looking for the following result: 

mean=[3.333, 2.333, 1.5, 1] #[(5+2+2)/3, (2+1+4)/3, (1+2)/2, 1/1]

I found that this can be done with numpy.ma.mean. As far as I understood I have to combine all my sublists into one masked array. 

But I cannot figure out how to do it, taking into account that the number of sublists in an array and the length of sublists is always different.

Thanks in advance
Reply
#2
Can't you just do something like this?
array = [[5,2,1],[2,1],[3,4,2,1]]
means = [ ] ## begins as an empty list
for group in array:
    means.append(  mean(group)  )
(I don't know the syntax for your mean function- sorry.)
Reply
#3
for list in array:
Not a good idea to use object names for variables,
You should use a different variable name, like sublist or such.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turn list of arrays into an array of lists Cola_Reb 6 1,663 Jul-20-2022, 06:55 PM
Last Post: Cola_Reb
  Array lists vs Linked Lists sabe 2 1,749 Nov-28-2020, 12:31 AM
Last Post: perfringo
  Apply rolling window function over time dimension of 3D data Staph 0 2,160 Jan-01-2020, 08:31 AM
Last Post: Staph
  Are lists Python's name for "variable array" Luke_Drillbrain 6 4,859 Apr-21-2017, 09:14 AM
Last Post: volcano63
  1 dimension Matrix Right Division theo_taison1 1 3,938 Jan-25-2017, 04:51 PM
Last Post: stranac

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020