-
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.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
|
||
from mqboost.base import XdataLike, YdataLike | ||
Check failure on line 5 in tests/test_encoder.py GitHub Actions / Flake8tests/test_encoder.py#L5
|
||
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() |