forked from HarrySSH/LinearModels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLR.py
More file actions
56 lines (37 loc) · 1.71 KB
/
Copy pathLR.py
File metadata and controls
56 lines (37 loc) · 1.71 KB
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
47
48
49
50
51
52
53
54
55
56
import numpy as np
class LinearRegression:
def __init__(self):
# Initialize the coefficients and intercept with None
# which will be calculated after fitting the model
#self.intercept =
#self.coefficients =
pass # after you have added the above lines, you can remove this line
def fit(self, X, y):
# Add a column of ones for the intercept (bias term)
# Transpose of X
# X^T * X
# Inverse of (X^T * X)
# X^T * y
# Calculate the coefficients using the Normal Equation
raise NotImplementedError("The fit method is not yet implemented") # after you have added the above lines, you can remove this line
def predict(self, X):
# Add a column of ones to match the structure used in fitting the model
# Use the calculated coefficients to make predictions
raise NotImplementedError("The predict method is not yet implemented") # after you have added the above lines, you can remove this line
def Rsquared(self, X, y):
# Predict y values using the predict method
# Total sum of squares
# Residual sum of squares
# R-squared formula
raise NotImplementedError("The Rsquared method is not yet implemented") # after you have added the above lines, you can remove this line
if __name__ == "__main__":
# Sample data
X = np.array([[1], [2], [3], [4], [5]])
y = np.array([1, 2, 3, 4, 5])
# Initialize and fit the model
model = LinearRegression()
model.fit(X, y)
# Make predictions and calculate R-squared
ypred = model.predict(X)
print(ypred) # Predicted values
print(model.Rsquared(X, y)) # R-squared value