-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscenario.py
More file actions
285 lines (225 loc) · 9.78 KB
/
scenario.py
File metadata and controls
285 lines (225 loc) · 9.78 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from pyrsistent import PClass, field, pvector_field
import pandas as pd
import numpy as np
from typing import List
from scipy.stats import bernoulli
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.impute import MissingIndicator, SimpleImputer
from sklearn.pipeline import Pipeline
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn_pandas import DataFrameMapper
from sklearn.datasets import make_classification, fetch_covtype
def build_generic_df_mapper(df, scalar_pipe=None) -> DataFrameMapper:
if scalar_pipe is None:
scalar_pipe = lambda: MissingIndicator()
df_cols = []
dropped_columns = set()
for c in df.columns:
if np.all(pd.isnull(df[c])):
continue
if df[c].nunique(dropna=False) <= 1:
continue
if df[c].dtype in ['float64', 'int', 'bool']:
if df[c].isnull().any():
df_cols.append(([c], scalar_pipe()))
else:
df_cols.append(([c], None))
elif df[c].dtype == 'O':
df_cols.append(([c], OneHotEncoder()))
else:
dropped_columns |= set([c])
return DataFrameMapper(df_cols)
class ModelScenario(PClass):
data_path = field(str)
data_source_name = field(str)
data_source_id_col = field(str, mandatory=False) # drop this column
data_source_outcome_col = field(str)
@property
def dataset_shifted(self) -> bool:
return False
@property
def data_source(self):
if hasattr(self, 'data_source_description'):
return self.data_source_description
else:
return self.data_source_name
def load_data(self):
df = pd.read_csv(self.data_path)
for c in df.columns:
if df[c].dtype == 'bool':
df[c] = df[c].astype(float)
if hasattr(self, 'data_source_id_col'):
df.drop(columns=[self.data_source_id_col], inplace=True)
return self.preprocess_data(df)
def preprocess_data(self, df: pd.DataFrame) -> pd.DataFrame:
return df
def base_sample_weight(self, X, y):
if isinstance(y, np.ndarray):
return np.ones(shape=y.shape)
elif isinstance(y, pd.Series):
return np.ones(shape=(len(y),))
else:
raise ValueError("y must be an np.array or pd.series")
def train_test(self, legacy_size=0.7, legacy_drop_frac=0.5, legacy_missing_label=0.2):
X = self.load_data()
assert 'legacy' not in X.columns
X['legacy'] = bernoulli(legacy_size).rvs(len(X))
y = X[self.data_source_outcome_col]
X = X.drop(columns=[self.data_source_outcome_col])
train_X, test_X, train_y, test_y = train_test_split(X, y, train_size=0.75)
train_y -= train_X['legacy'] * train_y * bernoulli(legacy_missing_label).rvs(len(train_y))
for c in train_X.columns:
if (bernoulli(legacy_drop_frac).rvs() == 1) and (c != 'legacy'):
train_X.loc[train_X['legacy'] == 1, c] = None
return train_X, test_X, train_y, test_y
def build_pipeline(self, train_X: pd.DataFrame) -> Pipeline:
return Pipeline([
('transform', build_generic_df_mapper(train_X)),
('final_estimator', HistGradientBoostingClassifier()),
])
class _CarInsuranceModelScenario(ModelScenario):
def preprocess_data(self, df: pd.DataFrame) -> pd.DataFrame:
for c in df.columns:
if set(df[c].unique()) == set(['Yes', 'No']):
df[c] = (df[c] == 'Yes')
for c in df.columns:
if df[c].dtype == 'bool':
df[c] = df[c].astype(float)
return df
class _TabularPlaygroundModelScenario(ModelScenario):
def build_pipeline(self, train_X: pd.DataFrame) -> Pipeline:
return Pipeline([
('transform', build_generic_df_mapper(train_X, scalar_pipe=lambda: [SimpleImputer(), StandardScaler()])),
('final_estimator', HistGradientBoostingClassifier()),
])
class SyntheticScenario(ModelScenario):
data_source_outcome_col = field(str, initial='outcome')
n_samples = field(int, initial=100000)
n_features = field(int, initial=100)
n_informative = field(int, initial=20)
n_redundant = field(int, initial=10)
n_repeated = field(int, initial=5)
n_clusters_per_class = field(int, initial=4)
weights = pvector_field(float, initial=[0.95, 0.05])
@property
def data_source_description(self):
return 'sklearn.datasets.make_classification'
def load_data(self):
X, y = make_classification(
n_samples=100000,
n_features=self.n_features,
n_informative=self.n_informative,
n_redundant=self.n_redundant,
n_repeated=self.n_repeated,
n_clusters_per_class=self.n_clusters_per_class,
weights=list(self.weights),
)
df = pd.DataFrame(X)
df.columns=['feature_{}'.format(n) for n in range(X.shape[1])]
df['outcome'] = y
return df
class SyntheticDSShift(SyntheticScenario):
data_source_outcome_col = field(str, initial='target')
weights = pvector_field(float, initial=[0.95, 0.04, 0.01])
def load_data(self):
raise NotImplemented
@property
def dataset_shifted(self) -> bool:
return True
def train_test(self, legacy_size=0.7, legacy_drop_frac=0.5, legacy_missing_label=0.2):
X, y = make_classification(
n_samples=100000,
n_classes=len(self.weights),
n_features=self.n_features,
n_informative=self.n_informative,
n_redundant=self.n_redundant,
n_repeated=self.n_repeated,
n_clusters_per_class=self.n_clusters_per_class,
weights=list(self.weights),
)
df = pd.DataFrame(X)
df.columns=['feature_{}'.format(n) for n in range(X.shape[1])]
df['raw_target'] = y
del y
# Dataset has 2 primary types, plus a third one which is distributed
# differently but gets lumped into the second
df[self.data_source_outcome_col] = (df['raw_target'] > 0)
# We will adjust the data so that the new cluster only shows up in
# new data (non-legacy)
df_new = df[df['raw_target'] == 2].copy()
df_old = df[df['raw_target'] != 2].copy()
del df
df_old['legacy'] = bernoulli(legacy_size).rvs(len(df_old))
# New data is by definition not legacy
df_new['legacy'] = 0
X = pd.concat([df_old, df_new])
y = X[self.data_source_outcome_col]
X.drop(columns=[self.data_source_outcome_col, 'raw_target'], inplace=True)
train_X, test_X, train_y, test_y = train_test_split(X, y, train_size=0.75)
train_y -= train_X['legacy'] * train_y * bernoulli(legacy_missing_label).rvs(len(train_y))
for c in train_X.columns:
if (bernoulli(legacy_drop_frac).rvs() == 1) and (c != 'legacy'):
train_X.loc[train_X['legacy'] == 1, c] = None
return train_X, test_X, train_y, test_y
class CoverTypeBinarized(ModelScenario):
data_source_outcome_col = field(str, initial='target')
@property
def data_source_description(self):
return 'sklearn.datasets.fetch_covtype, modified'
@property
def dataset_shifted(self) -> bool:
return True
def train_test(self, legacy_size=0.7, legacy_drop_frac=0.5, legacy_missing_label=0.2):
x = fetch_covtype(as_frame=True)
df = x['data']
df['raw_target'] = x['target']
# Dataset has 2 primary cover types, plus 7 more less common ones
# so lets put some nice imbalance in our set
df['target'] = (df['raw_target'] >= 3)
# We will adjust the data so that the 7'th cover type only shows up
# in non-legacy data.
df_new = df[df['raw_target'] == 7].copy()
df_old = df[df['raw_target'] != 7].copy()
del df
df_old['legacy'] = bernoulli(legacy_size).rvs(len(df_old))
df_new['legacy'] = 0
X = pd.concat([df_old, df_new])
y = X[self.data_source_outcome_col]
X = X.drop(columns=[self.data_source_outcome_col, 'raw_target'])
train_X, test_X, train_y, test_y = train_test_split(X, y, train_size=0.75)
train_y -= train_X['legacy'] * train_y * bernoulli(legacy_missing_label).rvs(len(train_y))
for c in train_X.columns:
if (bernoulli(legacy_drop_frac).rvs() == 1) and (c != 'legacy'):
train_X.loc[train_X['legacy'] == 1, c] = None
return train_X, test_X, train_y, test_y
SCENARIOS = {
'santander': ModelScenario(
data_path='santander/santander_training_data.csv',
data_source_name='https://www.kaggle.com/competitions/santander-customer-satisfaction',
data_source_outcome_col='TARGET',
data_source_id_col='ID',
),
'car_insurance': _CarInsuranceModelScenario(
data_path='car_insurance/train.csv',
data_source_name='https://www.kaggle.com/datasets/ifteshanajnin/carinsuranceclaimprediction-classification',
data_source_outcome_col='is_claim',
data_source_id_col='policy_id',
),
'tabular_playground': _TabularPlaygroundModelScenario(
data_path='tabular_playground_aug_2022/train.csv',
data_source_name='https://www.kaggle.com/competitions/tabular-playground-series-aug-2022/data?select=train.csv',
data_source_outcome_col='failure',
data_source_id_col='id',
),
'synthetic_1': SyntheticScenario(
data_source_name='Synthetic 1'
),
'synthetic_2_dataset_shift': SyntheticDSShift(
data_source_name='Synthetic 2 ds shift'
),
'cover_type_dataset_shift': CoverTypeBinarized(
data_source_name='cover type, ds shift',
),
}