Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Insert code here
- import numpy as np
- import matplotlib.pyplot as plt
- def mserror(yþjalfun, ytest):
- r = len(ytest)
- return np.dot(yþjalfun-ytest, yþjalfun-ytest)/r
- def sigmaVal(X,y,sigma,n):
- r = len(X)
- perm = np.random.permutation(len(y))
- Xperm = X[perm]
- Yperm = y[perm]
- skref = np.int(np.floor(r/n))
- w = np.zeros(len(sigma))
- for i in range(0, len(sigma)):
- d = sigma[i]
- error = 0
- for j in np.arange(0,r,skref):
- XTimi = Xperm[j:j+skref]
- YTimi = Yperm[j:j+skref]
- XAfram = np.concatenate((Xperm[0:j], Xperm[j+skref:r]), axis=None)
- YAfram = np.concatenate((Yperm[0:j], Yperm[j+skref:r]), axis=None)
- yþjalfun = np.zeros(len(XTimi))
- poly_predict(XTimi, poly_fit(XAfram, YAfram, q))
- for k in range(0, len(XTimi)):
- yþjalfun[k] = lwr_predict(XAfram, YAfram, XTimi[k], d)
- error = error + mserror(yþjalfun, YTimi)
- w[i] = error/n
- return w
- def qVal(X,y,fylki,n):
- r = len(X)
- perm = np.random.permutation(len(y))
- Xperm = X[perm]
- Yperm = y[perm]
- skref = np.int(np.floor(r/n))
- w = np.zeros(len(fylki))
- for i in range(0,len(fylki)):
- q = fylki[i]
- error = 0
- for j in np.arange(0,r,skref):
- XTimi = Xperm[j:j+skref]
- YTimi = Yperm[j:j+skref]
- XAfram = np.concatenate((Xperm[0:j], Xperm[j+skref:r]), axis=None)
- YAfram = np.concatenate((Yperm[0:j], Yperm[j+skref:r]), axis=None)
- yþjalfun = np.zeros(len(XTimi))
- yþjalfun = poly_predict(XTimi, poly_fit(XAfram, YAfram, q))
- error = error + mserror(yþjalfun, YTimi)
- w[i] = error/n
- return w
- # Setjum inn gildi og teiknum:
- sigma = np.sort(np.random.uniform(0.05,0.25,7))
- fylki = np.array([2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
- data = np.genfromtxt('artificial.txt', delimiter=',', skip_header=1)
- n = data.shape[0]
- y = data[0:1000,-1]
- X = data[0:1000,0:-1]
- w1 = sigmaVal(X,y,sigma,n)
- w2 = qVal(X,y,fylki,n)
- plt.scatter(sigma,w1,c='r')
- plt.plot(sigma,w1,c='r')
- plt.xlabel('Sigma')
- plt.ylabel('MSError')
- plt.show()
- plt.scatter(fylki,w1,c='r')
- plt.plot(fylki,w1,c='r')
- plt.xlabel('Q')
- plt.ylabel('MSError')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement