如何优化用于计算序列累积偏度的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
相关产品推荐
相关产品推荐

