Quiz 3 (q13-15, q18-20)
>>> # Q13-15 1st order and feature-transformed(2nd order) Linear Regression
>>> import random, math
>>> from pylab import*
>>> def training_set(N,noise,x_l,x_r): # Generates noisy quadratic-separable binary class. data points.
x_1 = []
x_2 = []
y = []
for i in range(N):
x1 = uniform(x_l,x_r) # The bug with 'uniform([,])' induced interesting stuff: 1).random.choice() vs. numpy.random.choice(); 2).'sign(array)' gives an array of
x2 = uniform(x_l,x_r) # signs! 3).'a has to be 1-dimensional' for numpy's choice()!!!
s = sign(x1**2 + x2**2 - 0.6)
y_noise = choice([s,-s], p = [1-noise, noise])
x_1.append(x1)
x_2.append(x2)
y.append(y_noise)
return [x_1,x_2,y]
>>> def lin_reg(D): # D is the output of the function above.
x_1 = D[0]
x_2 = D[1]
y = array(D[2])
x0 = 1
N = len(y)
input_matrix = []
for i in range(N):
x_n = [x0,x_1[i],x_2[i]]
input_matrix.append(x_n)
X = matrix(input_matrix) # 'numpy.matrix([row1,row2...])'
X_psu_inv = linalg.pinv(X) # 'numpy.linalg.pinv(M)' for getting the pseudo inverse of M
w_lin = matmul(X_psu_inv, y) # 'numpy.matmul()' can have arguments as matrix,array or lists, it returns arrays if arguments are lists/arrays.
a = array(w_lin) # To reduce the output matrix to an array that contains only 1 array as its element.
w_lin = a[0]
return w_lin
>>> def E_in(w, D):
x_1 = D[0]