@@ -23,21 +23,15 @@ def collect_dataset():
23
23
"master/Week1/ADRvsRating.csv" ,
24
24
timeout = 10 ,
25
25
)
26
- response .raise_for_status () # Check for HTTP errors
26
+ response .raise_for_status () # Raise an error for failed HTTP requests
27
27
lines = response .text .splitlines ()
28
- data = []
29
- for item in lines :
30
- item = item .split ("," )
31
- data .append (item )
32
- data .pop (0 ) # This is for removing the labels from the list
28
+ data = [line .split ("," ) for line in lines ]
29
+ data .pop (0 ) # Remove the labels from the list
33
30
dataset = np .matrix (data )
34
31
return dataset
35
32
except requests .exceptions .RequestException as e :
36
- print (f"Error in fetching dataset: { e } " )
37
- return None
38
- except Exception as e :
39
- print (f"Unexpected error in processing dataset: { e } " )
40
- return None
33
+ print (f"Error fetching dataset: { e } " )
34
+ return None # Return None if dataset fetching fails
41
35
42
36
43
37
def run_steep_gradient_descent (data_x , data_y , len_data , alpha , theta ):
@@ -51,15 +45,13 @@ def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta):
51
45
curr_features - alpha_ * gradient(w.r.t. feature)
52
46
"""
53
47
try :
54
- n = len_data
55
- prod = np .dot (theta , data_x .transpose ())
56
- prod -= data_y .transpose ()
48
+ prod = np .dot (theta , data_x .transpose ()) - data_y .transpose ()
57
49
sum_grad = np .dot (prod , data_x )
58
- theta = theta - (alpha / n ) * sum_grad
50
+ theta = theta - (alpha / len_data ) * sum_grad
59
51
return theta
60
52
except Exception as e :
61
- print (f"Error during gradient descent: { e } " )
62
- return None
53
+ print (f"Error in gradient descent: { e } " )
54
+ return theta # Return current theta even if an error occurs
63
55
64
56
65
57
def sum_of_square_error (data_x , data_y , len_data , theta ):
@@ -71,14 +63,13 @@ def sum_of_square_error(data_x, data_y, len_data, theta):
71
63
:return : sum of square error computed from given feature's
72
64
"""
73
65
try :
74
- prod = np .dot (theta , data_x .transpose ())
75
- prod -= data_y .transpose ()
66
+ prod = np .dot (theta , data_x .transpose ()) - data_y .transpose ()
76
67
sum_elem = np .sum (np .square (prod ))
77
68
error = sum_elem / (2 * len_data )
78
69
return error
79
70
except Exception as e :
80
71
print (f"Error in calculating sum of square error: { e } " )
81
- return None
72
+ return float ( 'inf' ) # Return infinity in case of an error
82
73
83
74
84
75
def run_linear_regression (data_x , data_y ):
@@ -87,31 +78,20 @@ def run_linear_regression(data_x, data_y):
87
78
:param data_y : contains the output (result vector)
88
79
:return : feature for line of best fit (Feature vector)
89
80
"""
90
- try :
91
- iterations = 100000
92
- alpha = 0.0001550
93
-
94
- no_features = data_x .shape [1 ]
95
- len_data = data_x .shape [0 ]
96
-
97
- theta = np .zeros ((1 , no_features ))
81
+ iterations = 100000
82
+ alpha = 0.0001550
83
+ len_data = data_x .shape [0 ] - 1
84
+ no_features = data_x .shape [1 ]
85
+ theta = np .zeros ((1 , no_features ))
98
86
87
+ try :
99
88
for i in range (iterations ):
100
89
theta = run_steep_gradient_descent (data_x , data_y , len_data , alpha , theta )
101
- if theta is None : # If gradient descent fails, exit
102
- print ("Gradient descent failed. Exiting." )
103
- return None
104
90
error = sum_of_square_error (data_x , data_y , len_data , theta )
105
- if error is None : # If error calculation fails, exit
106
- print ("Error calculation failed. Exiting." )
107
- return None
108
- if i % 1000 == 0 : # Print every 1000 iterations
109
- print (f"At Iteration { i + 1 } - Error is { error :.5f} " )
110
-
111
- return theta
91
+ print (f"At Iteration { i + 1 } - Error is { error :.5f} " )
112
92
except Exception as e :
113
- print (f"Error in linear regression: { e } " )
114
- return None
93
+ print (f"Error during linear regression: { e } " )
94
+ return theta
115
95
116
96
117
97
def mean_absolute_error (predicted_y , original_y ):
@@ -125,14 +105,14 @@ def mean_absolute_error(predicted_y, original_y):
125
105
return total / len (original_y )
126
106
except Exception as e :
127
107
print (f"Error in calculating mean absolute error: { e } " )
128
- return None
108
+ return float ( 'inf' )
129
109
130
110
131
111
def main ():
132
- """Driver function"""
112
+ """Driver function. """
133
113
data = collect_dataset ()
134
114
if data is None :
135
- print ("Failed to collect or process the dataset. Exiting." )
115
+ print ("Failed to retrieve dataset. Exiting." )
136
116
return
137
117
138
118
try :
@@ -141,16 +121,12 @@ def main():
141
121
data_y = data [:, - 1 ].astype (float )
142
122
143
123
theta = run_linear_regression (data_x , data_y )
144
- if theta is None :
145
- print ("Linear regression failed. Exiting." )
146
- return
147
-
148
124
len_result = theta .shape [1 ]
149
- print ("Resultant Feature vector: " )
125
+ print ("Resultant Feature vector : " )
150
126
for i in range (len_result ):
151
127
print (f"{ theta [0 , i ]:.5f} " )
152
128
except Exception as e :
153
- print (f"Unexpected error in main: { e } " )
129
+ print (f"Error in main execution : { e } " )
154
130
155
131
156
132
if __name__ == "__main__" :
0 commit comments