7

I have a list that I want to calculate the average(mean?) of the values for her. When I do this:

import numpy as np #in the beginning of the code

goodPix = ['96.7958', '97.4333', '96.7938', '96.2792', '97.2292']
PixAvg = np.mean(goodPix)

I'm getting this error code:

ret = um.add.reduce(arr, axis=axis, dtype=dtype, out=out, keepdims=keepdims)

TypeError: cannot perform reduce with flexible type

I tried to find some help but didn't find something that was helpful

Thank you all.

handle
  • 5,859
  • 3
  • 54
  • 82
shlomi
  • 523
  • 3
  • 13
  • 26
  • [Calculating arithmetic mean (one type of average) in Python](https://stackoverflow.com/questions/7716331/calculating-arithmetic-mean-one-type-of-average-in-python) – handle Dec 16 '21 at 10:50

5 Answers5

10

Convert you list from strings to np.float:

>>> gp = np.array(goodPix, np.float)
>>> np.mean(gp)
96.906260000000003
alko
  • 46,136
  • 12
  • 94
  • 102
  • hmm, i get that your solution might be a bit more pythonic, but why +7 there n +0 on mine? – usethedeathstar Dec 02 '13 at 13:45
  • @usethedeathstar: I think you have to accept that having upvoted an answer, people aren't duty-bound to then upvote every other answer that is correct but inferior. If alko's superior answer wasn't here then sure, maybe some of those 7 people would have upvoted yours instead. – Steve Jessop Dec 02 '13 at 14:56
  • @SteveJessop its more the fact that for a simple answer like this you get +9, while for an answer that is two pages long, people hardly ever get over +3 – usethedeathstar Dec 02 '13 at 15:07
  • 1
    @usethedeathstar: the more obviously correct an answer is, the more people are able to judge it. Probably not fair, but pretty inevitable that short answers will tend to win that contest... – Steve Jessop Dec 02 '13 at 15:08
  • @SteveJessop just checked your top answers (150+), all are the screen long :) – alko Dec 02 '13 at 16:46
  • @alko: same is probably true for my bottom answers, I'm inclined to be verbose :-) – Steve Jessop Dec 02 '13 at 17:11
3

There is a statistics library if you are using python >= 3.4

https://docs.python.org/3/library/statistics.html

You may use it's mean method like this. Let's say you have a list of numbers of which you want to find mean:-

list = [11, 13, 12, 15, 17]
import statistics as s
s.mean(list)

It has other methods too like stdev, variance, mode etc.

Chetan Sharma
  • 1,432
  • 12
  • 13
2

The things are still strings instead of floats. Try the following:

goodPix = ['96.7958', '97.4333', '96.7938', '96.2792', '97.2292']
gp2 = []
for i in goodPix:
    gp2.append(float(i))
numpy.mean(gp2)
usethedeathstar
  • 2,219
  • 1
  • 19
  • 30
  • I can't guess why some answers get instantly upvoted so those +8 is a riddle for me too; as for your answer, it is not bad, but me myself won't +1 as it's about two times slower than approach with numpy, and can be rewritten shorter and clearer as `np.mean([float(i) for i in goodPix])` – alko Dec 02 '13 at 13:53
  • true, but two times slower than the numpy approach, i assume that means 2 nanosec instead of 1 nanosec? If it aint slow, dont make it faster? – usethedeathstar Dec 02 '13 at 13:54
  • @usethedeathstar That depends on the length of the list. – Steinar Lima Dec 02 '13 at 14:54
0

Using list comprehension

>>> np.mean([float(n) for n in goodPix])
96.906260000000003
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
0

If you're not using numpy, the obvious way to calculate the arithmetic mean of a list of values is to divide the sum of all elements by the number of elements, which is easily achieved using the two built-ins sum() and len(), e.g.:

>>> l = [1,3]
>>> sum(l)/len(l)
2.0

In case the list elements are strings, one way to convert them is with a list comprehension:

>>> s = ['1','3']
>>> l = [float(e) for e in s]
>>> l
[1.0, 3.0]

For an integer result, use the // operator ("floored quotient of x and y") or convert with int().

For many other solutions, also see Calculating arithmetic mean (one type of average) in Python

handle
  • 5,859
  • 3
  • 54
  • 82