gpt4 book ai didi

python - Pandas for Python 中的 Winsorize 数据

转载 作者:行者123 更新时间:2023-11-28 17:50:20 29 4
gpt4 key购买 nike

我正在尝试在 pandas 中为 Python 运行 Winsorized 回归。 very helpful user manual提供此示例代码:

winz = rets.copy()
std_1year = rolling_std(rets, 250, min_periods=20)
cap_level = 3 * np.sign(winz) * std_1year
winz[np.abs(winz) > 3 * std_1year] = cap_level
winz_model = ols(y=winz['AAPL'], x=winz.ix[:, ['GOOG']],window=250)

我觉得第四行不对:RHS 不应该是cap_level[np.abs(winz) > 3 * std_1year]吗?

感谢您的帮助!我对使用 Pandas 数据框还是很陌生,想确保我的理解是正确的。

最佳答案

编辑:抱歉,误解了问题!

你是对的,这对大多数类型来说都是错误的;但是 pandas.DataFrame 对使用 bool 掩码设置值有特殊的支持;它将从具有相应时间值的 RHS 中选择相应的值。在引擎盖下它使用 np.putmask .

你可以自己检查一下:

>>> df = pandas.DataFrame(np.linspace(0.0, 1.0, 10).reshape(5, 2))
>>> df[df > 0.5] = -df
>>> df
0 1
0 0.000000 0.111111
1 0.222222 0.333333
2 0.444444 -0.555556
3 -0.666667 -0.777778
4 -0.888889 -1.000000

关于python - Pandas for Python 中的 Winsorize 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11295147/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com