-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_regression_nonlinear_test.py
83 lines (71 loc) · 2.11 KB
/
linear_regression_nonlinear_test.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
from linear_regression import Regression
import math
import random
import numpy as np
def random_point():
'''
Returns a random 2-dimensional vector of floats between -1 and +1
'''
return [random.uniform(-1., 1.), random.uniform(-1., 1.)]
def target_f(x1, x2):
'''
Nonlinear target function f(x1, x2) = sign(x1**2 + x2**2 - 0.6)
'''
return math.copysign(1.0, (x1**2 + x2**2 - 0.6))
def generate_dataset(n):
'''
Takes n=total number of datapoints to generate
Returns a length n list of tuples (x, y) with x a random vector and y=f(x)
with a 10% random noise
'''
data = []
for c in range(n):
x = random_point()
y = target_f(x[0], x[1])
if random.uniform(0., 1.) <= 0.1:
y = -y
data.append((x, y))
return data
def experiment1(n):
'''
Runs the experiment on n data points without transformations
Returns the in-sample error
'''
r = Regression(2)
total_Ein = 0.0
for run in range(1000):
data = generate_dataset(n)
r.reset(data)
r.solve()
total_Ein += r.classification_error(r.data)
avg_Ein = total_Ein / 1000
return avg_Ein
def experiment2(n):
'''
Runs the experiment on n data points with the feature vector (1, x1, x2, x1x2, x1**2, x2**2)
Returns the weights of the solution and the out-of-sample error
'''
r = Regression(5)
total_weights = np.zeros(6)
total_Eout = 0.0
for run in range(1000):
data = generate_dataset(n)
for point in data:
point[0].extend([point[0][0]*point[0][1], point[0][0]**2, point[0][1]**2])
r.reset(data)
r.solve()
total_weights += r.weights[0]
new_data = generate_dataset(n)
for point in new_data:
point[0].extend([point[0][0]*point[0][1], point[0][0]**2, point[0][1]**2])
total_Eout += r.classification_error(new_data)
avg_weights = total_weights / 1000
avg_Eout = total_Eout / 1000
return (avg_weights, avg_Eout)
#Experiments
'''
results = experiment1(1000)
print(results)
'''
results = experiment2(1000)
print(results)