Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate mean in python?

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.

like image 523
shlomi Avatar asked Dec 02 '13 13:12

shlomi


People also ask

How do you find the mean in Python?

Using Python's mean() mean() function takes a sample of numeric data (any iterable) and returns its mean. We just need to import the statistics module and then call mean() with our sample as an argument. That will return the mean of the sample. This is a quick way of finding the mean using Python.

How do you manually calculate mean in Python?

We define a list of numbers and calculate the length of the list. We then use sum() function to get sum of all the elements in a list. We finally divide the total sum by the number of elements in the list and we print the result to get the mean/average of a list.

How do you find the average value in Python?

Using Python sum() function Python statistics. sum() function can also be used to find the average of data values in Python list. The statistics. len() function is used to calculate the length of the list i.e. the count of data items present in the list.


1 Answers

Convert you list from strings to np.float:

>>> gp = np.array(goodPix, np.float)
>>> np.mean(gp)
96.906260000000003
like image 64
alko Avatar answered Sep 27 '22 18:09

alko