项目文件夹

文件
wehub-resource-sync 593b94c120
pytest / Unit Tests (push) Has been cancelled
pytest / Integration (integration_tests_a) (push) Has been cancelled
pytest / Integration (integration_tests_b) (push) Has been cancelled
pytest / Integration (integration_tests_c) (push) Has been cancelled
pytest / Integration (integration_tests_d) (push) Has been cancelled
pytest / Integration (integration_tests_e) (push) Has been cancelled
pytest / Integration (integration_tests_f) (push) Has been cancelled
pytest / Integration (integration_tests_g) (push) Has been cancelled
pytest / Integration (integration_tests_h) (push) Has been cancelled
pytest / Integration (integration_tests_i) (push) Has been cancelled
pytest / Integration (integration_tests_j) (push) Has been cancelled
pytest / Distributed (distributed_a) (push) Has been cancelled
pytest / Distributed (distributed_b) (push) Has been cancelled
pytest / Distributed (distributed_c) (push) Has been cancelled
pytest / Distributed (distributed_d) (push) Has been cancelled
pytest / Distributed (distributed_e) (push) Has been cancelled
pytest / Distributed (distributed_f) (push) Has been cancelled
pytest / Minimal Install (push) Has been cancelled
pytest / Event File (push) Has been cancelled
pytest (slow) / py-slow (push) Has been cancelled
Publish JSON Schema / publish-schema (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:49:20 +08:00

52 行
1.9 KiB
Python

import numpy as np
import pandas as pd
import pytest
from ludwig.backend.base import LocalBackend
from ludwig.constants import BALANCE_PERCENTAGE_TOLERANCE, NAME
from ludwig.data.preprocessing import balance_data
@pytest.mark.parametrize(
"method, balance",
[
("oversample_minority", 0.25),
("oversample_minority", 0.5),
("oversample_minority", 0.75),
("undersample_majority", 0.25),
("undersample_majority", 0.5),
("undersample_majority", 0.75),
("undersample_majority", 0.9),
],
)
def test_balance(method, balance):
config = {
"input_features": [
{"name": "Index", "proc_column": "Index", "type": "number"},
{"name": "random_1", "proc_column": "random_1", "type": "number"},
{"name": "random_2", "proc_column": "random_2", "type": "number"},
],
"output_features": [{"name": "Label", "proc_column": "Label", "type": "binary"}],
"preprocessing": {"oversample_minority": None, "undersample_majority": None},
}
input_df = pd.DataFrame(
{
"Index": np.arange(0, 200, 1),
"random_1": np.random.randint(0, 50, 200),
"random_2": np.random.choice(["Type A", "Type B", "Type C", "Type D"], 200),
"Label": np.concatenate((np.zeros(180), np.ones(20))),
"split": np.zeros(200),
}
)
config["preprocessing"][method] = balance
backend = LocalBackend()
test_df = balance_data(input_df, config["output_features"], config["preprocessing"], backend, 42)
target = config["output_features"][0][NAME]
majority_class = test_df[target].value_counts()[test_df[target].value_counts().idxmax()]
minority_class = test_df[target].value_counts()[test_df[target].value_counts().idxmin()]
new_class_balance = round(minority_class / majority_class, 2)
assert abs(balance - new_class_balance) < BALANCE_PERCENTAGE_TOLERANCE