-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOneHotEncoder.py
More file actions
36 lines (31 loc) · 1.37 KB
/
Copy pathOneHotEncoder.py
File metadata and controls
36 lines (31 loc) · 1.37 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
33
34
35
36
__author__ = 'gabriel'
import numpy as np
class OneHotEncoder():
def __init__(self, categorical_matrix, na_value=-1):
'''
Returns the one-hot-encoded feature matrix of a
categorical matrix. Assumes a matrix with categorical variables,
indicated by a number from 1 to N on
every column, where N is the number of possible categories.
Resulting matrix will have the same amount of lines and
the sum of all N's as the number of columns.
'''
self.na_value = na_value
max_values = np.max(categorical_matrix, axis=0)
self.n_contextual_condition = sum(max_values)
self.n_contextual_factor = np.shape(categorical_matrix)[1]
begin_index = np.cumsum(max_values)
begin_index[-1] = 0
#indexes indicating where each contextual factor begins
self.contextual_factor_index = np.roll(begin_index, 1)
def predict(self, categorical_matrix):
n_line = np.shape(categorical_matrix)[0]
result = np.zeros((n_line, self.n_contextual_condition))
for i in range(n_line):
not_na_columns = categorical_matrix[i, :] != self.na_value
a = categorical_matrix[i, not_na_columns]
b = self.contextual_factor_index[not_na_columns]
c = a + b - 1
c = c.astype(int)
result[i, c] = 1
return result