-
Notifications
You must be signed in to change notification settings - Fork 2
/
cutting.py
259 lines (222 loc) · 7.95 KB
/
cutting.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
'''
Created on Aug 13, 2018
@author: fan
import panda.cutting as pd_cut
'''
import logging
logger = logging.getLogger(__name__)
import pyfan.amto.json.json as support_json
import pandas as pd
import numpy as np
# https://stackoverflow.com/questions/42217848/how-to-clip-pandas-dataframe-column-wise
def pd_winsorize_columnwise(df, winsor_coln_list,
coln_perc_cutoffs, return_type,
print_array=False, json_debug=False):
"""
Winsorizing column by column, no dependence across coluns.
Winsorize column by column
cols = 5
rows = 20
np.random.seed(123)
data = (np.random.rand(rows,cols)-0.5)*100
df = pd.DataFrame(data, columns=['col' + str(ctr) for ctr in range(cols)])
winsor_coln_list = ['col0', 'col1','col3','col4']
Parameters
----------
df: dataFrame
initial dataset
winsor_coln_list: list
list of column names to winsorize
['col0', 'col1','col3','col4']
coln_perc_cutoffs: dictionary
a nested dictionary where keys are elements of winsor_coln_list, and values
are a dictionary with min and max percentiles of winsorizing values.
if min is 0, do not create cutcolss
{'col0':{'q_ge':0,'q_le':0.9, 'v_ll':10},
'col1':{'q_ge':0.30,'v_le':50},
'col3':{'q_ge':0.01,'q_le':0.60, 'v_ll':40},
'col4':{'q_ge':0.01,'q_le':1, 'v_ll':33, 'v_gg':-5}}
return_type: string
'winsorize' or 'cutsubset'
"""
'''
Deep copy original dataframe
'''
condi_ctr = 0
for winsor_coln in winsor_coln_list:
col_perc_cutoff_dict = coln_perc_cutoffs[winsor_coln]
'''
Initialize cut min max
'''
winsorize_upper = np.Inf
winsorize_lower = np.NINF
'''
Loop over all min and max conditions, including value and percentile conditions
'''
for key, val in col_perc_cutoff_dict.items():
'''
Current data series
'''
df_col = df[winsor_coln]
'''
Obtain Thresholds
'''
if ('q_' in key):
'''
Quantiles
'''
threshold = df_col.quantile(val)
elif ('v_' in key):
'''
levels
'''
threshold = val
else:
raise('did not find _q quantile or _v value in key')
'''
Generate conditions
'''
if ('_l' in key):
winsorize_upper = np.minimum(winsorize_upper, threshold)
if ('_le' in key):
if_condi = (df_col <= threshold)
elif ('_ll' in key):
if_condi = (df_col < threshold)
else:
raise('key:'+ key + ', is invalid')
elif ('_g' in key):
winsorize_lower = np.maximum(winsorize_lower, threshold)
if ('_ge' in key):
if_condi = (df_col >= threshold)
elif ('_gg' in key):
if_condi = (df_col > threshold)
else:
raise('key:'+ key + ', is invalid')
else:
raise('key:'+ key + ', is invalid')
'''
Combine Conditions
'''
if (condi_ctr == 0):
if_condi_all = if_condi
else:
if_condi_all = if_condi_all & if_condi
condi_ctr = condi_ctr + 1
'''
Winsorize
'''
if(return_type == 'winsorize'):
df[winsor_coln] = df_col.clip(lower=winsorize_lower, upper=winsorize_upper)
'''
Cut to data subset
'''
if(return_type == 'cutsubset'):
df_return = df[if_condi_all]
if (print_array):
print(df_return)
if (json_debug):
support_json.jdump(df_return.to_dict(), 'conditions:' + str(if_condi_all),
logger=logger.debug, print_here = True)
return df_return
elif(return_type == 'winsorize'):
if (print_array):
print(df)
if (json_debug):
support_json.jdump(df.to_dict(), 'winsorized',
logger=logger.debug, print_here = True)
return df
else:
raise('wrong return_type')
def sample_run():
cols = 5
rows = 20
np.random.seed(123)
data = (np.random.rand(rows,cols)-0.5)*100
df = pd.DataFrame(data, columns=['col' + str(ctr) for ctr in range(cols)])
winsor_coln_list = ['col0', 'col1','col3','col4']
coln_perc_cutoffs = {'col0':{'q_ge':0,'q_le':0.9, 'v_ll':10},
'col1':{'q_ge':0.30,'v_le':50},
'col3':{'q_ge':0.01,'q_le':0.60, 'v_ll':40},
'col4':{'q_ge':0.01,'q_le':1, 'v_ll':33, 'v_gg':-5}}
return_type = 'winsorize'
# return_type = 'cutsubset'
'''
Deep copy original dataframe
'''
condi_ctr = 0
for winsor_coln in winsor_coln_list:
col_perc_cutoff_dict = coln_perc_cutoffs[winsor_coln]
'''
Initialize cut min max
'''
winsorize_upper = np.Inf
winsorize_lower = np.NINF
'''
Loop over all min and max conditions, including value and percentile conditions
'''
for key, val in col_perc_cutoff_dict.items():
'''
Current data series
'''
df_col = df[winsor_coln]
'''
Obtain Thresholds
'''
if ('q_' in key):
'''
Quantiles
'''
threshold = df_col.quantile(val)
elif ('v_' in key):
'''
levels
'''
threshold = val
else:
raise('did not find _q quantile or _v value in key')
'''
Generate conditions
'''
if ('_l' in key):
winsorize_upper = np.minimum(winsorize_upper, threshold)
if ('_le' in key):
if_condi = (df_col <= threshold)
elif ('_ll' in key):
if_condi = (df_col < threshold)
else:
raise('key:'+ key + ', is invalid')
elif ('_g' in key):
winsorize_lower = np.maximum(winsorize_lower, threshold)
if ('_ge' in key):
if_condi = (df_col >= threshold)
elif ('_gg' in key):
if_condi = (df_col > threshold)
else:
raise('key:'+ key + ', is invalid')
else:
raise('key:'+ key + ', is invalid')
'''
Combine Conditions
'''
if (condi_ctr == 0):
if_condi_all = if_condi
else:
if_condi_all = if_condi_all & if_condi
condi_ctr = condi_ctr + 1
'''
Winsorize
'''
if(return_type == 'winsorize'):
df[winsor_coln] = df_col.clip(lower=winsorize_lower, upper=winsorize_upper)
'''
Cut to data subset
'''
if(return_type == 'cutsubset'):
df_return = df[if_condi_all]
print(df_return)
support_json.jdump(df_return.to_dict(), 'conditions:' + str(if_condi_all),
logger=logger.debug, print_here = True)
if(return_type == 'winsorize'):
print(df)
support_json.jdump(df.to_dict(), 'winsorized',
logger=logger.debug, print_here = True)