-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.py
More file actions
30 lines (22 loc) · 672 Bytes
/
1.py
File metadata and controls
30 lines (22 loc) · 672 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
import numpy as np
import matplotlib.pyplot as plt
trainCSV = np.genfromtxt('quasar_train.csv',delimiter=",")
lambdas = trainCSV[0,:]
train = trainCSV[1:,:]
test = np.genfromtxt('quasar_test.csv',delimiter=",",skip_header=1)
mm = lambdas.size
y = train[0,:]
y.reshape(y.size,1)
x1 = np.ones((mm,1))
x2 = lambdas.reshape((mm,1))
X = np.hstack((x1,x2))
XtX = (X.T).dot(X)
XtX_inv = np.linalg.inv( XtX )
theta = (XtX_inv).dot(X.T).dot(y)
plt.figure(1, figsize=(8, 4))
plt.scatter( x2, y, marker="x", c="red", s=2 )
plt.ylabel('Intensity')
plt.xlabel("Wavelength")
plt.title("Some noisy spectrum with an OLS regression")
plt.plot( x2, X.dot(theta), "b-", )
plt.show()