0

I have to find mean of each list in a list of lists. I am using numpy. I tried

numpy.mean(a, axis=1) 

It works fine when all lists are equal in length.

In my case each list may be of different length. It is giving

IndexError: `tuple index out of range`

Code to reproduce

import numpy
data = numpy.array([[1,2,3], [4,6]])
print(numpy.mean(data, axis=1))

Desired output

[ 2.  5.]
Sreeragh A R
  • 2,871
  • 3
  • 27
  • 54

3 Answers3

2

This code snippet could work:

data_mean = [numpy.mean(lst, axis=0) for lst in data]
0

Will this work for you?

numpy.mean([numpy.mean(list, axis=1) for list in lists], axis=1)
Ron Serruya
  • 3,988
  • 1
  • 16
  • 26
0

You could loop in the lists:

import numpy

data = numpy.array([[1, 2, 3], [4, 6]])
means = []
for row in data:
    means.append(numpy.mean(row))

print(means)
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23