-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.py
101 lines (73 loc) · 2.53 KB
/
funcs.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
import numpy as np
##
# recaling
# scaling and rescaling
# scale x by given xmean and xstd
def rescale_by(x, xmean, xstd):
return (x - xmean) / xstd
#compute xmean and xstd
#and scale x
def rescale(x):
xmean = x.mean(axis=0)
xstd = x.std(axis=0)
x_norm = rescale_by(x, xmean, xstd)
return x_norm, xmean, xstd
# scale back parameter
def back_scale_parameter(theta, xmean, xstd):
t = np.ones(theta.size)
t[0] = theta[0] - (theta[1:] * xmean / xstd).sum()
t[1:] = theta[1:] / xstd
return t
def scale_parameter(theta, xmean, xstd):
t = np.ones(theta.size)
t[0] = theta[0] + (theta[1:] * xmean).sum()
t[1:] = theta[1:] * xstd
return t
##### for logistic regression ####
def logistic_function(x):
return 1. / (1. + np.exp(-x))
def logistic_hypothesis(theta):
return lambda X: logistic_function(X.dot(theta))
def cross_entropy_loss(h, x, y):
return lambda theta: -y * np.log(h(theta)(x)) - (1-y) * np.log(1. - h(theta)(x))
##### for linear regression ####
def linear_hypothesis(theta):
return lambda X: X.dot(theta)
def squared_error_loss(h, x, y):
return lambda theta: 1./2. * (h(theta)(x) - y)**2
def sin_transform(x):
z = x.reshape((len(x), -1))
return np.power(z, np.arange(1, 6))
# didactic code to demonstrate gradient decent
# not optimized for speed
def cost_function(X, y, h, l, loss):
m = len(y)
return lambda theta: 1./(float(m)) * (loss(h,X,y)(theta).sum() + np.power(theta, 2).sum()*l)
# add x_0 for all x(Feature Vectors)
def addColumnZero(x):
m = len(x)
n = x[0].size + 1
X = np.ones(shape=(m,n))
X[:,1:] = x
return X
def compute_new_theta(X, y, theta, alpha, hypothesis, l):
m = len(X.T[0])
h0 = hypothesis(theta[0])(X.T[0])
h = hypothesis(theta[1:])(X.T[1:].T)
theta[0] = theta[0] - alpha * (1.0 / float(m)) * np.sum(X.T[0] * (h0 - y))
theta[1:] = theta[1:]*(1-alpha*(l/float(m))) - (alpha/float(m)) * X.T[1:].dot(h - y)
return theta
def gradient_descent(X, y, theta_, alpha, num_iters, hypothesis, l, loss):
x_norm ,xmean, xstd = rescale(X)
theta = scale_parameter(theta_, xmean, xstd)
X = addColumnZero(x_norm)
assert X.T[0].size == theta.size
cost = cost_function(X, y, hypothesis, l, loss)
for i in xrange(num_iters):
theta = compute_new_theta(X, y, theta, alpha, hypothesis, l)
return back_scale_parameter(theta, xmean, xstd), cost(theta)
def create_multi_data(func, point_count, r, min, max):
s = np.random.normal(0, r, point_count)
x_values = np.random.uniform(min, max, point_count)
y_values = func(x_values) + s
return x_values, y_values