File size: 5,342 Bytes
0a4a484
 
 
fad9d72
0a4a484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fad9d72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a4a484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fad9d72
0a4a484
 
 
 
7fa960b
 
0a4a484
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import pandas as pd
import numpy as np
def data_imp():
    insurance_feature_descriptions = {
    "CustID": "Unique identifier for each customer.",
    "FirstPolYear": "Year when the customer first bought an insurance policy.",
    "BirthYear": "Birth year of the customer, used to calculate age.",
    "EducDeg": "Highest educational degree obtained by the customer.",
    "MonthSal": "Monthly salary of the customer. (Numerical, float64)",
    "GeoLivArea": "Geographical area where the customer lives.",
    "Children": "Number of children the customer has.",
    "CustMonVal": "Total monetary value of the customer to the company.",
    "ClaimsRate": "Rate at which the customer files insurance claims.",
    "PremMotor": "Premium amount for motor insurance.",
    "PremHousehold": "Premium amount for household insurance.",
    "PremHealth": "Premium amount for health insurance.",
    "PremLife": "Premium amount for life insurance.",
    "PremWork": "Premium amount for work insurance."
    }
    retail_feature_descriptions = {
    "Channel": "Indicates the sales channel through which the customer made purchases.",
    "Region": "The geographical region where the customer is located.",
    "Fresh": "Annual spending (in monetary units) on fresh products.",
    "Milk": "Annual spending (in monetary units) on milk products.",
    "Grocery": "Annual spending (in monetary units) on grocery items.",
    "Frozen": "Annual spending (in monetary units) on frozen products.",
    "Detergents_Paper": "Annual spending (in monetary units) on detergents and paper products.",
    "Delicassen": "Annual spending (in monetary units) on delicatessen products."
    }
    bankng_feature_descriptions = {
    "CUST_ID": "Unique identifier for each customer.",
    "BALANCE": "The average balance left in the customer's account.",
    "BALANCE_FREQUENCY": "Frequency with which the balance is updated.",
    "PURCHASES": "The total amount of purchases made by the customer.",
    "ONEOFF_PURCHASES": "The total amount of one-time purchases made by the customer.",
    "INSTALLMENTS_PURCHASES": "The total amount of purchases made in installments.",
    "CASH_ADVANCE": "The total amount of cash advances taken by the customer.",
    "PURCHASES_FREQUENCY": "The frequency of purchases made by the customer.",
    "ONEOFF_PURCHASES_FREQUENCY": "The frequency of one-time purchases made by the customer.",
    "PURCHASES_INSTALLMENTS_FREQUENCY": "The frequency of purchases made in installments.",
    "CASH_ADVANCE_FREQUENCY": "The frequency of cash advances taken by the customer.",
    "CASH_ADVANCE_TRX": "The number of cash advance transactions made by the customer.",
    "PURCHASES_TRX": "The number of purchase transactions made by the customer.",
    "CREDIT_LIMIT": "The credit limit assigned to the customer's account.",
    "PAYMENTS": "The total amount of payments made by the customer.",
    "MINIMUM_PAYMENTS": "The minimum amount of payments made by the customer.",
    "PRC_FULL_PAYMENT": "The percentage of full payments made by the customer.",
    "TENURE": "The tenure of the customer in months."
    }

    insurance_defaults = {
                "FirstPolYear": 1999,
                "BirthYear": 1980,
                "MonthSal": 1000,
                "GeoLivArea": 0,  # Options: 0, 1, 2, 3
                "Children": 0,  # Options: 0, 1, 2
                "CustMonVal": 100,
                "ClaimsRate": 2.33,
                "PremMotor": 200,
                "PremHousehold": 200,
                "PremHealth": 200,
                "PremLife": 200,
                "PremWork": 200
            }

            # Define default values for banking dataset features
    banking_defaults = {
                "BALANCE": 2000,
                "BALANCE_FREQUENCY": 0.5,
                "PURCHASES": 500,
                "ONEOFF_PURCHASES": 0,
                "INSTALLMENTS_PURCHASES": 0,
                "CASH_ADVANCE": 200,
                "PURCHASES_FREQUENCY": 0.1,
                "ONEOFF_PURCHASES_FREQUENCY": 0.1,
                "PURCHASES_INSTALLMENTS_FREQUENCY": 0.5,
                "CASH_ADVANCE_FREQUENCY": 5,
                "CASH_ADVANCE_TRX": 5,
                "PURCHASES_TRX": 5,
                "CREDIT_LIMIT": 10000,
                "PAYMENTS": 500,
                "MINIMUM_PAYMENTS": 130,
                "PRC_FULL_PAYMENT": 0.22,
                "TENURE": 10
            }

            # Define default values for retail dataset features
    retail_defaults = {
                "Fresh": 6000,
                "Milk": 9000,
                "Grocery": 9000,
                "Frozen": 4000,
                "Detergents_Paper": 4000,
                "Delicassen": 2000
            }
    return insurance_feature_descriptions,bankng_feature_descriptions,retail_feature_descriptions,insurance_defaults,banking_defaults,retail_defaults

def preprocess_data(data):
    if 'CustID' in data.columns:
        data = data.drop(columns=['CustID'])
    if 'Cust_ID' in data.columns:
        data = data.drop(columns=['Cust_ID'])
    data = remove_outliers(data)
    return data

def remove_outliers(df, threshold=3):
    df_numeric = df.select_dtypes(include=[float, int])
    z_scores = np.abs((df_numeric - df_numeric.mean()) / df_numeric.std())
    df_clean = df[(z_scores < threshold).all(axis=1)]
    return df_clean