移动Window求和乘积计算Python
Moving Window Sum product Calculation Python
我有以下列表:
a= [1,2,3,4,5,6,7,8,9,10,11,12]
wts= [0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.10,0.10,0.30]
期望的结果是
result = [8.2,7.76,7.848,7.9504,8.179253333,8.420282667,8.628383467,8.790601973,8.894139057,8.930025594,8.891166196,8.770706404]
结果是列表'a'和列表'wts'的移动window和乘积。
例如结果8.2通过代码得到
sum(map(lambda xi, yi: xi * yi,x,wt))
结果是通过将 8.2 附加到列表 'a' 获得的新 window 获得的。
新列表 a 应该是附加上述结果的结果。
a = [1,2,3,4,5,6,7,8,9,10,11,12,8.2]
现在计算结果列表的下一个值即result[1] = 7.76,它应该是
的和积
a = [2,3,4,5,6,7,8,9,10,11,12,8.2] and
wts = [0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.10,0.10,0.30]
'wts' 列表是固定的,只有列表 'a' 会移动 window,新结果附加到 a.Any python 脚本到实现这一点在这里会有很大帮助。
基于下面,我将下面的函数应用于数据框。您能否阐明我如何将此功能应用于基于多个组(基于 Groupby)的数据框。
def wma(Curr,wts):
Curr.values.tolist()
wts.values.tolist()
len_list = len(Curr)
# Create a fixed sized queue
q = deque(maxlen=len_list)
# Add list a to q
q.extend(Curr)
i = 0
result = []
while i < len(a):
val = sum([x*y for x, y in zip(q, wts)])
q.append(val)
result.append(float(round(val, 2)))
i += 1
return result
例如,我有一个包含 5 列的数据框,即(A 列、B 列、C 列、权重、当前)。我使用以下代码应用上述功能
s1 = s1.groupby(['Column A', 'Column B', 'Column C']).apply(wma(df['Current'],df['Weights']))
我收到以下错误:类型错误:无法散列的类型:'list'。任何帮助都会有很大帮助。
在这种情况下,您需要使用 fixed-sized queue
,如下所示:
尝试:
from collections import deque
a= [1,2,3,4,5,6,7,8,9,10,11,12]
wts= [0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.10,0.10,0.30]
len_a = len(a)
# Create a fixed sized queue
q = deque(maxlen=len_a)
# Add list a to q
q.extend(a)
# See the q
print('q: ', q)
i = 0
result = []
while i < len(a):
val = sum([x*y for x, y in zip(q, wts)])
q.append(val)
result.append(float(round(val, 2)))
i += 1
print('result: ', result)
# Output
q: deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], maxlen=12)
result: [8.2, 7.76, 7.85, 7.95, 8.18, 8.42, 8.63, 8.79, 8.89, 8.93, 8.89, 8.77]
我有以下列表:
a= [1,2,3,4,5,6,7,8,9,10,11,12]
wts= [0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.10,0.10,0.30]
期望的结果是
result = [8.2,7.76,7.848,7.9504,8.179253333,8.420282667,8.628383467,8.790601973,8.894139057,8.930025594,8.891166196,8.770706404]
结果是列表'a'和列表'wts'的移动window和乘积。
例如结果8.2通过代码得到
sum(map(lambda xi, yi: xi * yi,x,wt))
结果是通过将 8.2 附加到列表 'a' 获得的新 window 获得的。
新列表 a 应该是附加上述结果的结果。
a = [1,2,3,4,5,6,7,8,9,10,11,12,8.2]
现在计算结果列表的下一个值即result[1] = 7.76,它应该是
的和积a = [2,3,4,5,6,7,8,9,10,11,12,8.2] and
wts = [0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.10,0.10,0.30]
'wts' 列表是固定的,只有列表 'a' 会移动 window,新结果附加到 a.Any python 脚本到实现这一点在这里会有很大帮助。
基于下面,我将下面的函数应用于数据框。您能否阐明我如何将此功能应用于基于多个组(基于 Groupby)的数据框。
def wma(Curr,wts):
Curr.values.tolist()
wts.values.tolist()
len_list = len(Curr)
# Create a fixed sized queue
q = deque(maxlen=len_list)
# Add list a to q
q.extend(Curr)
i = 0
result = []
while i < len(a):
val = sum([x*y for x, y in zip(q, wts)])
q.append(val)
result.append(float(round(val, 2)))
i += 1
return result
例如,我有一个包含 5 列的数据框,即(A 列、B 列、C 列、权重、当前)。我使用以下代码应用上述功能
s1 = s1.groupby(['Column A', 'Column B', 'Column C']).apply(wma(df['Current'],df['Weights']))
我收到以下错误:类型错误:无法散列的类型:'list'。任何帮助都会有很大帮助。
在这种情况下,您需要使用 fixed-sized queue
,如下所示:
尝试:
from collections import deque
a= [1,2,3,4,5,6,7,8,9,10,11,12]
wts= [0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.055555556,0.10,0.10,0.30]
len_a = len(a)
# Create a fixed sized queue
q = deque(maxlen=len_a)
# Add list a to q
q.extend(a)
# See the q
print('q: ', q)
i = 0
result = []
while i < len(a):
val = sum([x*y for x, y in zip(q, wts)])
q.append(val)
result.append(float(round(val, 2)))
i += 1
print('result: ', result)
# Output
q: deque([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], maxlen=12)
result: [8.2, 7.76, 7.85, 7.95, 8.18, 8.42, 8.63, 8.79, 8.89, 8.93, 8.89, 8.77]