Accumulation stage
In the script, the same-sized data matrix X
is re-estimated by some model (here just a random number generator (RNG)) and accumulated/saved in a matrix Y
over the course of a finite number of trials t
.
import numpy as np
from numpy.random import random
import pandas as pd
k = 3 #shape
t = 5 #trials
Y = np.zeros((t,k,k))
for i in range(5):
X = random((k,k)) #2D estimate
X = pd.DataFrame(X)
Y[i,:,:] = X #3D tensor
Reduction stage
Afterwards, how do I then apply an element-wise reduction of all accumulated 2d X
arrays inside the 3d Y
tensor into a single 2d matrix Z
that is the same shape as X
? An example reduction is the average of all the individual X
elements reduced into Z
:
Z[0,0] = average of: {the first Z[0,0], second Z[0,0], ... , fifth Z[0,0]}
I'd prefer no element-by-element loops, if possible. I showed the accumulation stage using numpy arrays because I don't think pandas DataFrames can be 3d tensors, being restricted to 2d inputs only, but can the arithmetic reduction stage (averaging across accumulated arrays) be done as a pandas DataFrame operation?