-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
66 lines (38 loc) · 1.5 KB
/
utils.py
File metadata and controls
66 lines (38 loc) · 1.5 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
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
# Implementation of functions for data manipulation for machine learning programs.
# Author: Eunice Ofori-Addo
# Various tools for data manipulation.
# Z transformation function. Support the pocket version for linearly non separable data.
import numpy as np
import math
class MyUtils:
def z_transform(X, degree = 2):
''' Transforming traing samples to the Z space
X: n x d matrix of samples, excluding the x_0 = 1 feature
degree: the degree of the Z space
return: the n x d' matrix of samples in the Z space, excluding the z_0 = 1 feature.
It can be mathematically calculated: d' = \sum_{k=1}^{degree} (k+d-1) \choose (d-1)
'''
r = degree
if r == 1:
return X
Z = X.copy()
#r = degree
n,d = X.shape
B = []
for i in range(r):
B.append(math.comb(d+i, d-1))
l = np.arange(np.sum(B))
q = 0
p = d
g = d
for i in range(1,r):
for j in range(q, p):
head = l[j]
for k in range(head, d):
temp = (Z[:,j] * X[:,k]).reshape(-1,1)
Z = np.append(Z, temp, axis = 1)
l[g] = k
g = g + 1
q = p
p = p + B[i]
return Z