You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何优化用于计算序列累积偏度的Python函数?

累积偏度计算实现

功能需求

针对包含零值的输入序列,计算当前元素及之前所有历史值的累积偏度,最终输出一个每行对应历史累积偏度的Series。

现有实现代码

def skewness_line(r):
    """
    计算输入序列每行的累积偏度
    返回一个包含每行累积偏度的Series
    """
    acumulado = pd.Series()
    result = []
    contador = 0
    for i in r: 
        acumulado[contador] = i
        demeaned_r = acumulado - acumulado.mean()
        # 使用总体标准差,设置自由度ddof=0
        sigma_r = acumulado.std(ddof=0)
        exp = (demeaned_r**3).mean()
        result.append(exp/sigma_r**3)
        contador += 1
        
    return result

使用示例

df1['SK'] = skewness_line(df1['Col_name'])
df1.head()

注:原使用示例中函数名写为skewness,与定义的skewness_line不一致,已修正为匹配函数定义的名称。

内容的提问来源于stack exchange,提问作者Josue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 20:12:37