shortcuts/expr.ts

20 lines
556 B
TypeScript
Raw Normal View History

2024-09-25 14:31:53 +00:00
import pl from "npm:nodejs-polars";
2024-09-25 11:19:01 +00:00
export function residuals(x: pl.Expr, y: pl.Expr): pl.Expr {
2024-09-25 14:31:53 +00:00
const xM = x.minus(x.mean());
const yM = y.minus(y.mean());
const xMSQ = xM.pow(2);
const beta = xM.dot(yM).div(xMSQ.sum());
return yM.minus(beta.mul(xM));
}
2024-09-26 01:41:33 +00:00
2024-09-26 04:07:37 +00:00
export const fillzero = (
value = 0.0001,
) => (pl.all().replaceStrict(0, value, pl.all()));
2024-09-27 11:20:19 +00:00
export const minmaxScale = (col: pl.Expr) =>
(col.minus(col.min())).div(col.max().minus(col.min()));
export const standardScale = (col: pl.Expr) =>
(col.minus(col.mean())).div(col.std());