forked from luoling1993/JDD_Census
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathts_features_engineering.py
202 lines (151 loc) · 7.42 KB
/
ts_features_engineering.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import datetime
import gc
import os
import warnings
import numpy as np
import pandas as pd
warnings.filterwarnings('ignore')
BASE_PATH = os.path.join(os.path.dirname(__file__), "data")
RAW_DATA_PATH = os.path.join(BASE_PATH, "RawData")
ETL_DATA_PATH = os.path.join(BASE_PATH, "EtlData")
def get_train_data(name):
if name not in ['flow', 'transition']:
raise ValueError('name should be `flow` or `transition`, but get `{}`'.format(name))
raw_data_name = os.path.join(RAW_DATA_PATH, '{}_train.csv'.format(name))
etl_data_name = os.path.join(ETL_DATA_PATH, 'arima_{}_train.csv'.format(name))
raw_data = pd.read_csv(raw_data_name)
etl_data = pd.read_csv(etl_data_name)
train_data = pd.concat([raw_data, etl_data], axis=0, ignore_index=True)
return train_data
class FlowProcessing(object):
@staticmethod
def _get_days_week(item):
item = str(item)
item_date = datetime.datetime.strptime(item, '%Y%m%d')
days_week = item_date.weekday()
return days_week
@staticmethod
def _get_stats_item(stats_dict):
stats_items_list = list()
date_dt = stats_dict['date_dt']
for key, item in stats_dict['stats'].items():
stats_item_list = list()
stats_item_list.append(date_dt) # date_dt
stats_item_list.append(key) # district_code
stats_item_list.append(item['days_1'][0]) # last value
# days_7 max
days_7_max = max(item['days_7'])
stats_item_list.append(days_7_max)
# days_7 min
days_7_min = min(item['days_7'])
stats_item_list.append(days_7_min)
# days_7 mean
days_7_mean = np.mean(item['days_7'])
stats_item_list.append(days_7_mean)
# days_15 max
days_15_max = max(item['days_15'])
stats_item_list.append(days_15_max)
# days_15 min
days_15_min = min(item['days_15'])
stats_item_list.append(days_15_min)
# days_15 mean
days_15_mean = np.mean(item['days_15'])
stats_item_list.append(days_15_mean)
# days_30 max
days_30_max = max(item['days_30'])
stats_item_list.append(days_30_max)
# days_30 min
days_30_min = min(item['days_30'])
stats_item_list.append(days_30_min)
# days_30 mean
days_30_mean = np.mean(item['days_30'])
stats_item_list.append(days_30_mean)
stats_items_list.append(stats_item_list)
return stats_items_list
@staticmethod
def _update_stats_dict(stats_dict, district_code, column_item):
if district_code not in stats_dict['stats'].keys():
stats_dict['stats'][district_code] = dict(days_1=list(), days_7=list(),
days_15=list(), days_30=list())
if len(stats_dict['stats'][district_code]['days_1']) == 1:
stats_dict['stats'][district_code]['days_1'].pop(0)
stats_dict['stats'][district_code]['days_1'].append(column_item)
else:
stats_dict['stats'][district_code]['days_1'].append(column_item)
if len(stats_dict['stats'][district_code]['days_7']) == 7:
stats_dict['stats'][district_code]['days_7'].pop(0)
stats_dict['stats'][district_code]['days_7'].append(column_item)
else:
stats_dict['stats'][district_code]['days_7'].append(column_item)
if len(stats_dict['stats'][district_code]['days_15']) == 15:
stats_dict['stats'][district_code]['days_15'].pop(0)
stats_dict['stats'][district_code]['days_15'].append(column_item)
else:
stats_dict['stats'][district_code]['days_15'].append(column_item)
if len(stats_dict['stats'][district_code]['days_30']) == 30:
stats_dict['stats'][district_code]['days_30'].pop(0)
stats_dict['stats'][district_code]['days_30'].append(column_item)
else:
stats_dict['stats'][district_code]['days_30'].append(column_item)
return stats_dict
def _get_stats_df(self, df, column):
if column not in ['dwell', 'flow_in', 'flow_out']:
raise ValueError()
init_date = 20170701 # int is enough
stats_dict = dict()
stats_list = list()
stats_df_columns = ['date_dt', 'district_code', 'days_1', 'days_7_max', 'days_7_min', 'days_7_mean',
'days_15_max', 'days_15_min', 'days_15_mean', 'days_30_max', 'days_30_min', 'days_30_mean']
df = df.copy()
for _, item in df.iterrows():
date_dt = item['date_dt']
district_code = item['district_code']
column_item = item[column]
if 'date_dt' not in stats_dict.keys():
stats_dict['date_dt'] = date_dt
stats_dict['stats'] = dict()
if date_dt != stats_dict['date_dt']:
stats_dict = self._update_stats_dict(stats_dict, district_code, column_item)
stats_dict['date_dt'] = date_dt
if date_dt < init_date:
continue
stats_items_list = self._get_stats_item(stats_dict)
stats_list.extend(stats_items_list)
else:
stats_dict = self._update_stats_dict(stats_dict, district_code, column_item)
stats_df = pd.DataFrame(data=stats_list, columns=stats_df_columns)
return stats_df
def processing(self):
flow_df = get_train_data(name='flow')
flow_df['days_week'] = flow_df['date_dt'].apply(self._get_days_week)
flow_df['city_code_copy'] = flow_df['city_code']
flow_df['district_code_copy'] = flow_df['district_code']
flow_df = pd.get_dummies(flow_df, columns=['days_week', 'city_code_copy', 'district_code_copy'])
dwell_df = flow_df.copy()
dwell_df = dwell_df.drop(columns=['flow_in', 'flow_out'])
flow_in_df = flow_df.copy()
flow_in_df = flow_in_df.drop(columns=['dwell', 'flow_out'])
flow_out_df = flow_df.copy()
flow_out_df = flow_out_df.drop(columns=['dwell', 'flow_in'])
del flow_df
gc.collect()
dwell_stats_df = self._get_stats_df(dwell_df, column='dwell')
dwell_df = pd.merge(dwell_df, dwell_stats_df, how='left', on=['date_dt', 'district_code'])
dwell_df = dwell_df[dwell_df['date_dt'] >= 20170701]
dwell_df_name = os.path.join(ETL_DATA_PATH, 'dwell_features.csv')
dwell_df.to_csv(dwell_df_name, index=False)
flow_in_stats_df = self._get_stats_df(flow_in_df, column='flow_in')
flow_in_df = pd.merge(flow_in_df, flow_in_stats_df, how='left', on=['date_dt', 'district_code'])
flow_in_df = flow_in_df[flow_in_df['date_dt'] >= 20170701]
flow_in_df_name = os.path.join(ETL_DATA_PATH, 'flow_in_features.csv')
flow_in_df.to_csv(flow_in_df_name, index=False)
flow_out_stats_df = self._get_stats_df(flow_out_df, column='flow_out')
flow_out_df = pd.merge(flow_out_df, flow_out_stats_df, how='left', on=['date_dt', 'district_code'])
flow_out_df = flow_out_df[flow_out_df['date_dt'] >= 20170701]
flow_out_df_name = os.path.join(ETL_DATA_PATH, 'flow_out_features.csv')
flow_out_df.to_csv(flow_out_df_name, index=False)
if __name__ == '__main__':
flow_processing = FlowProcessing()
flow_processing.processing()