File size: 2,507 Bytes
6752520
 
725d1d2
 
 
 
 
 
 
6752520
725d1d2
 
 
 
 
 
 
6752520
 
725d1d2
 
6752520
725d1d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6752520
725d1d2
 
6752520
725d1d2
 
 
 
 
6752520
725d1d2
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from api.model import ModelService
from api.schema.input_data import InputData


def preprocess_input_data(input_data: InputData, model_service: ModelService) -> dict:
    personal = input_data.personal_info
    financial = input_data.financial_info
    contact = input_data.contact_info
    campaign = input_data.campaign_info
    macro = input_data.macro_info  # <-- macro info

    model_input = {
        "default": financial.default,
        "housing": financial.housing,
        "loan": financial.loan,
        "campaign": campaign.campaign,
        "previous": campaign.previous,
        "macro_trend": macro.emp_var_rate * macro.euribor3m,
        "econ_health": macro.cons_conf_idx * macro.nr_employed,
    }

    # One-hot encoding kategori
    model_input["marital_married"] = 1 if personal.marital == "married" else 0
    model_input["marital_single"] = 1 if personal.marital == "single" else 0

    model_input["contact_cellular"] = 1 if contact.contact == "cellular" else 0
    model_input["contact_telephone"] = 1 if contact.contact == "telephone" else 0

    model_input["poutcome_failure"] = 1 if campaign.poutcome == "failure" else 0
    model_input["poutcome_nonexistent"] = 1 if campaign.poutcome == "nonexistent" else 0
    model_input["poutcome_success"] = 1 if campaign.poutcome == "success" else 0

    for m in ["apr", "aug", "jul", "jun", "mar", "may", "nov", "oct", "sep"]:
        model_input[f"month_{m}"] = 1 if contact.month == m else 0

    edu_types = [
        "basic.4y",
        "basic.6y",
        "basic.9y",
        "high.school",
        "illiterate",
        "professional.course",
        "university.degree",
    ]
    for edu in edu_types:
        model_input[f"education_{edu}"] = 1 if personal.education_str == edu else 0

    for j in ["cat1", "cat2", "cat3", "cat4", "other"]:
        model_input[f"job_categories_{j}"] = 1 if personal.job_category == j else 0

    for a in ["struggling", "stable", "about to retire", "old age", "counting a last breathe"]:
        model_input[f"age_categories_{a}"] = 1 if personal.age_category == a else 0

    for d in ["mon", "thu", "tue", "wed", "fri"]:
        model_input[f"day_of_week_{d}"] = 1 if contact.day_of_week == d else 0

    # Tambahkan kolom yang model harapkan tapi tidak ada
    if model_service.expected_features is not None:
        for c in model_service.expected_features:
            model_input.setdefault(c, 0)

    return model_input