12 KiB
12 KiB
In [1]:
## preparation: import libraries and read data
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
%matplotlib inline
datapath = '../3_data'
from os import chdir
chdir(datapath)
data = pd.read_csv('bank_data.csv', sep=';')In [2]:
## remove case for age > 100
data.drop(data[data.age >= 100].index, inplace=True)In [3]:
## remove duplicates
data.drop_duplicates(ignore_index=True, inplace = True)In [4]:
## alternative ['default', 'poutcome', 'duration']
vars_to_drop = ['default', 'poutcome']
data = data.drop(vars_to_drop, axis=1)In [5]:
## create lists of names of of categorical and numerical variables
cat_vars = data.select_dtypes(include='object').columns.tolist()
num_vars = data.select_dtypes(exclude='object').columns.tolist()
## import SimpleImputer class
from sklearn.impute import SimpleImputer
## imput for categorical variables
imp_mode = SimpleImputer(missing_values=np.nan, strategy='most_frequent')
data[cat_vars] = pd.DataFrame(imp_mode.fit_transform(data[cat_vars]), columns=data[cat_vars].columns)
## imput for numerical variables
imp_median = SimpleImputer(missing_values=np.nan, strategy='median')
data[num_vars] = pd.DataFrame(imp_median.fit_transform(data[num_vars]), columns=data[num_vars].columns)In [6]:
## education: illiterate : basic.4y
data.education = np.where(
data.education == 'illiterate',
'basic.4y',
data.education)Cell:
[Cell type raw - unsupported, skipped]
In [7]:
## education, day_of_week, month
replace_nums = {
'education': {
'basic.4y': 1,
'basic.6y': 2,
'basic.9y': 3,
'professional.course': 4,
'high.school': 5,
'university.degree': 6
},
'month': {
'jan': 1,
'feb': 2,
'mar': 3,
'apr': 4,
'may': 5,
'jun': 6,
'jul': 7,
'aug': 8,
'sep': 9,
'oct': 10,
'nov': 11,
'dec': 12
},
'day_of_week': {
'mon': 1,
'tue': 2,
'wed': 3,
'thu': 4,
'fri': 5
}
}
data.replace(replace_nums, inplace=True)/var/folders/30/93p10lq141sd2pvx31bfs77w0000gp/T/ipykernel_6176/854807168.py:33: FutureWarning: Downcasting behavior in `replace` is deprecated and will be removed in a future version. To retain the old behavior, explicitly call `result.infer_objects(copy=False)`. To opt-in to the future behavior, set `pd.set_option('future.no_silent_downcasting', True)`
data.replace(replace_nums, inplace=True)
In [8]:
## housing : no -> 0 else 1
data.housing = np.where(data.housing == 'no', 0, 1)
## contact : celular -> 1 else 0
data.contact = np.where(data.contact == 'cellular', 1, 0)
## rename
data = data.rename(columns={'contact': 'contact_cellular'})In [9]:
## one-hot encoding
## apply for all categorical variables except target
target = 'y'
sel_vars = data.select_dtypes(include=['object']).columns.drop(target)
data = pd.get_dummies(data, columns=sel_vars, drop_first=True)In [10]:
## duration and campaign
data.duration = np.log10(data.duration + data.duration.min() + 1)
data.campaign = np.log10(data.campaign + data.campaign.min() + 1)In [11]:
## pdays : 999 -> 0, else 1
data.pdays = np.where(data.pdays == 999, 0, 1)
## previous : > 0 -> 1 else 0
data.previous = np.where(data.previous > 0, 1, 0)In [12]:
old_names = data.columns
new_names = old_names.str.replace('[^a-zA-Z0-9_]', '_', regex=True)
for i in range(len(old_names)):
data.rename(columns={old_names[i]:new_names[i]}, inplace=True)Cell:
[Cell type raw - unsupported, skipped]
In [13]:
## as bank_data_prep.csv
## parameters
## sep = ',' (default)
## index = False (default True would add an index column on the left)
data.to_csv('bank_data_prep.csv', index=False)