Calculate the mean and variance by element by element of multiple arrays in Python

Question:

I have a block of code that provides five arrays:

for i in range(10):
    x = pic[10,15+i]

This produces the following ten arrays:

[504 591 471 ...   0   0   0]
[748 753 658 ...  94 138 101]
[814 781 710 ...   0   0   0]
[754 719 663 ...   0   0   0]
[1068  982 1096 ...    0    0    0]
[845 811 709 ...   0   0   0]
[1002  985  915 ...    0    0    0]
[1036  916  860 ...    0    0    0]
[570 623 722 ...   0   0   0]
[728 711 738 ...   0   0  90]

There are 1220 elements in each arrays which are presented by … . I want to calculate the mean of those 10 arrays by element by element (column wise). So, I want a final output like this:

[860.9, 827.4, 795.3, ...  9.4, 13.8, 19.1]  

I am not getting how to do it in Python. Any help would be highly appreciated. Also, if you can help me calculate the variance as well it would be an amazing help!

Asked By: Damien Rice

||

Answers:

Using zip function:-

Code:-

import statistics
#Using package statistics :- Variance
#Using Zip function -: mean
lis=[[22,23,20,32,45,0],
     [25,23,20,33,39,0],
     [26,23,26,31,56,0],
     [28,23,26,0,11,0],
     [25,34,25,0,0,0]]
mean=[]
variance=[]
for i in zip(*lis):
    mean.append(sum(i)/len(i))
    variance.append(statistics.variance(i))
print(mean)
print(variance)

Output:-

[25.2, 25.2, 23.4, 19.2, 30.2, 0.0]
[4.7, 24.2, 9.8, 307.7, 560.7, 0]

Updated:-
Query-
Try this Don’t know is it working or not please let me know..

x=[]
mean=[]
variance=[]
for i in range(10):
    x.append(pic[10,15+i])

for i in zip(*x):
    mean.append(sum(i)/len(i))
    length=len(i)
    x=sum(i)/length  #x is also a mean
    variance.append(sum((j - x) ** 2 for j in i)/(length-1))
Answered By: Yash Mehta

You could use numpy as following:
Create an array that stores the 5 arrays, and then use the numpy.mean() function:

import numpy as np

arrays = [
[22, 23, 20, 32, 45, 0],
[25, 23, 20, 33, 39, 0],
[26, 23, 26, 31, 56, 0],
[28, 23, 26, 0, 11, 0],
[25, 34, 25, 0, 0, 0]]

mean = np.mean(arrays, axis=0)
variance = np.variance(arrays, axis=0)

The axis specifies that you want to calculate the mean column wise and not row wise.

Answered By: Luca Knaack