yk4*_*4r2 3 python numpy python-polars
我有一个代码
df.select([
pl.all().exclude("elapsed_time_linreg"),
pl.col("elapsed_time_linreg").arr.get(0).suffix("_slope"),
pl.col("elapsed_time_linreg").arr.get(1).suffix("_intercept"),
pl.col("elapsed_time_linreg").arr.get(2).suffix("_resid_std"),
])
Run Code Online (Sandbox Code Playgroud)
解压函数的结果
@jit
def linear_regression(session_np: np.ndarray) -> np.ndarray:
w = len(session_np)
x = np.arange(w)
sx = w ** 2 / 2
sy = np.sum(session_np)
sx2 = (w * (w + 1) * (2 * w + 1)) / 6
sxy = np.sum(x * session_np)
slope = (w * sxy - sx * sy) / (w * sx2 - sx**2)
intercept = (sy - slope * sx) / w
resids = session_np - (x * slope + intercept)
return slope, intercept, resids.std()
def get_linreg_aggs(session) -> np.ndarray:
return linear_regression(np.array(session))
df.select(
pl.col("elapsed_time")
.apply(utils.get_linreg_aggs)
.cast(pl.Float32)
.alias("elapsed_time_linreg")
)
Run Code Online (Sandbox Code Playgroud)
它在极地 17.15 上完美运行,但自从我将其升级到最新版本(0.18.4)后,它就停止工作了。现在我收到以下错误,并且不知道如何解决此问题:
AttributeError: 'ExprArrayNameSpace' object has no attribute 'get'
Run Code Online (Sandbox Code Playgroud)
有没有办法在不降版本的情况下修复它?或者甚至是从这个函数创建一堆列的更有效的方法?
我想到用 lru-cache 缓存计算结果,但由于 Polars 使用多处理,如果有帮助的话,我不知道。