I currently have a dataframe formatted as such:
a b c e f
2 3 4 5 1
9 8 6 4 6
1 2 2 2 3
9 8 8 9 1
3 6 8 6 2
. . . . .
. . . . .
7 5 4 1 8
My goal is to apply an adaptive weight function over the last n(4) games in a sliding window. On each individual column. For example:
awa = [0.1, 0.2, 0.3, 0.4])
col a
a5 = sum([2, 9, 1, 9] * awa)
So the 5th element would be equal to the sum of last 4 (.2 + 1.8 + .3 + 3.6) = 5.9 I want to do this while maintaining the original dataset - meaning the 5.9 calculation wouldn't be used in the AWA calculation for the next elements. Next calculation:
a6 = sum([9, 1, 9, 3] * awa)
a6 = 5
And so forth...
My initial approach would be the following:
- Convert the dataframe to a numpy array
df.to_numpy()
- Utilize the np.convolve function OR sliding window view
from numpy.lib.stride_tricks import sliding_window_view
np.sum(sliding_window_view(values, window_shape = 4), axis = 1)
np.convolve(mydata,np.ones(4,dtype=int),'valid')
My problem becomes twofold:
- How do utilize these functions on columns instead of numpy rows?
- How to apply the adaptive weight function dynamically to each 'column slice' before remaking the frame?