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()
| mpg | disp | cyl | mpg_centered | disp_zscore |
|---|---|---|---|---|
| f64 | f64 | i64 | f64 | f64 |
| 21.0 | 160.0 | 6 | 0.909375 | -0.57062 |
| 21.0 | 160.0 | 6 | 0.909375 | -0.57062 |
| 22.8 | 108.0 | 4 | 2.709375 | -0.990182 |
| 21.4 | 258.0 | 6 | 1.309375 | 0.220094 |
| 18.7 | 360.0 | 8 | -1.390625 | 1.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()
| mpg | disp | cyl | mpg_scaled | disp_scaled | cyl_scaled |
|---|---|---|---|---|---|
| f64 | f64 | i64 | f64 | f64 | f64 |
| 21.0 | 160.0 | 6 | 3.484351 | 1.290961 | 3.35961 |
| 21.0 | 160.0 | 6 | 3.484351 | 1.290961 | 3.35961 |
| 22.8 | 108.0 | 4 | 3.783009 | 0.871399 | 2.23974 |
| 21.4 | 258.0 | 6 | 3.550719 | 2.081674 | 3.35961 |
| 18.7 | 360.0 | 8 | 3.102731 | 2.904662 | 4.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()
| mpg | disp | cyl | mpg_zscore | mpg_zscore_by_cyl |
|---|---|---|---|---|
| f64 | f64 | i64 | f64 | f64 |
| 21.0 | 160.0 | 6 | 0.150885 | 0.864867 |
| 21.0 | 160.0 | 6 | 0.150885 | 0.864867 |
| 22.8 | 108.0 | 4 | 0.449543 | -0.856715 |
| 21.4 | 258.0 | 6 | 0.217253 | 1.140053 |
| 18.7 | 360.0 | 8 | -0.230735 | 1.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.