-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] add categorical encoder for dataset (#32)
- Loading branch information
Showing
7 changed files
with
122 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import numpy as np | ||
from sklearn.preprocessing import LabelEncoder | ||
|
||
from mqboost.base import XdataLike | ||
|
||
|
||
class MQLabelEncoder: | ||
def __init__(self) -> None: | ||
self.label_encoder = LabelEncoder() | ||
|
||
def fit(self, series: XdataLike) -> None: | ||
self.label_encoder.fit(list(series[~series.isna()]) + ["Unseen", "NaN"]) | ||
|
||
def transform(self, series: XdataLike) -> XdataLike: | ||
return self.label_encoder.transform( | ||
np.select( | ||
[series.isna(), ~series.isin(self.label_encoder.classes_)], | ||
["NaN", "Unseen"], | ||
series, | ||
) | ||
) | ||
|
||
def fit_transform(self, series: XdataLike) -> XdataLike: | ||
self.fit(series=series) | ||
return self.transform(series=series) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
from mqboost.encoder import MQLabelEncoder | ||
|
||
|
||
# Test data for categorical variables | ||
@pytest.fixture | ||
def sample_data(): | ||
return pd.Series(["apple", "banana", "orange", None, "kiwi", np.nan]) | ||
|
||
|
||
# Test data for label encoding | ||
@pytest.fixture | ||
def sample_label_data(): | ||
return np.array([2, 3, 5, 0, 4, 0]) | ||
|
||
|
||
def test_fit_transform(sample_data): | ||
encoder = MQLabelEncoder() | ||
transformed = encoder.fit_transform(sample_data) | ||
|
||
# Check that the transformed result is numeric | ||
assert transformed is not None | ||
assert transformed.dtype == int | ||
assert len(transformed) == len(sample_data) | ||
|
||
|
||
def test_unseen_and_nan_values(sample_data): | ||
encoder = MQLabelEncoder() | ||
encoder.fit(sample_data) | ||
|
||
# Include new unseen value and check behavior | ||
test_data = pd.Series(["apple", "unknown", None, "melon", np.nan]) | ||
transformed = encoder.transform(test_data) | ||
|
||
# Check for correct handling of unseen and NaN values | ||
assert ( | ||
transformed | ||
== encoder.label_encoder.transform(["apple", "Unseen", "NaN", "Unseen", "NaN"]) | ||
).all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41670da
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.