Element-Wise Mean Over List of Matrices

Element-wise mean over list of matrices

You can use:

Reduce("+", my.list) / length(my.list)

According to comments, you want both mean and sd implemented on a list of matrices, and the above ways will not work smoothly for sd. Try this instead :

apply(simplify2array(my.list), 1:2, mean)
apply(simplify2array(my.list), 1:2, sd)

element-wise averages of two (or more) nested lists of matrices

In base R, We could use:

A <-list(A_1, A_2)
lapply(Reduce(\(x, y)Map('+', x, y), A), '/', length(A))

$a
[,1] [,2] [,3] [,4]
[1,] 5.5 7.5 9.5 11.5
[2,] 6.5 8.5 10.5 12.5

$b
[,1] [,2] [,3] [,4]
[1,] 3.5 5.5 7.5 9.5
[2,] 4.5 6.5 8.5 10.5

This code is generic in that we can use to find the mean of several lists.
Note that A_1 and A_2 must have the same number of matrices, not necessarily 2. Can be 10 etc. Also note that each corresponding matrix has the same dimensions. Example below:

B_1 <- list(matrix(c(1,2,3,4), 2), matrix(c(1,3,4,2), 2),
matrix(c(1:10), 5), matrix(c(1:20), 5))
B_2 <- lapply(B_1, '*', 2) # In this case, its B_1 * 2
B_3 <- lapply(B_2, '*', 3) #

Now you could use the code provide above:

B <-list(B_1, B_2, B_3)
lapply(Reduce(\(x, y)Map('+', x, y), B), '/', length(B))

How to get the element-wise mean of an ndarray

You can just use np.mean directly:

>>> np.mean([a, b, c], axis=0)
array([ 30., 20., 30.])

How to average the same cells over many lists of matrices in R

Use as.matrix on the output if you want a matrix class object instead of a data.frame

Reduce('+', x)/length(x)

Compute element-wise quantiles for a list of matrices R

Q <- apply(simplify2array(matrix.list), 1:2, quantile, prob = c(0.025, 0.975))

apply would simplify the result to an array, and since the function quantile returns more than one values, you have a 3D array. But extraction is straightforward:

Q[1, , ]  ## 0.025 quantile
# [,1] [,2] [,3]
#[1,] -2.046691 -1.925256 -2.075718
#[2,] -1.981182 -1.999648 -1.887588
#[3,] -1.931738 -1.743275 -1.854083

Q[2, , ] ## 0.975 quantile
# [,1] [,2] [,3]
#[1,] 1.953820 2.042508 1.836591
#[2,] 2.065854 2.006068 1.899495
#[3,] 1.885080 2.021729 1.943645

Python: get the element-wise mean of multiple arrays in a dataframe

Setup

np.random.seed([3,14159])
df = pd.DataFrame(
np.random.randint(10, size=(3, 3, 5)).tolist(),
list('XYZ'), list('ABC')
).applymap(np.array)

df.loc['X', 'B'] = np.nan
df.loc['Z', 'A'] = np.nan

df

                 A                B                C
X [4, 8, 1, 1, 9] NaN [8, 2, 8, 4, 9]
Y [4, 3, 4, 1, 5] [1, 2, 6, 2, 7] [7, 1, 1, 7, 8]
Z NaN [9, 3, 8, 7, 7] [2, 6, 3, 1, 9]

Solution

g = df.stack().groupby(level=1)
g.apply(np.sum, axis=0) / g.size()

A [4.0, 5.5, 2.5, 1.0, 7.0]
B [5.0, 2.5, 7.0, 4.5, 7.0]
C [5.66666666667, 3.0, 4.0, 4.0, 8.66666666667]
dtype: object

If you insist on the shape you presented

g = df.stack().groupby(level=1)
(g.apply(np.sum, axis=0) / g.size()).to_frame().T

A B C
0 [4.0, 5.5, 2.5, 1.0, 7.0] [5.0, 2.5, 7.0, 4.5, 7.0] [5.66666666667, 3.0, 4.0, 4.0, 8.66666666667]


Related Topics



Leave a reply



Submit