Created
May 14, 2019 01:07
-
-
Save ven-kyoshiro/2ada11a061ba69465cbd0808177013b5 to your computer and use it in GitHub Desktop.
This is a sample code of exercise in https://esslab.jp/~ess/ja/teaching/2019/cgo/.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
import numpy as np | |
class PolynomialPredictor(object): | |
def __init__(self,u,v,n=1,debug=True): | |
self.n = n | |
u = np.array(u) | |
A = np.array([u**i for i in range(self.n,-1,-1)]).T | |
b = np.array([v]).T | |
self.alpha = np.matmul(np.matmul( np.linalg.inv( np.matmul(A.T,A) ), A.T ), b ) | |
self.sum_of_se = np.sum((np.matmul(A,np.fliplr(self.alpha))-b)**2) | |
if debug: | |
pprint.pprint('n') | |
pprint.pprint(n) | |
pprint.pprint('u') | |
pprint.pprint(u) | |
pprint.pprint('A') | |
pprint.pprint(A) | |
pprint.pprint('b') | |
pprint.pprint(b) | |
pprint.pprint('alpha') | |
pprint.pprint(self.alpha) | |
pprint.pprint('pred') | |
pprint.pprint(np.matmul(A,np.fliplr(self.alpha))) | |
pprint.pprint('GT') | |
pprint.pprint(b) | |
pprint.pprint('diff') | |
pprint.pprint(np.matmul(A,np.fliplr(self.alpha))-b) | |
pprint.pprint('self.sum_of_se') | |
pprint.pprint(self.sum_of_se) | |
def __call__(self,u): | |
u = np.array(u) | |
A = np.array([u**i for i in range(self.n,-1,-1)]).T | |
return np.matmul(A,np.fliplr(self.alpha)) | |
import matplotlib.pyplot as plt | |
u = [0,1,2,3] | |
v = [1,2,0,3] | |
pps = dict([['pp'+str(n),PolynomialPredictor(u,v,n=n,debug=False)] for n in range(1,5)]) | |
print('[sum of square errors]') | |
for k in pps.keys(): | |
print(' ',k,pps[k].sum_of_se) | |
plt.scatter(u,v) | |
u = np.linspace(0,3,100) | |
for k in pps.keys(): | |
plt.plot(u, pps[k](u),label=k) | |
plt.legend() | |
_ = plt.ylim(-1,4) | |
Author
ven-kyoshiro
commented
May 14, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment