Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Fixes: Dask Extra, OneHotEncode example, categorize_pandas tests #12

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions ibisml/transforms/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,29 @@


class OneHotEncode(Transform):
"""
A transformation class that applies one-hot encoding to specified categorical columns.

Examples
----------
>>> from ibis.interactive import *
>>> import ibisml as ml
>>> penguins = ex.penguins.fetch()
>>> recipe = ml.Recipe(ml.OneHotEncode("species"))
>>> recipe.fit(penguins).transform(penguins).table.select(s.startswith("species"))
┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓
┃ species_Adelie ┃ species_Chinstrap ┃ species_Gentoo ┃
┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩
│ int8 │ int8 │ int8 │
├────────────────┼───────────────────┼────────────────┤
│ 1 │ 0 │ 0 │
│ 1 │ 0 │ 0 │
│ 1 │ 0 │ 0 │
│ 1 │ 0 │ 0 │
│ … │ … │ … │
└────────────────┴───────────────────┴────────────────┘
"""

def __init__(self, categories: dict[str, list[Any]]):
self.categories = categories

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ dependencies = [
"ibis-framework"
]

[project.optional-dependencies]
dask = ["dask[dataframe]"]

[tool.setuptools]
include-package-data = false

Expand Down
39 changes: 39 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from textwrap import dedent

import ibis
import pandas as pd
import pyarrow as pa

import ibisml as ml
Expand Down Expand Up @@ -50,3 +51,41 @@ def test_transform_result_repr():
d int64
}"""
)


def test_categorize_pandas():
categories = {
"num_col": ml.core.Categories(pa.array([1, 2, 3, 4, 5]), ordered=True),
"str_col": ml.core.Categories(pa.array(["a", "b", "c"]), ordered=False),
}

df = pd.DataFrame(
{
"num_col": [
0,
1,
2,
-1,
4,
],
"str_col": [
0,
1,
2,
0,
-1,
],
}
)

t = ibis.table({"num_col": "int64", "str_col": "string"})
res = ml.TransformResult(t, categories=categories)

transformed_df = res._categorize_pandas(df)

assert isinstance(transformed_df["num_col"].dtype, pd.CategoricalDtype)
assert isinstance(transformed_df["str_col"].dtype, pd.CategoricalDtype)
assert transformed_df["num_col"].cat.ordered is True
assert transformed_df["str_col"].cat.ordered is False
assert transformed_df["num_col"].isna().sum() == 1
assert transformed_df["str_col"].isna().sum() == 1