-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdummy_var.py
More file actions
32 lines (29 loc) · 1.21 KB
/
Copy pathdummy_var.py
File metadata and controls
32 lines (29 loc) · 1.21 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 8 12:54:46 2018
@author: claudia
"""
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
def preprocessData(df):
label_encoder = LabelEncoder()
dummy_encoder = OneHotEncoder()
pdf = pd.DataFrame()
for att in df.columns:
if df[att].dtype == np.float64 or df[att].dtype == np.int64:
pdf = pd.concat([pdf, df[att]], axis=1)
else:
df[att] = label_encoder.fit_transform(df[att])
# Fitting One Hot Encoding on train data
temp = dummy_encoder.fit_transform(df[att].values.reshape(-1,1)).toarray()
# Changing encoded features into a dataframe with new column names
temp = pd.DataFrame(temp,
columns=[(att + "_" + str(i)) for i in df[att].value_counts().index])
# In side by side concatenation index values should be same
# Setting the index values similar to the data frame
temp = temp.set_index(df.index.values)
# adding the new One Hot Encoded varibales to the dataframe
pdf = pd.concat([pdf, temp], axis=1)
return pdf