Serializing Polars expressions as JSON or YAML file?

huangapple go评论83阅读模式
英文:

Serializing Polars expressions as JSON or YAML file?

问题

我对 Polars 表达式语法非常满意,以至于我的很多特征工程都用 Polars 表达式来表达。

但是,我现在正在尝试将特征工程移到 JSON 或 YAML 文件中(出于 MLOps 的原因)。

问题是 - 我该如何将其编码为 JSON 文件:

英文:

I am extremely happy with the polars expression syntax, so much so that a lot of my feature engineering is expressed in polars expressions.

However, I am now trying to move the feature engineering to JSON or YAML files (for MLOps reasons).

The question is - how could I encode this as a JSON file:

  1. configuration = {
  2. 'features': [
  3. pl.col('col1').fill_null(0).log().le(0.2).alias('feature1'),
  4. pl.col('col2').fill_null(0).log().le(0.2).alias('feature2'),
  5. pl.col('col3').fill_null(0).log().le(0.2).alias('feature3')
  6. ],
  7. 'filters': [
  8. pl.col('col4') >= 500_000,
  9. pl.col('col5').is_in(['A', 'B'])
  10. ]
  11. }
  12. # This is how I use it - just for context
  13. X = (df
  14. .filter(pl.all(configuration['filters']))
  15. .select(configuration['features'])
  16. )

Any ideas on how I could serialize (or re-write) this as JSON such that it could be converted back to Polars expressions?

Note that this question has a lot of overlap with https://stackoverflow.com/questions/74976313/possible-to-stringize-a-polars-expression, but it's not a duplicate.

答案1

得分: 4

polars >= 0.18.1开始,我们直接支持将表达式序列化为JSON并从JSON反序列化。

  1. def test_expression_json() -> None:
  2. # 创建一个表达式
  3. e = pl.col("foo").sum().over("bar")
  4. # 序列化为JSON
  5. json = e.meta.write_json()
  6. # 从JSON反序列化回表达式
  7. round_tripped = pl.Expr.from_json(json)
  8. # 断言表达式相等性
  9. assert round_tripped.meta == e
英文:

As of polars >= 0.18.1 we directly support serializing/deserializing expressions to and from json.

  1. def test_expression_json() -> None:
  2. # create an expression
  3. e = pl.col("foo").sum().over("bar")
  4. # serialize to json
  5. json = e.meta.write_json()
  6. # deserialize back to an expression
  7. round_tripped = pl.Expr.from_json(json)
  8. # assert expression equality
  9. assert round_tripped.meta == e

huangapple
  • 本文由 发表于 2023年6月5日 18:31:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定