pymer4.expressions#

Custom polars expressions useable inside of any polars context and compatible with other polars selectors.

Examples

Center mpg and zscore disp by passing in their names as you would to pl.col()

from pymer4.expressions import center, zscore, scale

mtcars.with_columns(
    center('mpg').alias('mpg_centered'), 
    zscore('disp').alias('disp_zscore')
    ).head()
shape: (5, 5)
mpgdispcylmpg_centereddisp_zscore
f64f64i64f64f64
21.0160.060.909375-0.57062
21.0160.060.909375-0.57062
22.8108.042.709375-0.990182
21.4258.061.3093750.220094
18.7360.08-1.3906251.043081

Normalize all columns by dividing by their standard deviations using a polars selector like pl.all()

mtcars.with_columns(
    # Normalize all columns
    scale(pl.all()).name.suffix('_scaled')
    ).head()
shape: (5, 6)
mpgdispcylmpg_scaleddisp_scaledcyl_scaled
f64f64i64f64f64f64
21.0160.063.4843511.2909613.35961
21.0160.063.4843511.2909613.35961
22.8108.043.7830090.8713992.23974
21.4258.063.5507192.0816743.35961
18.7360.083.1027312.9046624.47948

Expression are extensible .over('column_name')expressions.

For example, we can z-score mpg over all rows or by sub-groups of cyl

mtcars.with_columns(
    zscore('mpg').alias('mpg_zscore'),
    zscore('mpg').over('cyl').alias('mpg_zscore_by_cyl')
    ).head()
shape: (5, 5)
mpgdispcylmpg_zscorempg_zscore_by_cyl
f64f64i64f64f64
21.0160.060.1508850.864867
21.0160.060.1508850.864867
22.8108.040.449543-0.856715
21.4258.060.2172531.140053
18.7360.08-0.2307351.406224

pymer4.expressions.center(expr: Expr | str) Expr[source]#

Mean center the values in the expression

Parameters:

expr (Expr | str) – The expression to center

pymer4.expressions.logit2odds(expr: Expr | str) Expr[source]#

Convert logits to log-odds

Parameters:

expr (Expr | str) – The expression to convert

pymer4.expressions.logit2prob(expr: Expr | str) Expr[source]#

Convert logits to probabilities

Parameters:

expr (Expr | str) – The expression to convert

pymer4.expressions.rank(expr: Expr | str, method='average', descending=False) Expr[source]#

Rank the values in the expression

Parameters:
  • expr (Expr | str) – The expression to rank

  • method (str, optional) – The method to use for ranking. Defaults to “average”.

  • descending (bool, optional) – Whether to rank in descending order. Defaults to False.

pymer4.expressions.scale(expr: Expr | str) Expr[source]#

Scale the values in the expression by their standard deviation

Parameters:

expr (Expr | str) – The expression to scale

pymer4.expressions.zscore(expr: Expr | str) Expr[source]#

Z-score the values in the expression

Parameters:

expr (Expr | str) – The expression to z-score