-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrdinalEncoding.py
81 lines (75 loc) · 2.25 KB
/
OrdinalEncoding.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
import numpy as np
class ordinalEncoding:
def __init__(self):
self.inverse_dict = {}
self.main_dict = {}
def fit(self, data):
self.inverse_dict = {}
self.main_dict = {}
i = 0
for d in data:
d=tuple(d)
if not d in self.main_dict:
self.main_dict.update({d: i})
self.inverse_dict.update({i: d})
i += 1
def transform(self, data):
out = []
for d in data:
# d=tuple(d)
if d[0] in self.main_dict:
out.append(self.main_dict[d[0]])
else:
out.append(np.nan)
return np.array(out)
def fit_transform(self, data):
self.inverse_dict = {}
self.main_dict = {}
out=[]
i = 0
for d in data:
d=tuple(d)
if not d in self.main_dict:
self.main_dict.update({d: i})
self.inverse_dict.update({i: d})
out.append(i)
i += 1
else:
out.append(self.main_dict[d])
return np.array(out)
def fit_transform_largeData(self, data):
self.inverse_dict = {}
self.main_dict = {}
out=[]
i = 0
for d in data:
if not d[0] in self.main_dict:
self.main_dict.update({d[0]: i})
self.inverse_dict.update({i: d[0]})
out.append(i)
i += 1
else:
out.append(self.main_dict[d[0]])
return np.array(out)
def fit_update_transform(self, data):
out=[]
i = len(self.main_dict)
for d in data:
d=d[0]
if not d in self.main_dict:
self.main_dict.update({d: i})
self.inverse_dict.update({i: d})
out.append(i)
i += 1
else:
out.append(self.main_dict[d])
return np.array(out)
def inverse_transform(self, data):
data=np.array(data).reshape(len(data),)
out=[]
for i in data:
if i in self.inverse_dict:
out.append(self.inverse_dict[i])
else:
out.append(np.nan)
return np.array(out)