-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_selection.py
More file actions
40 lines (30 loc) · 793 Bytes
/
model_selection.py
File metadata and controls
40 lines (30 loc) · 793 Bytes
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
import numpy as np
from numpy.linalg import inv
import matplotlib.pyplot as plt
model = 9
'''True Funtion'''
x_true = np.linspace(0,1,100)
y_true = np.sin(2*np.pi*x_true)
'''Polynomial Feature'''
def feature(x,degree):
X = np.empty([x.shape[0], 0])
for i in range(degree+1):
X = np.concatenate((X,np.power(x,i)),axis=1)
return X
x = np.linspace(0, 1, 10)
y = np.sin(2*np.pi*x)
t = y + np.random.normal(scale=0.25, size=x.shape)
t = t.T
x.resize(len(x),1)
X = feature(x,model)
w = inv(X.T.dot(X)).dot(X.T).dot(t)
x_t = np.linspace(0,1,100)
x_t.resize(len(x_t),1)
x_t_feature = feature(x_t,model)
y_t = x_t_feature.dot(w.T)
plt.plot(x_t,y_t,c="r")
plt.scatter(x,t)
plt.plot(x_true,y_true,c="g")
plt.show()
np.set_printoptions(suppress=True)
print(w)