I am trying to find the mean of a Python list. When I print my list it is as follows:
[array([0.33345667, 0.33523274, 0.33222637, 0.3341597 , 0.33329942,
0.3330034 , 0.33197044, 0.33394078, 0.33458831, 0.33147091])]
That outcome is expected, there are no null values.
When I try statistics.mean(accuracy_list)
(which is what I have done in the past to get the mean) I get this error
TypeError: can't convert type 'ndarray' to numerator/denominator
Any ideas?
CodePudding user response:
statistics.mean()
works on vanilla Python lists. It does not work on numpy.ndarray
s. You should use numpy.mean()
instead.
Also, it looks like accuracy_list
is a list containing a one-element numpy
list. If that is the case, you can use list unpacking:
import numpy as np
[accuracy_list] = [np.array([0.33345667, 0.33523274, 0.33222637, 0.3341597 , 0.33329942,
0.3330034 , 0.33197044, 0.33394078, 0.33458831, 0.33147091])]
np.mean(accuracy_list)
Both of these output:
0.333334874
CodePudding user response:
I'm not sure why there's a NumPy array in the list, so you could just extract it and compute the mean:
accuracy_list[0].mean()
CodePudding user response:
Try this: l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
import statistics statistics.mean(l)
OR
On older versions of Python you can:
sum(l) / len(l) On Python 2, you need to convert len to a float to get float division
sum(l) / float(len(l))