-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtools.py
153 lines (132 loc) · 4.28 KB
/
tools.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from random import uniform
from random import randint
from numpy import array
from numpy.linalg import pinv as pinv # pseudo inverse aka dagger
from numpy import dot
from numpy import eye
from numpy import size
def data_interval(low_b,high_b,N=100):
'returns a list of N values uniformly distributed between low boundary and high boundary'
d = []
for i in range(N):
d.append(uniform(low_b,high_b))
return d
def data(N = 10):
'return N random points (x1,x2)'
d = []
for i in range(N):
x = uniform(-1,1)
y = uniform(-1,1)
d.append([x,y])
return d
def data_from_file(filepath):
'from a filepath returns a dataset with the form [[x1,x2],y]'
datafile = open(filepath, 'r')
data = []
for line in datafile:
split = line.split()
x1 = float(split[0])
x2 = float(split[1])
y = float(split[2])
data.append([ [x1,x2],y ])
return data
def randomline():
'computes a random line and returns [a,b] : y = ax + b'
x1 = uniform(-1,1)
y1 = uniform(-1,1)
x2 = uniform(-1,1)
y2 = uniform(-1,1)
a = abs(x1-x2)/abs(y1-y2)
b = y1 - a*x1
return [a,b] # a*x + b
def target_function(coords):
'from a coordinate input [a,b] returns the function a*x + b'
f = lambda x: coords[0]*x + coords[1]
return f
def target_random_function(coords):
'''
description: from a coordinate (coords) with the format [a,b] generated a random function.
- coord: a list of the form [a,b]
- returns: the generated random function that takes as argument a list with the form [x,y]
and returns 1 or -1 whether y is below the linear function defined by a*x + b or above.
'''
func = target_function(coords)
def f(X):
x = X[0]
y = X[1]
if func(x) < y:
return 1.0
else:
return -1.0
return f
def signex(x,compare_to = 0):
'returns +1 or -1 by comparing (x) to (compare_to) param (by default = 0)'
if x > compare_to:
return +1.
else:
return -1.
def sign(x,compare_to = 0):
'returns +1 or -1 by comparing (x) to (compare_to) param (by default = 0)'
if x > compare_to:
return +1.
else:
return -1.
def map_point(point,f):
'maps a point (x1,x2) to a sign -+1 following function f '
x1 = point[0]
y1 = point[1]
y = f(x1)
compare_to = y1
return sign(y,compare_to)
def map_point_fmultipleparams(point,f):
y1 = point[1]
y = f(point)
compare_to = y1
return sign(y,compare_to)
def build_training_set(data, func):
t_set = []
for i in range(len(data)):
point = data[i]
y = map_point(point,func)
t_set.append([ [ 1.0, point[0],point[1] ] , y ])
return t_set
def build_training_set_fmultipleparams(data,func):
t_set = []
for i in range(len(data)):
point = data[i]
y = map_point_fmultipleparams(point,func)
t_set.append([ [ 1.0, point[0],point[1] ] , y ])
return t_set
def print_avg(name,vector):
print 'Average %s: %s'%(name,sum(vector)/(len(vector)*1.0))
def target_vector(t_set):
'creates a numpy array (eg a Y matrix) from the training set'
y = array([t[1] for t in t_set])
return y
def input_data_matrix(t_set):
'creates a numpy array (eg a X matrix) from the training set'
X = array([t[0] for t in t_set])
return X
def pseudo_inverse(X):
'dagger of pseudo matrix used for linear regression'
return pinv(X)
def linear_regression(N_points,t_set):
'''Linear regresion algorithm
from Y and X compute the dagger or pseudo matrix
return the Xdagger.Y as the w vector
default lambda is 1.0
'''
y_vector = target_vector(t_set)
X_matrix = input_data_matrix(t_set)
X_pseudo_inverse = pseudo_inverse(X_matrix)
return dot(dot(X_pseudo_inverse,X_matrix.T),y_vector),X_matrix,y_vector
def linear_regression_lda(N_points,t_set,lda):
'''Linear regresion algorithm
from Y and X compute the dagger or pseudo matrix
return the Xdagger.Y as the w vector
default lambda is 1.0
'''
y_vector = target_vector(t_set)
X_matrix = input_data_matrix(t_set)
X_pseudo_inverse = pseudo_inverse(dot(X_matrix.T,X_matrix)+lda*eye(size(X_matrix,1)))
return dot(dot(X_pseudo_inverse,X_matrix.T),y_vector),X_matrix,y_vector