-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResponseEncoder.py
53 lines (40 loc) · 1.62 KB
/
ResponseEncoder.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
class ResponseEncoder:
"""
Converts Categorical feature into numericals as per y/Target/Class/Dependent Feature.
Example:-
> re = ResponseEncoder()
> re.fit(X_train[feature],y)
> encoded_feature= re.transform(X_test[feature])
"""
def __init__(self):
self.table= None
self.category=None
self.target=None
def make_table(self,X,y):
"""Returns [p(Y=1|X),p(Y=2|X)...p(Y=n|X)] for each unique i in X"""
X.reset_index(drop=True, inplace=True)
y.reset_index(drop=True, inplace=True)
category = list(set(X))
target = list(set(y))
table = np.zeros((len(category),len(target)))
for i in range(len(X)):
index1 = category.index(X[i])
index2 = target.index(y[i])
table[index1,index2] += 1
table /= table.sum(axis=1).reshape(-1,1)
return table,category,target
def encode_using_table(self,X):
"""Returns [p(Y=1|X),p(Y=2|X)...p(Y=n|X)] for each i in X"""
X.reset_index(drop=True, inplace=True)
xtable = np.zeros((len(X),len(self.target)))
for i in range(len(X)):
if X[i] in self.category:
xtable[i,:] = self.table[self.category.index(X[i]),:]
else:
xtable[i,:] = (1/len(self.target))*np.ones((len(self.target)))
return xtable
def fit(self,X,y):
assert len(X) == len(y), "Both Inputs must be equal in size"
self.table, self.category, self.target = self.make_table(X,y)
def transform(self, X):
return self.encode_using_table(X)