-
Notifications
You must be signed in to change notification settings - Fork 0
/
finance_model.py
186 lines (143 loc) · 6.98 KB
/
finance_model.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import numpy as np
import pandas as pd
from catboost import CatBoostRegressor, Pool
from timeseries_train import train_models, predict
def preprocess_contributors(contributors):
contributors['accnt_pnsn_schm'] = contributors['accnt_pnsn_schm'].astype(float).fillna(
contributors['accnt_pnsn_schm'].mode()[0]
).astype(int).astype('category')
blnc_filter = (((contributors['npo_blnc'].isna()) | (contributors['npo_blnc'] < 0)) & (contributors['accnt_pnsn_schm'] == 1))
contributors.loc[blnc_filter, 'npo_blnc'] = contributors.loc[blnc_filter, 'npo_pmnts_sum'].fillna(0)
contributors.loc[contributors['npo_accnt_status'] == 0, 'npo_blnc'] = np.nan
contributors.loc[contributors['npo_pmnts_sum'] < 0, 'npo_pmnts_sum'] = np.nan
contributors.loc[contributors.npo_ttl_incm < 0, 'npo_ttl_incm'] = 0
contributors['npo_ttl_incm'] = contributors.npo_ttl_incm.fillna(0)
contributors.dropna(subset=['npo_pmnts_sum'], inplace=True)
contributors.reset_index(drop=True, inplace=True)
return contributors
def extract_features_table_from_contributors(contributors):
processed_contributors = contributors.copy()
processed_contributors['date'] = pd.to_datetime(processed_contributors['npo_accnt_status_date']).dt.to_period('Q')
processed_contributors['npo_accnt_status_date'] = pd.to_datetime(processed_contributors['npo_accnt_status_date'])
processed_contributors['npo_lst_pmnt_date'] = pd.to_datetime(processed_contributors['npo_lst_pmnt_date'])
processed_contributors['npo_frst_pmnt_date'] = pd.to_datetime(processed_contributors['npo_frst_pmnt_date'])
features = processed_contributors.groupby(["date"]).agg(
{
'npo_accnt_id' : 'nunique',
'npo_pmnts_sum' : 'sum',
'npo_pmnts_nmbr' : 'sum',
'npo_ttl_incm' : 'sum',
}
)
features.columns = [
'number_acounts',
'payments_sum',
'payments_count',
'total_income'
]
return features.reset_index().sort_values(by='date', ascending=False)
def get_external_data(path_to_external_data):
external_data = pd.read_feather(path_to_external_data)
external_data.rename(columns={
'quarter' : 'date',
}, inplace=True)
return external_data
def get_target(path_to_target, external_data):
# external_data.rename(columns={
# 'quarter' : 'date',
# }, inplace=True)
target = pd.read_feather(path_to_target)
target.rename(columns={
'quarter' : 'date',
'paid_avg_correct' : 'mean_contribution',
'transactions_count' : 'count_contributions',
}, inplace=True)
target['date'] = target['date'].astype(str)
# external_data['date'] = external_data['date'].astype(str)
# target = pd.merge(target, external_data, on=['clnt_id', 'date'])
return target.drop(['paid_avg', 'seasonal'], axis=1)
def prepare_dates(contribution, transaction):
pass
class FeatureExtractor:
def __init__(self, path_to_data):
self.data = extract_features_table_from_contributors(path_to_data)
self.features_next_quarter = dict()
self.feature_columns = self.data.columns
result = dict()
for column in self.feature_columns:
if column != 'date':
ts = self.data[column]
result[column] = predict(ts, train_models(ts, 1))
self.features_next_quarter = result
def get_features_values_next_quarter(self):
return self.features_next_quarter
def get_feature_values(self, quarter):
return self.data[quarter]
def get_features(self):
return self.data
def model_factory(*args, **kwargs):
return Model(args, kwargs)
class Model:
def __init__(self,
path_to_external_data,
path_to_clients_table,
path_to_contributors_table,
path_to_target,
mode = True,
):
# self.data_train = pd.read_csv(path_to_external_data)
self.mode = mode
if self.mode:
self.contributors = preprocess_contributors(pd.read_csv(path_to_contributors_table))
self.clients = pd.read_csv(path_to_clients_table)
self.client_feature_extractor = FeatureExtractor(self.contributors)
self.internal_features = self.client_feature_extractor.get_features()
self.external_data = get_external_data(path_to_external_data)
self.target = get_target(path_to_target, self.external_data)
X_train, y_train_mean, y_train_count = self.get_train_data(self.clients)
else:
self.data = pd.read_feather('data/full_base.frt')
X_train = self.data.drop(['quarter', 'clnt_id', 'transactions_count', 'paid_avg_correct'], axis=1)
y_train_mean = self.data['paid_avg_correct']
y_train_count = self.data['transactions_count']
self.model_mean = CatBoostRegressor().fit(
X=X_train,
y=y_train_mean,
)
self.model_count = CatBoostRegressor().fit(
X=X_train,
y=y_train_count,
)
def get_train_data(self, clients):
self.internal_features['date'] = self.internal_features['date'].astype(str)
merged = pd.merge(self.target, self.internal_features, on='date')
all_merged = pd.merge(merged, clients, on='clnt_id')
y_train_mean = all_merged[all_merged['date'] != '2022Q2']['mean_contribution']
y_train_count = all_merged[all_merged['date'] != '2022Q2']['count_contributions']
X_train = all_merged[all_merged['date'] != '2022Q2'].drop(
[
'mean_contribution',
'count_contributions',
'date',
'pstl_code'
], axis=1)
return X_train, y_train_mean, y_train_count
def predict(self, client_id, override_features = dict()):
if self.mode:
features = self.client_feature_extractor.get_features_values_next_quarter()
features.update(override_features)
features = pd.concat(
[
# self.external_data[
# (self.external_data['clnt_id'] == client_id) &
# (self.external_data['date'] == '2023Q3')
# ].reset_index(drop=True),
pd.DataFrame(features).reset_index(drop=True),
self.clients[self.clients['clnt_id'] == client_id].reset_index(drop=True),
], axis=1)
if not features['clnt_id'][0]:
raise RuntimeError("ClientNotFound")
features = features.drop(['pstl_code'], axis=1)
else:
features = self.data[(self.data['clnt_id'] == client_id) & (self.data['quarter'] == '2023Q2')].drop(['quarter', 'clnt_id', 'transactions_count', 'paid_avg_correct'], axis=1)
return int(self.model_mean.predict(features.reset_index(drop=True))[0]), int(self.model_count.predict(features.reset_index(drop=True))[0])