-
Notifications
You must be signed in to change notification settings - Fork 0
/
churn_script_logging_and_tests.py
157 lines (139 loc) · 4.27 KB
/
churn_script_logging_and_tests.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
This is the Python Test for the churn_library.py module.
This module will be used to test
1. import_data
2. peform_eda
3. encode_data
4. perform_feature_engineering
5. train_test_model
Author: Sandeep Pandey
Date: 3-Mar-2023
"""
import os
import logging
# import churn_library_solution as cls
from churn_library import import_data, encoder_helper
logging.basicConfig(
filename='./logs/churn_library.log',
level=logging.INFO,
filemode='w',
format='%(name)s - %(levelname)s - %(message)s')
def test_import(i_data):
'''
test data import - this example is completed for you to assist with the other test functions
'''
try:
df_data = i_data("data/bank_data.csv")
logging.info("Testing import_data: SUCCESS")
except FileNotFoundError as err:
logging.error("Testing import_eda: The file wasn't found")
raise err
try:
assert df_data.shape[0] > 0
assert df_data.shape[1] > 0
except AssertionError as err:
logging.error(
"Testing import_data: The file doesn't appear to have rows and columns")
raise err
def test_eda():
'''
test perform eda function
'''
try:
assert os.path.isfile('images/eda/Churn_hist.png')
assert os.path.isfile('images/eda/Cust_age_hist.png')
assert os.path.isfile('images/eda/marital_bar.png')
assert os.path.isfile('images/eda/Total_Trans_Ct.png')
assert os.path.isfile('images/eda/corr_heatmap.png')
except AssertionError as err:
logging.error("Testing test_eda: all required images are not present.")
raise err
def test_encoder_helper(e_helper):
'''
test encoder helper
'''
df_data = import_data("data/bank_data.csv")
cat_columns_exist = [
'Gender',
'Education_Level',
'Marital_Status',
'Income_Category',
'Card_Category'
]
cat_columns = [
'Gender_Churn',
'Education_Level_Churn',
'Marital_Status_Churn',
'Income_Category_Churn',
'Card_Category_Churn']
df_data['Churn'] = df_data['Attrition_Flag'].apply(
lambda val: 0 if val == "Existing Customer" else 1)
df_news = e_helper(df_data, cat_columns_exist)
# print(df.columns)
for i in cat_columns:
try:
assert i in df_news
except AssertionError as err:
logging.error(
"Testing test_encoder_helper: all required columns are not presents")
raise err
def test_perform_feature_engineering():
'''
test perform_feature_engineering
'''
df_data = import_data("data/bank_data.csv")
cat_columns_exist = [
'Gender',
'Education_Level',
'Marital_Status',
'Income_Category',
'Card_Category'
]
df_data['Churn'] = df_data['Attrition_Flag'].apply(
lambda val: 0 if val == "Existing Customer" else 1)
df_news = encoder_helper(df_data, cat_columns_exist)
keep_cols = [
'Customer_Age',
'Dependent_count',
'Months_on_book',
'Total_Relationship_Count',
'Months_Inactive_12_mon',
'Contacts_Count_12_mon',
'Credit_Limit',
'Total_Revolving_Bal',
'Avg_Open_To_Buy',
'Total_Amt_Chng_Q4_Q1',
'Total_Trans_Amt',
'Total_Trans_Ct',
'Total_Ct_Chng_Q4_Q1',
'Avg_Utilization_Ratio',
'Gender_Churn',
'Education_Level_Churn',
'Marital_Status_Churn',
'Income_Category_Churn',
'Card_Category_Churn']
for i in keep_cols:
try:
assert i in df_news
except AssertionError as err:
logging.error(
"Testing test_perform_feature_engineering: all required columns are not present")
raise err
def test_train_models():
'''
test train_models
'''
try:
assert os.path.isfile('models/logistic_model.pkl')
assert os.path.isfile('models/rfc_model.pkl')
except AssertionError as err:
logging.error(
"Testing test_train_models: all required models are not present.")
raise err
if __name__ == "__main__":
test_import(import_data)
test_eda()
test_encoder_helper(encoder_helper)
test_perform_feature_engineering()
test_train_models()
# pass