Skip to content

Commit 1a93fd5

Browse files
committed
update linear_regression.py
1 parent 46fbd93 commit 1a93fd5

File tree

1 file changed

+25
-49
lines changed

1 file changed

+25
-49
lines changed

machine_learning/linear_regression.py

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,15 @@ def collect_dataset():
2323
"master/Week1/ADRvsRating.csv",
2424
timeout=10,
2525
)
26-
response.raise_for_status() # Check for HTTP errors
26+
response.raise_for_status() # Raise an error for failed HTTP requests
2727
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
3330
dataset = np.matrix(data)
3431
return dataset
3532
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
4135

4236

4337
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):
5145
curr_features - alpha_ * gradient(w.r.t. feature)
5246
"""
5347
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()
5749
sum_grad = np.dot(prod, data_x)
58-
theta = theta - (alpha / n) * sum_grad
50+
theta = theta - (alpha / len_data) * sum_grad
5951
return theta
6052
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
6355

6456

6557
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):
7163
:return : sum of square error computed from given feature's
7264
"""
7365
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()
7667
sum_elem = np.sum(np.square(prod))
7768
error = sum_elem / (2 * len_data)
7869
return error
7970
except Exception as e:
8071
print(f"Error in calculating sum of square error: {e}")
81-
return None
72+
return float('inf') # Return infinity in case of an error
8273

8374

8475
def run_linear_regression(data_x, data_y):
@@ -87,31 +78,20 @@ def run_linear_regression(data_x, data_y):
8778
:param data_y : contains the output (result vector)
8879
:return : feature for line of best fit (Feature vector)
8980
"""
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))
9886

87+
try:
9988
for i in range(iterations):
10089
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
10490
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}")
11292
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
11595

11696

11797
def mean_absolute_error(predicted_y, original_y):
@@ -125,14 +105,14 @@ def mean_absolute_error(predicted_y, original_y):
125105
return total / len(original_y)
126106
except Exception as e:
127107
print(f"Error in calculating mean absolute error: {e}")
128-
return None
108+
return float('inf')
129109

130110

131111
def main():
132-
"""Driver function"""
112+
"""Driver function."""
133113
data = collect_dataset()
134114
if data is None:
135-
print("Failed to collect or process the dataset. Exiting.")
115+
print("Failed to retrieve dataset. Exiting.")
136116
return
137117

138118
try:
@@ -141,16 +121,12 @@ def main():
141121
data_y = data[:, -1].astype(float)
142122

143123
theta = run_linear_regression(data_x, data_y)
144-
if theta is None:
145-
print("Linear regression failed. Exiting.")
146-
return
147-
148124
len_result = theta.shape[1]
149-
print("Resultant Feature vector:")
125+
print("Resultant Feature vector : ")
150126
for i in range(len_result):
151127
print(f"{theta[0, i]:.5f}")
152128
except Exception as e:
153-
print(f"Unexpected error in main: {e}")
129+
print(f"Error in main execution: {e}")
154130

155131

156132
if __name__ == "__main__":

0 commit comments

Comments
 (0)