lis中正值的python滚动窗口

2024-06-25 05:50:09 发布

您现在位置:Python中文网/ 问答频道 /正文

什么是python方法来计算列表的平均值,但只考虑正值?在

所以如果我有价值观 [1,2,3,4,5,-1,4,2,3]我要计算三个值的滚动平均值,它基本上是计算[1,2,3,4,5,'nan',4,2,3]的平均滚动平均值。 这就变成了 [nan,2,3,4,4.5,4.5,3,nan]其中第一个和最后一个nan是由于缺少元素。 2=平均值([1,2,3]) 3=平均值([2,3,4]) 但是4.5=平均值([4,5,nan])=平均值([4,5]) 等等。所以重要的是,当有负值时,它们被排除在外,但除法是在正值的数目之间。在

我试过了:

def RollingPositiveAverage(listA,nElements):
     listB=[element for element in listA if element>0]
     return pd.rolling_mean(listB,3)

但是列表B缺少元素。我试图用nan代替这些元素,但是平均值变成了nan本身。在

有什么好办法解决这个问题吗?在

谢谢


Tags: 方法元素列表fordefelementnan平均值
2条回答

得到滚动求和,得到参与正元素掩码滚动求和的有效元素的计数,并简单地将其除以平均值。对于滚动求和,我们可以使用^{}。在

因此,实施-

def rolling_mean(a, W=3):
    a = np.asarray(a) # convert to array
    k = np.ones(W) # kernel for convolution

    # Mask of positive numbers and get clipped array
    m = a>=0
    a_clipped = np.where(m,a,0)

    # Get rolling windowed summations and divide by the rolling valid counts
    return np.convolve(a_clipped,k,'same')/np.convolve(m,k,'same')

扩展到NaN-padding在边界处的特定情况-

^{pr2}$

样本运行-

In [54]: a
Out[54]: array([ 1,  2,  3,  4,  5, -1,  4,  2,  3])

In [55]: rolling_mean_pad(a, W=3)
Out[55]: array([ nan,  2. ,  3. ,  4. ,  4.5,  4.5,  3. ,  3. ,  nan])

既然你用的是熊猫:

import numpy as np
import pandas as pd

def RollingPositiveAverage(listA, window=3):
     s = pd.Series(listA)
     s[s < 0] = np.nan
     result = s.rolling(window, center=True, min_periods=1).mean()
     result.iloc[:window // 2] = np.nan
     result.iloc[-(window // 2):] = np.nan
     return result  # or result.values or list(result) if you prefer array or list

print(RollingPositiveAverage([1, 2, 3, 4, 5, -1, 4, 2, 3]))

输出:

^{pr2}$

纯Python版本:

import math

def RollingPositiveAverage(listA, window=3):
    result = [math.nan] * (window // 2)
    for win in zip(*(listA[i:] for i in range(window))):
        win = tuple(v for v in win if v >= 0)
        result.append(float(sum(win)) / min(len(win), 1))
    result.extend([math.nan] * (window // 2))
    return result

print(RollingPositiveAverage([1, 2, 3, 4, 5, -1, 4, 2, 3]))

输出:

[nan, 2.0, 3.0, 4.0, 4.5, 4.5, 3.0, 3.0, nan]

相关问题 更多 >