-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile1.py
46 lines (32 loc) · 1.09 KB
/
file1.py
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
41
42
43
44
45
46
import numpy as np
from math import sin
from utils import load_data, save_sklearn_model, evaluate_predictions
from sklearn.linear_model import LinearRegression
# Load the data
data_path = '../data/data.npz'
x, y = load_data(data_path)
# Initialize the regressor
linear_regressor = LinearRegression(fit_intercept = True)
# Number of data points
n = len(x)
# Initialize array
x3 = np.empty((0, n))
# Append x3
for x1, x2 in x:
temp = sin(x1) * x2
x3 = np.append(x3, temp)
# Build the X matrix
X = np.insert(x, 2, x3, axis = 1)
# Fit the model
linear_regressor.fit(X, y)
# Get the parameters
theta0 = linear_regressor.intercept_
theta1, theta2, theta3 = linear_regressor.coef_
print("\nThe parameter values are: theta0 = {}, theta1 = {}, theta2 = {}, theta3 {}.".format(theta0, theta1, theta2, theta3))
# Make the predictions of the model
y_pred = linear_regressor.predict(X)
# Print the prediction
MSE = evaluate_predictions(y_pred, y)
print("Task 1 Linear Rregression Model MSE: {}\n".format(MSE))
# Save the model
save_sklearn_model(linear_regressor, '../deliverable/Linear_Regression.pickle')