Для примера: самый короткий способ, которым я мог бы выяснить выполнение softmax над ленивым кадром данных, заключается в следующем:
Код: Выделить всё
import polars as pl
df = pl.LazyFrame({'a': [1,2,3,4,5,6,7,8,9,10], 'b':[5,5,5,5,5,5,5,5,5,5], 'c': [10,9,8,7,6,5,4,3,2,1]})
cols = ['a','b','c']
df = df.with_columns([ pl.col(c).exp().alias(c) for c in cols]) # Exp all columns
df = df.with_columns(pl.sum_horizontal(cols).alias('sum')) # Get row sum of exps
df = df.with_columns([ (pl.col(c)/pl.col('sum')).alias(c) for c in cols ]).drop('sum')
df.collect()
Код: Выделить всё
shape: (10, 3)
┌──────────┬──────────┬──────────┐
│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 │
╞══════════╪══════════╪══════════╡
│ 0.000123 ┆ 0.006692 ┆ 0.993185 │
│ 0.000895 ┆ 0.01797 ┆ 0.981135 │
│ 0.006377 ┆ 0.047123 ┆ 0.946499 │
│ 0.04201 ┆ 0.114195 ┆ 0.843795 │
│ 0.211942 ┆ 0.211942 ┆ 0.576117 │
│ 0.576117 ┆ 0.211942 ┆ 0.211942 │
│ 0.843795 ┆ 0.114195 ┆ 0.04201 │
│ 0.946499 ┆ 0.047123 ┆ 0.006377 │
│ 0.981135 ┆ 0.01797 ┆ 0.000895 │
│ 0.993185 ┆ 0.006692 ┆ 0.000123 │
└──────────┴──────────┴──────────┘
Подробнее здесь: https://stackoverflow.com/questions/794 ... xpressions