0

I got a formal problem by using python and some for loops for each segment of my lists to be averaged.

  1. I have an array of x=(epochs, 257)
  2. Now I want to calculate for every epoch the mean value of the 257 values in there
  3. The output should be an array of x _ mean = (120, 1)

I used a for loop, but the results are more than questionable. Any help is appreciated.

3
  • Did you mean: sum(arr)/len(arr) ? Commented May 30, 2015 at 5:21
  • Why do you consider your results questionable? Commented May 30, 2015 at 5:26
  • x_mean=np.zeros (epochs, 1) For i in range (0, epochs): x_mean [i] = np.mean (x [i]) <---- that's my approach. ..I like to work with numpy more than using uncomfortable other stuff... Commented May 30, 2015 at 6:34

2 Answers 2

1
a = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]]
b = [float(sum(x))/len(x) for x in a]

Simply, you select all sublists and you calculate the average of each sublist in a standard way, e.g. sum/len.

The only potentially dangerous part is the divide operation. If the list contains only integers also the sum is integer. The len is integer automatically. Python 2 (in contrary to Python 3) uses integer-division when both numerator and denominator are integers so 2/3 -> 0. To prevent this we can convert e.g. the numerator to float.

Sign up to request clarification or add additional context in comments.

2 Comments

Nope way to uncomfortable.... look up ...numpy and just use numpy.mean (x) .....But I do like your approach. ..Thank you I think it is helpful....sometimes you Programm and miss the tree for the wood
after modifying your sugestion it fits in my code somehow...thank you for your help
0
np.mean(x, axis=0)

is the solution, thanks for trying so hard. and thanks for posting comments just because.

1 Comment

Please use the numpy tag in your question next time in order to emphasize that you are interested in the numpy-based solution.

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.