-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpute.py
More file actions
571 lines (461 loc) · 22.7 KB
/
Copy pathimpute.py
File metadata and controls
571 lines (461 loc) · 22.7 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import itertools
import random
from sklearn.ensemble import RandomForestClassifier
from transform import *
from sklearn import linear_model
from graphic_utils import *
import math
LINEAR_FILL_CORR_THRESHOLD = 0.8
CAT_RARITY_THRESHOLD = 0.01
STD_DIFF = 5
plt.rcParams.update({'font.size': 5})
def get_num_feature_list(train_set):
'''
:param train_set: A Pandas dataframe
:return: list containing names of all numeric features. Every non-numeric feature is categorical
'''
assert isinstance(train_set, pd.DataFrame)
ret = train_set.select_dtypes(exclude=['object']).columns
assert 'Vote' not in ret
return ret
def analyze_NaNs(train_set, val_set, test_set, verbose=True):
if verbose:
train_miss = train_set.isnull().sum().sum()
val_miss = val_set.isnull().sum().sum()
test_miss = test_set.isnull().sum().sum()
train_bad_samples = (train_set.shape[0] - train_set.dropna().shape[0])*100/train_set.shape[0]
val_bad_samples = (val_set.shape[0] - val_set.dropna().shape[0])*100/val_set.shape[0]
test_bad_samples = (test_set.shape[0] - test_set.dropna().shape[0])*100/test_set.shape[0]
print("========== Dataset Analysis ==========")
print("dataset sizes:", train_set.shape[0], val_set.shape[0], test_set.shape[0])
print("train_set has in total", train_miss,
"missing values, in ~", np.round(train_bad_samples), "% of samples")
print("val_set has in total", val_miss,
"missing values, in ~", np.round(val_bad_samples), "% of samples")
print("test_set has in total", test_miss,
"missing values, in ~", np.round(test_bad_samples), "% of samples")
def delete_missing_values(train_set, val_set, test_set, verbose=True):
# This option will lead to loss of 45% of samples!!!
for data_set in (train_set, val_set, test_set):
data_set.dropna()
return train_set, val_set, test_set
def fill_missing_most_common(train_set, val_set, test_set):
"""
This function fill missing values with
most common value in a CATEGORICAL column
"""
for col in train_set.columns:
# Check if categorical
if not np.issubdtype(train_set[col].dtype, np.number):
train_set[col].fillna(train_set[col].mode().iloc[0], inplace=True)
for col in val_set.columns:
# Check if categorical
if not np.issubdtype(val_set[col].dtype, np.number):
val_set[col].fillna(train_set[col].mode().iloc[0], inplace=True)
for col in test_set.columns:
# Check if categorical
if not np.issubdtype(test_set[col].dtype, np.number):
test_set[col].fillna(test_set[col].mode().iloc[0], inplace=True)
return train_set, val_set, test_set
def fill_missing_mean(train_set, val_set, test_set):
"""
This function fill missing values with
most common value in a NUMERICAL column
"""
for col in train_set.columns:
# Check if numerical
if np.issubdtype(train_set[col].dtype, np.number):
train_set[col].fillna(train_set[col].mean(), inplace=True)
for col in val_set.columns:
# Check if numerical
if np.issubdtype(val_set[col].dtype, np.number):
val_set[col].fillna(train_set[col].mean(), inplace=True)
for col in test_set.columns:
# Check if numerical
if np.issubdtype(test_set[col].dtype, np.number):
test_set[col].fillna(test_set[col].mean(), inplace=True)
return train_set, val_set, test_set
def get_correlated_feature_groups(corr_matrix, threshold=0.93, verbose=False):
'''
Gets a correlation matrix and returns all groups of features that have higher
absolute correlation than threshold
:param corr_matrix:
:param threshold:
:param verbose:
:return: A list of lists, each list represents a group of features that are correlated
'''
corr_matrix_cpy = corr_matrix.copy()
corr_matrix_cpy.loc[:, :] = np.tril(corr_matrix_cpy, k=-1)
already_in = set()
corr_features_groups = []
for col in corr_matrix_cpy:
perfect_corr = corr_matrix_cpy[col][corr_matrix_cpy[col] > threshold].index.tolist()
if perfect_corr and col not in already_in:
already_in.update(set(perfect_corr))
perfect_corr.append(col)
corr_features_groups.append(perfect_corr)
for corr_group in corr_features_groups: # Check that the relation is transitive
for x, y in list(itertools.combinations(corr_group, 2)):
assert corr_matrix[x][y] > threshold
if verbose:
print("correlated groups are :")
for corr_group in corr_features_groups:
print(corr_group, "\n")
return corr_features_groups
def impute_by_lin_model(model, data, X, Y):
'''
:param model: linear regression model of X and Y
:param data: The DataFrame we fill missing values in
:param X: The reference label
:param Y: The label we fill missing values in
:return: data, imputed
'''
assert isinstance(model, linear_model.LinearRegression)
assert isinstance(data, pd.DataFrame)
num_nans_start = data[Y].isna().sum()
for index, row in data[data[Y].isnull()].iterrows():
if not np.isnan(data[X][index]):
data.at[index, Y] = model.predict([[data[X][index]]])[0][0]
print("Filled ", num_nans_start - data[Y].isna().sum(), "nans of feature", Y, "from ", X)
return data
def __fill_missing_linear_regression(train, validation, test, features, corr_mat):
'''
Fill missing values in all 3 sets by a linear regression to the most correlated other feature,
in absolute value. Will never fill missing values if the correlation between features is lower
than LINEAR_FILL_CORR_THRESHOLD
:param train: train set
:param validation: validation set
:param test: test set
:param features: list of feature names to fill, must be numeric features
:param corr_mat: correlation matrix between all features
:return:
'''
assert isinstance(train, pd.DataFrame)
assert isinstance(test, pd.DataFrame)
assert isinstance(validation, pd.DataFrame)
assert isinstance(features, list)
sum_nas_before = sum([s[features].isna().sum().sum() for s in (train, validation, test)])
all_sets = [train, validation, test]
for feature in features:
corr_tuples = [(corr_mat[feature][col], col) for col in corr_mat.columns if col != feature]
corr_tuples.sort(key=lambda x: x[0], reverse=True)
while corr_tuples[0][0] >= LINEAR_FILL_CORR_THRESHOLD and \
sum([s[feature].isna().sum() for s in (train, validation, test)]) > 0:
reference_feature = corr_tuples[0][1]
feature_duo_train = train[[reference_feature, feature]].copy()
feature_duo_val = validation[[reference_feature, feature]].copy()
feature_duo = pd.concat([feature_duo_train, feature_duo_val])
feature_duo = feature_duo.dropna(how='any')
lin_model = linear_model.LinearRegression()
model = lin_model.fit(feature_duo[reference_feature].values.reshape(-1, 1),
feature_duo[feature].values.reshape(-1, 1))
for index, data_set in enumerate(all_sets):
all_sets[index] = impute_by_lin_model(model, data_set, reference_feature, feature)
corr_tuples.pop(0)
[train, validation, test] = all_sets
sum_nas_after = sum([s[features].isna().sum().sum() for s in (train, validation, test)])
assert sum_nas_after < sum_nas_before
print('we filled', (1-float(sum_nas_after)/sum_nas_before)*100, '% of nas in numerical features',
'of all three sets')
return train, validation, test
def fill_missing_vals_by_mean(train, val, test, features):
'''
Fills three data sets' all missing values in given features by the mean value of that feature
Used as a last resort
:param train: train data set
:param val: validation data set
:param test: test data set
:param features: A list or set of feature names to fill. Features MUST be numeric
:return:
'''
assert isinstance(train, pd.DataFrame)
assert isinstance(val, pd.DataFrame)
assert isinstance(test, pd.DataFrame)
assert isinstance(features, (list, set))
for f in features:
# compute mean
train_and_val = pd.concat([train, val])
mean = train_and_val[f].mean()
for data_set in (train, val, test):
data_set[f].fillna(mean, inplace=True)
assert data_set[f].isna().sum() == 0
return train, val, test
def fill_missing_vals_exp1(train_set, val_set, test_set, verbose=True, graphic=False):
'''
Show results of very basic experiments, filling missing values with the mean or most common value
:param train_set:
:param val_set:
:param test_set:
:param verbose:
:return:
'''
assert isinstance(train_set, pd.DataFrame)
assert isinstance(val_set, pd.DataFrame)
assert isinstance(test_set, pd.DataFrame)
exp_train = train_set.copy()
exp_val = val_set.copy()
exp_test = test_set.copy()
# Show % of corrupted samples.
analyze_NaNs(exp_train, exp_val, exp_test, verbose)
# Show histograms
if graphic:
show_set_hist(exp_train, title='train_set before removing NaNs')
# Fill categorical data with most common
exp_train, exp_val, exp_test = fill_missing_most_common(exp_train, exp_val, exp_test)
# Show % of corrupted samples.
analyze_NaNs(exp_train, exp_val, exp_test, verbose)
# Fill numerical data with mean
exp_train, exp_val, test_set = fill_missing_mean(exp_train, exp_val, exp_test)
# Show % of corrupted samples.
analyze_NaNs(exp_train, exp_val, exp_test, verbose)
if graphic:
show_set_hist(exp_train, title='train_set after removing NaNs')
assert exp_train.isnull().sum().sum()
assert exp_val.isnull().sum().sum() == 0
assert exp_test.isnull().sum().sum() == 0
def fill_nans_by_lin_regress(train_set, val_set, test_set, verbose=True, graphic=False, all_history=False):
'''
Fills all numeric missing values in all three sets, first by correlated features then the rest
are just filled by the median value
:param graphic: Whether to show graphs
:param all_history: Running entire history of experimentations
:return:
'''
assert isinstance(train_set, pd.DataFrame)
assert isinstance(val_set, pd.DataFrame)
assert isinstance(test_set, pd.DataFrame)
train_and_val = pd.concat([train_set, val_set])
numeric_features = train_and_val.select_dtypes(exclude=['object']).columns
corr_matrix = train_and_val[numeric_features].corr() # ignores string columns
if all_history:
fig = plt.figure(figsize=(20, 10))
ax = fig.add_subplot(111)
cax = ax.matshow(corr_matrix, cmap='coolwarm', vmin=-1, vmax=1)
fig.colorbar(cax)
ticks = np.arange(0, len(numeric_features), 1)
ax.set_xticks(ticks)
plt.xticks(rotation=90)
ax.set_yticks(ticks)
ax.set_xticklabels(numeric_features)
ax.set_yticklabels(numeric_features)
plt.show()
corr_matrix = abs(corr_matrix)
if verbose:
corr_indices = np.where(corr_matrix > 0.95)
corr_pairs = [(corr_matrix.index[x], corr_matrix.columns[y]) for x, y in zip(*corr_indices)
if x != y and x < y]
print("correlated feature pairs are:\n")
for pair in corr_pairs:
print(pair, "\n")
# failed experiment
# if graphic:
# from pandas.plotting import scatter_matrix
# scatter_matrix(train_set)
# plt.show()
corr_feature_groups = get_correlated_feature_groups(corr_matrix)
if verbose:
print("Correlated feature groups are: ", corr_feature_groups)
redundant_features = []
for corr_group in corr_feature_groups:
for feature in corr_group[1:]:
redundant_features.append(feature)
if verbose:
print("redundant features are: ", redundant_features)
#useful_features = set(numeric_features).difference(set(redundant_features)) #TODO: last try
useful_features = set(numeric_features)
useful_features = list(useful_features)
useful_features.sort()
train_set, val_set, test_set = \
__fill_missing_linear_regression(train_set, val_set, test_set, useful_features, corr_matrix)
return train_set, val_set, test_set, redundant_features, useful_features
def __delete_vals_out_of_range(data_set, feature, min_val=-math.inf, max_val=math.inf):
'''
Marks all values of a given feature in a give data frame that are below min_val or
above max_val as nans
:param data_set: Pandas DataFrame, includes column feature
'''
assert isinstance(data_set, pd.DataFrame)
assert max_val >= min_val
count = -data_set[feature].isna().sum()
for index, row in data_set[~data_set[feature].between(min_val, max_val)].iterrows():
data_set.ix[index, feature] = np.nan
count += 1
if count > 0:
print("removed", count, "vals out of range for feature", feature)
def delete_vals_out_of_range(train_set, val_set, test_set, verbose=True):
'''
Delete all values in all data sets that are out of range
Deleted values will be marked as nans
No need to give features as an argument - they are hard coded and classified here
:return:
'''
assert isinstance(train_set, pd.DataFrame)
assert isinstance(val_set, pd.DataFrame)
assert isinstance(test_set, pd.DataFrame)
if verbose:
start_num_nans = sum([s.isna().sum().sum() for s in (train_set, val_set, test_set)])
non_negative_features = ['Avg_lottary_expanses',
'Avg_Residancy_Altitude',
'Avg_Satisfaction_with_previous_vote',
'Avg_education_importance',
'Avg_monthly_expense_on_pets_or_plants',
'Avg_monthly_household_cost',
'Avg_monthly_income_all_years',
'Avg_monthly_expense_when_under_age_21',
'Avg_environmental_importance',
'Political_interest_Total_Score',
'Avg_size_per_room',
'Garden_sqr_meter_per_person_in_residancy_area',
'Num_of_kids_born_last_10_years',
'Number_of_differnt_parties_voted_for',
'Weighted_education_rank',
'Yearly_ExpensesK',
'Yearly_IncomeK',
]
percentage_features = ['%Time_invested_in_work',
'%_satisfaction_financial_policy',
'Last_school_grades']
zero_to_ten_scale_features = ['Occupation_Satisfaction']
zero_to_one_scale_features = ['%Of_Household_Income',
'Financial_balance_score_(0-1)']
zero_to_120_scale_features = ['Number_of_valued_Kneset_members']
for data_set in (train_set, test_set, val_set):
for f in non_negative_features:
__delete_vals_out_of_range(data_set, f, min_val=0)
for f in zero_to_120_scale_features:
__delete_vals_out_of_range(data_set, f, min_val=0, max_val=120)
for f in percentage_features:
__delete_vals_out_of_range(data_set, f, min_val=0, max_val=100)
for f in zero_to_ten_scale_features:
__delete_vals_out_of_range(data_set, f, min_val=0, max_val=10)
for f in zero_to_one_scale_features:
__delete_vals_out_of_range(data_set, f, min_val=0, max_val=1)
if verbose:
num_nans_after_clip = sum([s.isna().sum().sum() for s in (train_set, val_set, test_set)])
num_vals_in_frame = 10000 * len(train_set.columns)
percentage_dropped_by_clipping = \
(float(num_nans_after_clip - start_num_nans) * 100) / num_vals_in_frame
print("Clipping dropped:", str(percentage_dropped_by_clipping) + '%',
'from all data sets combined')
def delete_outliers(train_set, val_set, test_set, features, verbose=True):
'''
Deletes all values of features that are STD_THRESHOLD number of standard deviations above mean
'''
assert isinstance(train_set, pd.DataFrame)
assert isinstance(val_set, pd.DataFrame)
assert isinstance(test_set, pd.DataFrame)
if verbose:
start_num_nans = sum([s.isna().sum().sum() for s in (train_set, val_set, test_set)])
train_and_val = pd.concat([train_set, val_set])
for f in features:
# find the mean and the std
std = train_and_val[f].std()
mean = train_and_val[f].mean()
for data_set in (train_set, val_set, test_set):
delta = STD_DIFF * std
for index, row in data_set[~data_set[f].between(mean - delta, mean + delta)].iterrows():
data_set.ix[index, f] = np.nan
if verbose:
final_num_nans = sum([s.isna().sum().sum() for s in (train_set, val_set, test_set)])
percentage_dropped_by_std_dropping = \
(float(final_num_nans - start_num_nans) * 100) / (10000 * len(train_set.columns))
print("Outliers dropped:", str(percentage_dropped_by_std_dropping) + '%', 'of all data sets combined')
def delete_rare_categorical_vals(train_set, val_set, test_set):
'''
Looks for categorical features that have categories appearing less often than CAT_RARITY_THRESHOLD
as a percentage across all label of that feature across all three sets.
NOTE: we did not find such labels. This code will not delete the labels if it finds any,
just prints them
'''
assert isinstance(train_set, pd.DataFrame)
assert isinstance(val_set, pd.DataFrame)
assert isinstance(test_set, pd.DataFrame)
categoric_features = train_set.select_dtypes(include=['object']).columns
for f in categoric_features:
value_counts = train_set[f].value_counts()
value_counts.add(val_set[f].value_counts(), fill_value=0)
value_counts.add(test_set[f].value_counts(), fill_value=0)
total_value_count = value_counts.sum()
for label, count in value_counts.iteritems():
if float(count)/total_value_count <= CAT_RARITY_THRESHOLD:
print('Found a categorical rarity at feature', f, 'label', label)
'''
def fill_categorical_missing_vals(train, val, test):
assert isinstance(train, pd.DataFrame)
assert isinstance(val, pd.DataFrame)
assert isinstance(test, pd.DataFrame)
categoric_features = train.select_dtypes(include=['object']).columns
for f in categoric_features:
value_counts = train[f].value_counts()
total_value_count = value_counts.sum()
for data_set in (train, val, test):
for index, row in data_set[data_set[f].isnull()].iterrows():
sample_index = random.randint(1, total_value_count)
for label, count in value_counts.iteritems():
if sample_index <= count:
data_set.ix[index, f] = label
break
sample_index -= count
assert data_set[f][index] != np.nan
for data_set in (train, test, val):
assert data_set[[f for f in categoric_features]].isna().sum().sum() == 0
'''
def fill_categorical_missing_vals(train, val, test):
assert isinstance(train, pd.DataFrame)
assert isinstance(val, pd.DataFrame)
assert isinstance(test, pd.DataFrame)
categoric_features = train.drop('Vote', axis=1).select_dtypes(include=['object']).columns
for f in categoric_features:
all_sets = [train, val, test]
for i, data_set in enumerate(all_sets):
transform_label(all_sets[i], f)
[train, val, test] = all_sets
all_sets = [train, val, test]
for f in categoric_features:
train_and_val = pd.concat([train, val])
train_cpy = []
for ff in categoric_features:
train_cpy = train_and_val[(~train_and_val[ff] < 0 )]
tree_data = train_cpy[(~train_cpy[f] < 0 )]
tree_data = tree_data.drop(columns='Vote')
#transform_label(tree_data, f)
X = tree_data.drop(columns=f)
Y = tree_data[f]
from sklearn import neighbors
clf = RandomForestClassifier()
clf.fit(X, Y)
for i, data_set in enumerate(all_sets):
#transform_label(all_sets[i], f)
for_prediction = data_set[data_set[f] < 0].drop(columns=f)
for_prediction = for_prediction.drop(columns='Vote')
for index, row in data_set[data_set[f] < 0].iterrows():
predict = clf.predict(for_prediction.loc[index].to_numpy().reshape(1, -1))
all_sets[i].ix[index, f] = predict
train = all_sets[0]
val = all_sets[1]
test = all_sets[2]
all_sets = [train, val, test]
assert len(train[(train[f] < 0)]) == 0
assert len(val[(val[f] < 0)]) == 0
assert len(test[(test[f] < 0)]) == 0
train["Occupation"] = all_sets[0]["Occupation"].astype('category')
val["Occupation"] = all_sets[1]["Occupation"].astype('category')
test["Occupation"] = all_sets[2]["Occupation"].astype('category')
train["Main_transportation"] = all_sets[0]["Main_transportation"].astype('category')
val["Main_transportation"] = all_sets[1]["Main_transportation"].astype('category')
test["Main_transportation"] = all_sets[2]["Main_transportation"].astype('category')
train["Most_Important_Issue"] = all_sets[0]["Most_Important_Issue"].astype('category')
val["Most_Important_Issue"] = all_sets[1]["Most_Important_Issue"].astype('category')
test["Most_Important_Issue"] = all_sets[2]["Most_Important_Issue"].astype('category')
all_sets = [train, val, test]
for i, data_set in enumerate(all_sets):
for f1 in ['Looking_at_poles_results', 'Gender', 'Married', 'Voting_Time', 'Financial_agenda_matters']:
for index, row in data_set[data_set[f1] == 0].iterrows():
all_sets[i].ix[index, f1] = -1
for f in ['Age_group' , 'Will_vote_only_large_party']:
all_sets[i][f] = all_sets[i][f]-1
[train, val, test] = all_sets
return train, val, test