# -*- coding: utf-8 -*-
"""
@author: yq
@time: 2024/11/1
@desc: 
"""

import pandas as pd

from commom import f_format_float


class DataFeatureEntity():
    """
    数据特征准备完毕
    """

    def __init__(self, data_x: pd.DataFrame, data_y: pd.Series):
        self._data_x = data_x
        self._data_y = data_y

    @property
    def x_columns(self):
        return self._data_x.columns.tolist()

    @property
    def data_x(self):
        return self._data_x

    @property
    def data_y(self):
        return self._data_y

    def get_odds0(self):
        train_good_len = len(self._data_y[self._data_y == 0])
        train_bad_len = len(self._data_y[self._data_y == 1])
        odds0 = train_bad_len / train_good_len
        return odds0


class DataSplitEntity():
    """
    初始数据训练集测试集划分
    """

    def __init__(self, train_data: pd.DataFrame, test_data: pd.DataFrame):
        self._train_data = train_data
        self._test_data = test_data
        self._data = pd.concat((train_data, test_data))

    @property
    def data(self) -> pd.DataFrame:
        return self._data

    @property
    def train_data(self) -> pd.DataFrame:
        return self._train_data

    @property
    def test_data(self) -> pd.DataFrame:
        return self._test_data

    def get_distribution(self, y_column) -> pd.DataFrame:
        df = pd.DataFrame()

        train_data_len = len(self._train_data)
        train_bad_len = len(self._train_data[self._train_data[y_column] == 1])
        train_bad_rate = f"{f_format_float(train_bad_len / train_data_len * 100, 2)}%"

        test_data_len = len(self._test_data)
        test_bad_len = len(self._test_data[self._test_data[y_column] == 1])
        test_bad_rate = f"{f_format_float(test_bad_len / test_data_len * 100, 2)}%"

        total = train_data_len + test_data_len
        bad_total = train_bad_len + test_bad_len
        bad_rate = f"{f_format_float(bad_total / total * 100, 2)}%"

        df["样本"] = ["训练集", "测试集", "合计"]
        df["样本数"] = [train_data_len, test_data_len, total]
        df["样本占比"] = [f"{f_format_float(train_data_len / total * 100, 2)}%",
                      f"{f_format_float(test_data_len / total * 100, 2)}%", "100%"]
        df["坏样本数"] = [train_bad_len, test_bad_len, bad_total]
        df["坏样本比例"] = [train_bad_rate, test_bad_rate, bad_rate]

        return df


if __name__ == "__main__":
    pass
