-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
193 lines (130 loc) · 7.21 KB
/
utils.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
import numpy as np
import pandas as pd
import os
import time
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler
def dataset_treatment(azdias, customers, cust=True):
print("Deleting columns...")
# Specific to customers
if cust:
del customers['PRODUCT_GROUP']
del customers['CUSTOMER_GROUP']
del customers['ONLINE_PURCHASE']
# Just the person ID
del azdias["LNR"]
del customers["LNR"]
# Replace 'X', 'XX' and 'nan' values with np.nan for 'CAMEO_DEUG_2015' and 'CAMEO_INTL_2015' columns
cols = ["CAMEO_DEUG_2015", "CAMEO_INTL_2015"]
azdias[cols] = azdias[cols].replace({"X": np.nan, "XX": np.nan})
azdias[cols] = azdias[cols].astype(float)
customers[cols] = customers[cols].replace({"X": np.nan, "XX": np.nan})
customers[cols] = customers[cols].astype(float)
# Create unkown attributes
attributes_values = pd.read_csv('data/DIAS Attributes - Values 2017.csv', sep=';')
attributes_values["Attribute"] = attributes_values["Attribute"].ffill() # To simply the visualisation
unkown_attributes_values = attributes_values[attributes_values["Meaning"] == "unknown"].dropna().reset_index(drop=True)
# Replace unkown attributes
customers = unknown_to_NaN(customers, unkown_attributes_values)
azdias = unknown_to_NaN(azdias, unkown_attributes_values)
azdias["WOHNLAGE"] = azdias["WOHNLAGE"].replace({0: np.nan})
customers["WOHNLAGE"] = customers["WOHNLAGE"].replace({0: np.nan})
# Delete columns with too much missing values
columns_to_delete = ["ALTER_KIND4", "ALTER_KIND3", "TITEL_KZ", "ALTER_KIND2", "ALTER_KIND1", "KK_KUNDENTYP", "KBA05_BAUMAX", "AGER_TYP", "EXTSEL992"]
for column in columns_to_delete:
del azdias[column]
del customers[column]
print("Deleting rows...")
customers = remove_rows(customers, threshold=50)
azdias = remove_rows(azdias, threshold=50)
print("Encoding...")
del customers["CAMEO_DEU_2015"]
del customers["D19_LETZTER_KAUF_BRANCHE"]
del azdias["CAMEO_DEU_2015"]
del azdias["D19_LETZTER_KAUF_BRANCHE"]
customers = date_to_year(customers)
azdias = date_to_year(azdias)
customers["OST_WEST_KZ"] = customers["OST_WEST_KZ"].replace({"W": 0, "O": 1})
azdias["OST_WEST_KZ"] = azdias["OST_WEST_KZ"].replace({"W": 0, "O": 1})
customers["ANREDE_KZ"] = customers["ANREDE_KZ"].replace({1: 0, 2: 1})
azdias["ANREDE_KZ"] = azdias["ANREDE_KZ"].replace({1: 0, 2: 1})
imputer = SimpleImputer(strategy="most_frequent") # With this parameter, we will replace the NaN values by the most frequent value for this parameter.
imputer.fit(azdias) # We will fit the imputer on the azidias esclusively, as it contains the most data.
azdias = pd.DataFrame(imputer.transform(azdias), columns = azdias.columns)
customers = pd.DataFrame(imputer.transform(customers), columns = customers.columns)
scaler = StandardScaler()
scaler.fit(azdias)
azdias = pd.DataFrame(scaler.transform(azdias), columns = azdias.columns)
customers = pd.DataFrame(scaler.transform(customers), columns = customers.columns)
return azdias, customers
def dataset_treatment_test(azdias, customers):
print("Deleting columns...")
# Just the person ID
del azdias["LNR"]
del customers["LNR"]
# Replace 'X', 'XX' and 'nan' values with np.nan for 'CAMEO_DEUG_2015' and 'CAMEO_INTL_2015' columns
cols = ["CAMEO_DEUG_2015", "CAMEO_INTL_2015"]
azdias[cols] = azdias[cols].replace({"X": np.nan, "XX": np.nan})
azdias[cols] = azdias[cols].astype(float)
customers[cols] = customers[cols].replace({"X": np.nan, "XX": np.nan})
customers[cols] = customers[cols].astype(float)
# Create unkown attributes
attributes_values = pd.read_csv('data/DIAS Attributes - Values 2017.csv', sep=';')
attributes_values["Attribute"] = attributes_values["Attribute"].ffill() # To simply the visualisation
unkown_attributes_values = attributes_values[attributes_values["Meaning"] == "unknown"].dropna().reset_index(drop=True)
# Replace unkown attributes
customers = unknown_to_NaN(customers, unkown_attributes_values)
azdias = unknown_to_NaN(azdias, unkown_attributes_values)
azdias["WOHNLAGE"] = azdias["WOHNLAGE"].replace({0: np.nan})
customers["WOHNLAGE"] = customers["WOHNLAGE"].replace({0: np.nan})
# Delete columns with too much missing values
columns_to_delete = ["ALTER_KIND4", "ALTER_KIND3", "TITEL_KZ", "ALTER_KIND2", "ALTER_KIND1", "KK_KUNDENTYP", "KBA05_BAUMAX", "AGER_TYP", "EXTSEL992"]
for column in columns_to_delete:
del azdias[column]
del customers[column]
print("Deleting rows...")
#customers = remove_rows(customers, threshold=50)
azdias = remove_rows(azdias, threshold=50)
print("Encoding...")
del customers["CAMEO_DEU_2015"]
del customers["D19_LETZTER_KAUF_BRANCHE"]
del azdias["CAMEO_DEU_2015"]
del azdias["D19_LETZTER_KAUF_BRANCHE"]
customers = date_to_year(customers)
azdias = date_to_year(azdias)
customers["OST_WEST_KZ"] = customers["OST_WEST_KZ"].replace({"W": 0, "O": 1})
azdias["OST_WEST_KZ"] = azdias["OST_WEST_KZ"].replace({"W": 0, "O": 1})
customers["ANREDE_KZ"] = customers["ANREDE_KZ"].replace({1: 0, 2: 1})
azdias["ANREDE_KZ"] = azdias["ANREDE_KZ"].replace({1: 0, 2: 1})
imputer = SimpleImputer(strategy="most_frequent") # With this parameter, we will replace the NaN values by the most frequent value for this parameter.
imputer.fit(azdias) # We will fit the imputer on the azidias esclusively, as it contains the most data.
azdias = pd.DataFrame(imputer.transform(azdias), columns = azdias.columns)
customers = pd.DataFrame(imputer.transform(customers), columns = customers.columns)
scaler = StandardScaler()
scaler.fit(azdias)
azdias = pd.DataFrame(scaler.transform(azdias), columns = azdias.columns)
customers = pd.DataFrame(scaler.transform(customers), columns = customers.columns)
return azdias, customers
def get_unkown_repr(attrib, unkown_attributes_values):
unkown = unkown_attributes_values[unkown_attributes_values["Attribute"] == attrib]["Value"]
unkown = unkown.astype(str).str.cat(sep=",")
unkown = [int(x) for x in unkown.split(",")]
return [unkown]
def replace_unkown_with_nan(val, unkown):
if val in unkown:
return np.nan
else:
return val
def unknown_to_NaN(df, unkown_attributes_values):
for attrib in unkown_attributes_values.Attribute:
unkown = get_unkown_repr(attrib, unkown_attributes_values)
if attrib in df.columns: # For some reasons, some attributes are not present in the datasets
df[attrib] = df[attrib].apply(replace_unkown_with_nan, args=(unkown))
return df
def remove_rows(df, threshold):
df = df.dropna(thresh=df.shape[1]-threshold).reset_index(drop=True) # thresh (int, optional): Require that many non-NA values.
return df
def date_to_year(df):
df["EINGEFUEGT_AM"] = pd.to_datetime(df["EINGEFUEGT_AM"])
df["EINGEFUEGT_AM"] = df['EINGEFUEGT_AM'].map(lambda x: x.year)
return df