-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategorical.py
145 lines (114 loc) · 5.38 KB
/
categorical.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
"""
schema: data -> fit_transform - choose func (i.e label encoding) -> dict (i.e label_encoders ) -> transforms value
in one encoding - it is becoming as a parse matrix ( if many of its coefficients are zero) and be
A matrix is sparse if many of its coefficients are zero.
The interest in sparsity arises because its exploitation can lead to enormous computational savings and because many large matrix problems that occur in practice are sparse.
So we can use simple models like logistic regression
"""
from sklearn import preprocessing
import category_encoders as ce
class CategoricalFeatures:
def __init__(self, df, categorical_features, encoding_type, handle_na=False):
"""
df: pandas dataframe
categorical_feature s: list of column names, e.g. ["ord_1", "nom_0"......]
encoding_type: label, binary, ohe - one hot encoding
handle_na: True/False
"""
self.df = df
self.cat_feats = categorical_features
self.enc_type = encoding_type
self.handle_na = handle_na
self.label_encoders = dict()
self.binary_encoders = dict()
self.ohe = None
self.target = self.df.iloc[:,:-1]
if self.handle_na:
for c in self.cat_feats:
self.df.loc[:, c] = self.df.loc[:, c].astype(str).fillna("-9999999")
self.output_df = self.df.copy(deep=True)
def _label_encoding(self):
for c in self.cat_feats:
lbl = preprocessing.LabelEncoder()
lbl.fit(self.df[c].values)
self.output_df.loc[:, c] = lbl.transform(self.df[c].values)
self.label_encoders[c] = lbl
return self.output_df
def _count_encoding(self):
count_enc = ce.CountEncoder()
return count_enc.fit_transform(self.df[self.cat_feats].values)
def _label_binarization(self):
for c in self.cat_feats:
lbl = preprocessing.LabelBinarizer()
lbl.fit(self.df[c].values)
val = lbl.transform(self.df[c].values)
#remove column which we did binarization and replace it we other columns starting with bin
self.output_df = self.output_df.drop(c, axis=1)
for j in range(val.shape[1]):
new_col_name = c + f"__bin_{j}"
self.output_df[new_col_name] = val[:, j]
self.binary_encoders[c] = lbl
return self.output_df
def _one_hot(self):
ohe = preprocessing.OneHotEncoder()
ohe.fit(self.df[self.cat_feats].values)
return ohe.transform(self.df[self.cat_feats].values)
def fit_transform(self):
if self.enc_type == "label":
return self._label_encoding()
elif self.enc_type == "binary":
return self._label_binarization()
elif self.enc_type == "ohe":
return self._one_hot()
elif self.enc_type == "cnt":
return self._count_encoding()
elif self.enc_type == "hel":
return self._helmert_encoding()
else:
raise Exception("Encoding type not understood")
def transform(self, dataframe):
if self.handle_na:
for c in self.cat_feats:
dataframe.loc[:, c] = dataframe.loc[:, c].astype(str).fillna("-9999999")
if self.enc_type == "label":
for c, lbl in self.label_encoders.items():
dataframe.loc[:, c] = lbl.transform(dataframe[c].values)
return dataframe
elif self.enc_type == "binary":
for c, lbl in self.binary_encoders.items():
val = lbl.transform(dataframe[c].values)
dataframe = dataframe.drop(c, axis=1)
for j in range(val.shape[1]):
new_col_name = c + f"__bin_{j}"
dataframe[new_col_name] = val[:, j]
return dataframe
elif self.enc_type == "ohe":
return self.ohe(dataframe[self.cat_feats].values)
elif self.enc_type == "cnt":
return self.cnt(dataframe[self.cat_feats].values)
else:
raise Exception("Encoding type not understood")
if __name__ == "__main__":
import pandas as pd
from sklearn import linear_model
df = pd.read_csv("../input/train_cat.csv")
df_test = pd.read_csv("../input/test_cat.csv")
sample = pd.read_csv("../input/sample_submission.csv")
train_len = len(df)
#fake column to concatenate dataset
df_test["target"] = -1
#to avoid new column in test dataset, we are concatinating both datasets
full_data = pd.concat([df, df_test])
cols = [c for c in df.columns if c not in ["id", "target"]]
cat_feats = CategoricalFeatures(full_data,
categorical_features=cols,
encoding_type="ohe",
handle_na=True)
full_data_transformed = cat_feats.fit_transform()
X = full_data_transformed[:train_len, :]
X_test = full_data_transformed[train_len:, :]
clf = linear_model.LogisticRegression()
clf.fit(X, df.target.values)
preds = clf.predict_proba(X_test)[:, 1]
sample.loc[:, "target"] = preds
sample.to_csv("submission.csv", index=False)