Dùng kiểu dữ liệu Categorical và Enum

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Liam Brannigan

Data Scientist & Polars Contributor

Mã hóa chuỗi lặp lại

Sơ đồ bảng với cột tiêu đề sự kiện và cột khu vực có chuỗi lặp lại.

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Mã hóa chuỗi lặp lại

Sơ đồ bảng với cột tiêu đề sự kiện và cột khu vực, trong đó chuỗi được mã hóa bằng số nguyên.

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tập dữ liệu có nhãn lặp lại

events = pl.read_parquet("chicago_events.parquet")
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tập dữ liệu có nhãn lặp lại

events["area"].value_counts(sort=True).head(5)
shape: (5, 2)
| area          | count |
| ---           | ---   |
| str           | u32   |
|-------------  |-------|
| Loop          | 35982 |
| West Loop     | 27432 |
| Andersonville | 24660 |
| Downtown      | 24320 |
| Pullman       | 21985 |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tạo cột Categorical

events_cat = events.with_columns(
    pl.col("area").cast(pl.Categorical)
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tạo cột Categorical

events_cat.select("event_title","area").head()
shape: (5, 2)
| event_title        | area          |
| ---                | ---           |
| str                | cat           |
|--------------------|---------------|
| Folk Festival      | Andersonville |
| Fireworks Night    | Downtown      |
| Greektown Market   | West Side     |
| Rail History Day   | Pullman       |
| Grant Park Concert | Loop          |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tạo cột Categorical

events_cat.select("event_title","area").with_columns(
    pl.col("area").to_physical().alias("physical")
).head(3)
shape: (3, 3)
| event_title        | area          | physical |
| ---                | ---           | ---      |
| str                | cat           | u32      |
|--------------------|---------------|----------|
| Folk Festival      | Andersonville | 0        |
| Fireworks Night    | Downtown      | 1        |
| Greektown Market   | West Side     | 2        |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Dùng biểu thức cho Categorical

events_cat.select("event_title","area",
    pl.col("area").cat.starts_with("West").alias("westside"),
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Dùng biểu thức cho Categorical

events_cat.select("event_title","area",
    pl.col("area").cat.starts_with("West").alias("westside"),
).filter(pl.col("westside")).head(2)
shape: (2, 3)
| event_title             | area      | westside |
| ---                     | ---       | ---      |
| str                     | cat       | bool     |
|-------------------------|-----------|----------|
| Greektown Market        | West Side | true     |
| West Loop Chef Showcase | West Loop | true     |
  • Các thao tác chuỗi khác → ép về String
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Categorical và Enum

Sơ đồ bảng với cột tiêu đề sự kiện và cột khu vực, trong đó chuỗi được mã hóa bằng số nguyên.

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tạo cột Enum

area_enum = pl.Enum([
    "Albany Park",
    "Andersonville",
    ...
    "West Loop",
    "West Side",
    "Wicker Park",
])
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tạo cột Enum

events_enum = events.with_columns(
    pl.col("area").cast(area_enum)
)
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Tạo cột Enum

events_enum.select("event_title", "area").head()
shape: (5, 2)
| event_title                  | area           |
| ---                          | ---            |
| str                          | enum           |
|------------------------------|----------------|
| Greektown Market             | West Side      |
| Folk Festival                | Andersonville  |
| West Loop Chef Showcase      | West Loop      |
| Rail History Day             | Pullman        |
| Fireworks Night              | Downtown       |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Kiểm tra dtype Enum

events_enum.schema["area"]
Enum(categories=['Albany Park', 'Andersonville', 'Chinatown', 'Downtown', ...])
  • Enum cho từ vựng cố định
  • Categorical cho từ vựng thay đổi
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Hiệu quả bộ nhớ của Enum

events_enum.select("event_title","area").with_columns(
    pl.col("area").to_physical().alias("physical")
).head(3)
shape: (3, 3)
| event_title        | area          | physical |
| ---                | ---           | ---      |
| str                | enum          | u8       |
|--------------------|---------------|----------|
| Folk Festival      | Andersonville | 1        |
| Fireworks Night    | Downtown      | 3        |
| Greektown Market   | West Side     | 16       |
Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

¡Vamos a practicar!

Mở rộng và tối ưu hóa pipeline dữ liệu với Polars

Preparing Video For Download...