10

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.

1

5 Answers 5

11

Convert you list from strings to np.float:

>>> gp = np.array(goodPix, np.float)
>>> np.mean(gp)
96.906260000000003
Sign up to request clarification or add additional context in comments.

6 Comments

hmm, i get that your solution might be a bit more pythonic, but why +7 there n +0 on mine?
@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.
@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: 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...
@SteveJessop just checked your top answers (150+), all are the screen long :)
|
5

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.

Comments

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)

3 Comments

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])
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 That depends on the length of the list.
0

Using list comprehension

>>> np.mean([float(n) for n in goodPix])
96.906260000000003

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.