@@ -31,85 +31,32 @@ def collect_dataset():
31
31
return dataset
32
32
33
33
34
- def run_steep_gradient_descent (data_x , data_y , len_data , alpha , theta ):
35
- """Run steep gradient descent and updates the Feature vector accordingly_
36
- :param data_x : contains the dataset
37
- :param data_y : contains the output associated with each data-entry
38
- :param len_data : length of the data_
39
- :param alpha : Learning rate of the model
40
- :param theta : Feature vector (weight's for our model)
41
- ;param return : Updated Feature's, using
42
- curr_features - alpha_ * gradient(w.r.t. feature)
43
- """
44
- n = len_data
45
-
46
- prod = np .dot (theta , data_x .transpose ())
47
- prod -= data_y .transpose ()
48
- sum_grad = np .dot (prod , data_x )
49
- theta = theta - (alpha / n ) * sum_grad
50
- return theta
51
-
52
-
53
- def sum_of_square_error (data_x , data_y , len_data , theta ):
54
- """Return sum of square error for error calculation
55
- :param data_x : contains our dataset
56
- :param data_y : contains the output (result vector)
57
- :param len_data : len of the dataset
58
- :param theta : contains the feature vector
59
- :return : sum of square error computed from given feature's
60
- """
61
- prod = np .dot (theta , data_x .transpose ())
62
- prod -= data_y .transpose ()
63
- sum_elem = np .sum (np .square (prod ))
64
- error = sum_elem / (2 * len_data )
65
- return error
66
-
67
-
68
- def run_linear_regression (data_x , data_y ):
69
- """Implement Linear regression over the dataset
70
- :param data_x : contains our dataset
71
- :param data_y : contains the output (result vector)
34
+ def run_linear_regression_ols (data_x , data_y ):
35
+ """Implement Linear regression using OLS over the dataset
36
+ :param data_x : contains our dataset
37
+ :param data_y : contains the output (result vector)
72
38
:return : feature for line of best fit (Feature vector)
73
39
"""
74
- iterations = 100000
75
- alpha = 0.0001550
76
-
77
- no_features = data_x .shape [1 ]
78
- len_data = data_x .shape [0 ] - 1
40
+ # Add a column of ones to data_x for the bias term
41
+ data_x = np .c_ [np .ones (data_x .shape [0 ]), data_x ].astype (float )
79
42
80
- theta = np .zeros ((1 , no_features ))
81
-
82
- for i in range (iterations ):
83
- theta = run_steep_gradient_descent (data_x , data_y , len_data , alpha , theta )
84
- error = sum_of_square_error (data_x , data_y , len_data , theta )
85
- print (f"At Iteration { i + 1 } - Error is { error :.5f} " )
43
+ # Use NumPy's built-in function to solve the linear regression problem
44
+ theta = np .linalg .inv (data_x .T .dot (data_x )).dot (data_x .T ).dot (data_y )
86
45
87
46
return theta
88
47
89
48
90
- def mean_absolute_error (predicted_y , original_y ):
91
- """Return sum of square error for error calculation
92
- :param predicted_y : contains the output of prediction (result vector)
93
- :param original_y : contains values of expected outcome
94
- :return : mean absolute error computed from given feature's
95
- """
96
- total = sum (abs (y - predicted_y [i ]) for i , y in enumerate (original_y ))
97
- return total / len (original_y )
98
-
99
-
100
49
def main ():
101
50
"""Driver function"""
102
51
data = collect_dataset ()
103
-
104
- len_data = data .shape [0 ]
105
- data_x = np .c_ [np .ones (len_data ), data [:, :- 1 ]].astype (float )
52
+ data_x = data [:, :- 1 ].astype (float )
106
53
data_y = data [:, - 1 ].astype (float )
107
54
108
- theta = run_linear_regression (data_x , data_y )
109
- len_result = theta . shape [ 1 ]
110
- print ( "Resultant Feature vector : " )
111
- for i in range (len_result ):
112
- print (f"{ theta [ 0 , i ]:.5f} " )
55
+ theta = run_linear_regression_ols (data_x , data_y )
56
+ print ( "Resultant Feature vector (weights): " )
57
+ theta_list = theta . tolist ()[ 0 ]
58
+ for i in range (len ( theta_list ) ):
59
+ print (f"{ theta_list [ i ]:.5f} " )
113
60
114
61
115
62
if __name__ == "__main__" :
0 commit comments