-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_cleaning.py
157 lines (124 loc) · 5.87 KB
/
data_cleaning.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
import numpy as np
import pandas as pd
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import Imputer
from sklearn.preprocessing import StandardScaler
class DataPreparation:
def __init__(self,
attribute_values,
imputer,
scaler,
final_cols):
"""
Data Preparation, including:
* Deal with "Unknown" values
* Deal with missing values
* Remove "fine" categorical variables
* Decide which columns will need one-hot-encoding or extra engineering
* Make sure that all columns are numerical
* Fill missing values and scale the data
params:
attribute_values: the table with the description of the attributes ("attribute_values_cleaned_checkpoint.csv")
imputer: the fitted imputer
scaler: the fitted scaler
final_cols: final columns
"""
self.attribute_values = attribute_values
self.imputer = imputer
self.scaler = scaler
self.final_cols = final_cols
@staticmethod
def code_unknown_to_nan(data, attribute_values):
"""
Code numbers that represent "unknown" values to NaN
"""
attribute_values_unknown = attribute_values[attribute_values['Meaning'] == "unknown"]
for i in range(len(attribute_values_unknown)):
colname = attribute_values_unknown.iloc[i]['Attribute']
unknown_values = eval('[' + str(attribute_values_unknown.iloc[i]['Value']) + ']')
try:
data[colname] = data[colname].replace(unknown_values, float('nan'))
except:
pass
return data
@staticmethod
def remove_cols_high_missing_rates(data, min_missing_rate=0.4):
"""
Remove columns with missing rates more than min_missing_rate
"""
cols_keep = list(data.isna().mean()[data.isna().mean() < min_missing_rate].index)
return data[cols_keep], cols_keep
@staticmethod
def remove_fine_cols(data):
cols_keep = [i for i in list(data.columns) if not i.endswith("_FEIN")]
data = data[cols_keep]
return data, cols_keep
@staticmethod
def label_to_meaning(attribute, attribute_values):
"""
Create a dictionary to map attribute labels to meanings
"""
attribute_values_1 = attribute_values[attribute_values['Attribute'] == attribute]
result = {}
for i in range(len(attribute_values_1)):
result[attribute_values_1.iloc[i]["Value"]] = attribute_values_1.iloc[i]["Meaning"]
return result
def engineer_cat_cols(self, data, attribute_values):
"""
Perform ad-hoc transformation on selected attributes:
"""
data['CAMEO_DEU_2015'] = data['CAMEO_DEU_2015']\
.replace("XX", float('nan'))\
.apply(lambda x: x if str(x) == "nan" else str(x)[0])
data['CAMEO_DEUG_2015'] = data['CAMEO_DEUG_2015'].replace("X", np.float('nan')).astype("float")
data['CAMEO_INTL_2015'] = data['CAMEO_INTL_2015'].replace("XX", np.float('nan')).astype("float")
def custom_split1(x):
x = str(x)
try:
splits1 = x.split(" - ")
splits2 = splits1[1].split(" (")
splits3 = splits2[1].split(", ")
return [splits1[0], splits3[0]]
except:
return x
PRAEGENDE_JUGENDJAHRE_dict = self.label_to_meaning("PRAEGENDE_JUGENDJAHRE", attribute_values)
PRAEGENDE_JUGENDJAHRE_temp = data['PRAEGENDE_JUGENDJAHRE']\
.apply(lambda x: PRAEGENDE_JUGENDJAHRE_dict[str(int(x))] if str(x) != "nan" else x)
data['PRAEGENDE_JUGENDJAHRE_part1'] = PRAEGENDE_JUGENDJAHRE_temp\
.apply(lambda x: custom_split1(x)[0] if str(x) != "nan" else x)
data['PRAEGENDE_JUGENDJAHRE_part2'] = PRAEGENDE_JUGENDJAHRE_temp\
.apply(lambda x: custom_split1(x)[1] if str(x) != "nan" else x)
data = data\
.join(pd.get_dummies(data[['PRAEGENDE_JUGENDJAHRE_part1','PRAEGENDE_JUGENDJAHRE_part2']]))\
.drop(['PRAEGENDE_JUGENDJAHRE','PRAEGENDE_JUGENDJAHRE_part1','PRAEGENDE_JUGENDJAHRE_part2'], axis=1)
one_hot_encode_cols = ['CJT_GESAMTTYP',
'D19_KONSUMTYP',
'GFK_URLAUBERTYP',
'HEALTH_TYP',
'LP_LEBENSPHASE_GROB',
'SHOPPER_TYP',
'ZABEOTYP']
data = data\
.join(pd.get_dummies(data[one_hot_encode_cols].astype("object"), dummy_na=False))\
.drop(one_hot_encode_cols, axis=1)
return data
def pre_fit(self, data):
data = self.code_unknown_to_nan(data, self.attribute_values)
data, cols_keep1 = self.remove_cols_high_missing_rates(data)
data, cols_keep2 = self.remove_fine_cols(data)
data = self.engineer_cat_cols(data, self.attribute_values)
data = data[self.final_cols]
self.index = data.index
return data
def fit(self, data):
data = self.pre_fit(data)
self.imputer = Imputer(missing_values=float('nan'), strategy="mean", axis=0).fit(data)
data = self.imputer.transform(data)
self.scaler = StandardScaler().fit(data)
def transform(self, new_data):
new_data = self.pre_fit(new_data)
self.colnames = new_data.columns
self.index = new_data.index
new_data = self.imputer.transform(new_data)
new_data = self.scaler.transform(new_data)
return new_data