-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
74 lines (66 loc) · 1.88 KB
/
common.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
import numpy.random
import cvxpy as cp
import math
class Util():
@staticmethod
def combineSets(a,b):
return numpy.concatenate((a,b), axis=0)
@staticmethod
def dot(w, cartisian, dimensions=2):
products = list()
for i in range(dimensions):
products.append(cp.multiply(w[i], cartisian[i]))
return cp.sum(products)
@staticmethod
def norm(U):
normalization = 0.0
for u in U:
normalization += u**2
normalization = math.sqrt(normalization)
return normalization
@staticmethod
def sign(number):
if number < 0:
return 1
if number >= 0:
return -1
class RandomData():
@staticmethod
def __random_gauss__(mean, covMatrix, size=200):
numpy.random.seed(seed=4789)
return numpy.random.multivariate_normal(mean, covMatrix, size)
@staticmethod
def random_data(size=200):
mean1 = (-1, -1)
mean2 = (1, 1)
covariance = [
[1,0],
[0,1]
]
randomSet1 = RandomData.__random_gauss__(mean1, covariance, size)
randomSet2 = RandomData.__random_gauss__(mean2, covariance, size)
fullSet = Util.combineSets(randomSet1, randomSet2)
X, Y = fullSet.T
return randomSet1, randomSet2, X, Y, fullSet
@staticmethod
def log_labels(size=400):
labels = []
mid_point = size / 2
for i in range(size):
lab = 0
if i < mid_point:
labels.append(0)
else:
labels.append(1)
return labels
@staticmethod
def linear_labels(size=400):
labels = []
mid_point = size / 2
for i in range(size):
lab = 0
if i < mid_point:
labels.append(-1)
else:
labels.append(1)
return labels