Skip to content

Commit 578bec2

Browse files
Kyle KastnerKyle Kastner
authored andcommitted
Added code for conference
1 parent f99f188 commit 578bec2

File tree

6 files changed

+258
-0
lines changed

6 files changed

+258
-0
lines changed

gradient_linear_regression.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/python
2+
import numpy as np
3+
import matplotlib.pyplot as plot
4+
import math
5+
from sklearn import datasets
6+
7+
X,y = datasets.make_regression(n_samples=1000,n_features=1,n_informative=1,noise=15,bias=1000)
8+
#Ensure y array is 2D
9+
y = y[:,np.newaxis]
10+
11+
#Add bias terms
12+
X = np.hstack((np.ones((X.shape[0],1)), X))
13+
14+
#Initialize theta to zeros
15+
theta = np.zeros((X.shape[1],1))
16+
alpha = 0.01
17+
iters = 1000
18+
19+
def linear_cost(theta,X,y):
20+
m = y.shape[0]
21+
return 1./(2.*m)*np.sum((np.dot(X,theta)-y)**2.)
22+
23+
def linear_cost_grad(theta,X,y):
24+
m = y.shape[0]
25+
return 1./m*np.dot(X.T,(y-np.dot(X,theta)))
26+
27+
def gradient_descent(theta,X,y,alpha,iters):
28+
m = y.shape[0]
29+
all_cost = []
30+
print theta.shape
31+
for i in range(iters):
32+
all_cost.append(linear_cost(theta,X,y))
33+
theta += float(alpha)*linear_cost_grad(theta,X,y)
34+
return theta,all_cost
35+
36+
theta,all_cost = gradient_descent(theta,X,y,alpha,iters)
37+
plot.figure()
38+
plot.title("Cost vs. number of iterations")
39+
plot.plot(range(len(all_cost)),all_cost)
40+
plot.figure()
41+
plot.title("Linear Regression")
42+
plot.plot(X[:,1], y, 'rx')
43+
plot.plot(X[:,1], np.dot(X,theta))
44+
plot.show()

logistic_regression.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/python
2+
import numpy as np
3+
import scipy.optimize as opt
4+
from scipy.io import loadmat
5+
import matplotlib.pyplot as plot
6+
7+
def sigmoid(X):
8+
return 1./(1.+np.exp(-X))
9+
10+
def logistic_cost(theta, X, y):
11+
m = y.shape[0]
12+
if len(theta.shape) < 2:
13+
theta = theta[:,np.newaxis]
14+
h = sigmoid(np.dot(X,theta))
15+
#THE LINES BELOW DO NOT WORK VERY WELL
16+
#POSSIBLE NUMERICAL ISSUES?
17+
#Direct versions with distributed minus
18+
#return np.ravel(1./m*np.sum(-y*np.log(h) - (1.-y)*np.log(1.-h)))
19+
#return np.ravel(1./m*(np.dot(-y.T,np.log(h)) - np.dot((1.-y.T),np.log(1.-h))))
20+
21+
#PARAMETERIZATIONS THAT WORK
22+
#return np.ravel(-1./m*(np.dot(y.T,np.log(h)) + np.dot((1.-y).T,np.log(1.-h))))
23+
#Chose this one since it seems the most direct
24+
return np.ravel(-1./m*np.sum(y*np.log(h) + (1.-y)*np.log(1.-h)))
25+
26+
def logistic_cost_grad(theta, X, y):
27+
m = y.shape[0]
28+
if len(theta.shape) < 2:
29+
theta = theta[:,np.newaxis]
30+
h = sigmoid(np.dot(X,theta))
31+
return np.ravel(1./m*np.dot(X.T,h-y))
32+
33+
def logistic_cost_reg(theta, X, y, l=.1):
34+
m = y.shape[0]
35+
if len(theta.shape) < 2:
36+
theta = theta[:,np.newaxis]
37+
reg = l/(2.*m)*np.sum(theta[1:,:]**2.)
38+
return np.ravel(logistic_cost(theta,X,y) + reg)
39+
40+
def logistic_cost_reg_grad(theta, X, y, l=.1):
41+
m = y.shape[0]
42+
if len(theta.shape) < 2:
43+
theta = theta[:,np.newaxis]
44+
reg = float(l)/m*np.sum(np.vstack((np.zeros(1), theta[1:,:])))
45+
return np.ravel(logistic_cost_grad(theta,X,y) + reg)
46+
47+
from sklearn import datasets
48+
digits = datasets.load_digits()
49+
#These digits are randomly ordered, if given an ORDERED set
50+
#of training data make sure to randomize before splitting
51+
#Normalize between 0 and 1
52+
X = digits.data/255.
53+
#Subtract the mean, though it doesn't appear to make a big difference
54+
X = X - np.mean(X)
55+
y = digits.target.T[:,np.newaxis]
56+
#Add bias terms
57+
X = np.hstack((np.ones((X.shape[0],1)), X))
58+
#Expect 10 labels for digits
59+
num_labels = np.amax(y)-np.amin(y)+1
60+
all_theta = np.zeros((X.shape[1],num_labels))
61+
for c in range(num_labels):
62+
#Initialize theta to zeros and pass into optimization routine
63+
theta0 = np.zeros((X.shape[1],))
64+
#fmin_cg requires the cost and cost_grad functions to return flattened 1D arrays!
65+
#theta = opt.fmin_cg(logistic_cost, theta0, fprime=logistic_cost_grad, args=(X, (y == c)), maxiter=50)
66+
theta = opt.fmin_cg(logistic_cost_reg, theta0, fprime=logistic_cost_reg_grad, args=(X, (y == c)), maxiter=50)
67+
all_theta[:,c] = theta
68+
69+
#We can use the builtin check_grad function to perform numerical gradient
70+
#checking, ensuring the gradient code is correct for the cost
71+
#print opt.check_grad(logistic_cost, logistic_cost_grad, theta, X, y)
72+
h = sigmoid(np.dot(X, all_theta))
73+
pred = np.argmax(h,axis=1)[:,np.newaxis]
74+
print "Classification accuracy(%):" + `100*np.sum((y == pred))/float(len(y))`
75+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/python
2+
import numpy as np
3+
import matplotlib.pyplot as plot
4+
import math
5+
6+
7+
N = 10
8+
noise_var = B = 5
9+
xs = np.matrix(range(N)).T
10+
ys = np.square(xs) - 4*xs + 1
11+
wm = ys + np.sqrt(B)*np.random.randn(N,1)
12+
13+
def gen_dft(m, n, N):
14+
return np.exp(1j*-2*m*n/N)
15+
16+
def gen_polynomial(x, m):
17+
return np.power(x, m)
18+
19+
N_basis = 3
20+
#basis_func = np.vectorize(gen_dft)
21+
#basis = basis_func(xs, np.arange(N_basis), N).T
22+
basis_func = np.vectorize(gen_polynomial)
23+
basis = basis_func(xs, np.arange(N_basis)).T
24+
test_data = t = basis*wm
25+
#Calculate the Moore-Penrose pseudoinverse using the following formula
26+
#maximum_likelihood = wml = np.linalg.inv(basis.T*basis)*basis.T*t
27+
#Direct calculation appears to have numerical instability issues...
28+
#Luckily the pinv method calculates Moore-Penrose pseudo inverse using SVD, which largely avoids the numerical issues
29+
maximum_likelihood = wml = np.linalg.pinv(basis)*t
30+
31+
plot.figure()
32+
plot.title("Regression fit using polynomial basis function, number of basis functions = $" + `N_basis` + "$")
33+
plot.plot(ys, 'b')
34+
plot.plot(wm, 'ro')
35+
plot.plot(np.real(wml), 'g')
36+
plot.show()

two_unknowns.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/python
2+
import matplotlib.pyplot as plot
3+
import numpy as np
4+
5+
total_obs = 1000
6+
primary_mean = 5.
7+
primary_var = 4.
8+
x = np.sqrt(primary_var)*np.random.randn(total_obs) + primary_mean
9+
f, axarr = plot.subplots(3)
10+
f.suptitle("Unknown mean ($\mu=$"+`primary_mean`+"), unknown variance ($\sigma^2=$"+`primary_var`+")")
11+
y0label = "Timeseries"
12+
y1label = "Estimate for mean"
13+
y2label = "Estimate for variance"
14+
axarr[0].set_ylabel(y0label)
15+
axarr[1].set_ylabel(y1label)
16+
axarr[2].set_ylabel(y2label)
17+
axarr[0].plot(x)
18+
prior_mean = 0.
19+
prior_var = 1.
20+
prior_kappa = 1.
21+
prior_v = 0.
22+
all_mean_guess = []
23+
all_var_guess = []
24+
for i in range(total_obs):
25+
posterior_mean = (prior_kappa*prior_mean+x[i])/(prior_kappa + 1)
26+
posterior_var = (prior_v*prior_var + prior_kappa/(prior_kappa + 1)*(x[i]-prior_mean)**2)/(prior_v + 1)
27+
prior_kappa += 1
28+
prior_v += 1
29+
all_mean_guess.append(posterior_mean)
30+
all_var_guess.append(posterior_var)
31+
prior_mean = posterior_mean
32+
prior_var = posterior_var
33+
34+
axarr[1].plot(all_mean_guess)
35+
axarr[2].plot(all_var_guess)
36+
plot.show()

unknown_mean.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/python
2+
import numpy as np
3+
import matplotlib.pyplot as plot
4+
total_obs = 1000
5+
primary_mean = 5.
6+
primary_var = known_var = 4.
7+
x = np.sqrt(primary_var)*np.random.randn(total_obs) + primary_mean
8+
f, axarr = plot.subplots(3)
9+
f.suptitle("Unknown mean ($\mu=$"+`primary_mean`+"), known variance ($\sigma^2=$"+`known_var`+")")
10+
y0label = "Timeseries"
11+
y1label = "Estimate for mean"
12+
y2label = "Doubt in estimate"
13+
axarr[0].set_ylabel(y0label)
14+
axarr[1].set_ylabel(y1label)
15+
axarr[2].set_ylabel(y2label)
16+
axarr[0].plot(x)
17+
prior_mean = 0.
18+
prior_var = 1000000000000.
19+
all_mean_guess = []
20+
all_mean_doubt = []
21+
for i in range(total_obs):
22+
posterior_mean_doubt = 1./(1./known_var+1./prior_var)
23+
posterior_mean_guess = (prior_mean/prior_var+x[i]/known_var)*posterior_mean_doubt
24+
all_mean_guess.append(posterior_mean_guess)
25+
all_mean_doubt.append(posterior_mean_doubt)
26+
prior_mean=posterior_mean_guess
27+
prior_var=posterior_mean_doubt
28+
29+
axarr[1].plot(all_mean_guess)
30+
axarr[2].plot(all_mean_doubt)
31+
plot.show()

unknown_variance.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/python
2+
import numpy as np
3+
import matplotlib.pyplot as plot
4+
total_obs = 1000
5+
primary_mean = known_mean = 5
6+
primary_var = 4
7+
x = np.sqrt(primary_var)*np.random.randn(total_obs) + primary_mean
8+
all_a = []
9+
all_b = []
10+
all_prec_guess = []
11+
all_prec_doubt = []
12+
prior_a=1/2.+1
13+
prior_b=1/2.*np.sum((x[0]-primary_mean)**2)
14+
f,axarr = plot.subplots(3)
15+
f.suptitle("Known mean ($\mu=$"+`known_mean`+"), unknown variance ($\sigma^2=$"+`primary_var`+"; $\lambda$="+`1./primary_var`+")")
16+
y0label = "Timeseries"
17+
y1label = "Estimate for precision"
18+
y2label = "Doubt in estimate"
19+
axarr[0].set_ylabel(y0label)
20+
axarr[1].set_ylabel(y1label)
21+
axarr[2].set_ylabel(y2label)
22+
23+
axarr[0].plot(x)
24+
for i in range(1,total_obs):
25+
posterior_a=prior_a+1/2.
26+
posterior_b=prior_b+1/2.*np.sum((x[i]-known_mean)**2)
27+
all_a.append(posterior_a)
28+
all_b.append(posterior_b)
29+
all_prec_guess.append(posterior_a/posterior_b)
30+
all_prec_doubt.append(posterior_a/(posterior_b**2))
31+
prior_a=posterior_a
32+
prior_b=posterior_b
33+
34+
axarr[1].plot(all_prec_guess)
35+
axarr[2].plot(all_prec_doubt)
36+
plot.show()

0 commit comments

Comments
 (0)