Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions machine_learning/linear_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,25 +67,22 @@ def run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta):
return theta


def sum_of_square_error(data_x, data_y, len_data, theta):
def sum_of_square_error(data_x, data_y, theta):
"""Return sum of square error for error calculation
:param data_x : contains our dataset
:param data_y : contains the output (result vector)
:param len_data : len of the dataset
:param theta : contains the feature vector
:return : sum of square error computed from given feature's

Example:
>>> vc_x = np.array([[1.1], [2.1], [3.1]])
>>> vc_y = np.array([1.2, 2.2, 3.2])
>>> round(sum_of_square_error(vc_x, vc_y, 3, np.array([1])),3)
np.float64(0.005)
>>> round(sum_of_square_error(vc_x, vc_y, np.array([1])), 3)
np.float64(0.03)
"""
prod = np.dot(theta, data_x.transpose())
prod -= data_y.transpose()
sum_elem = np.sum(np.square(prod))
error = sum_elem / (2 * len_data)
return error
return np.sum(np.square(prod))


def run_linear_regression(data_x, data_y):
Expand All @@ -104,7 +101,7 @@ def run_linear_regression(data_x, data_y):

for i in range(iterations):
theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta)
error = sum_of_square_error(data_x, data_y, len_data, theta)
error = sum_of_square_error(data_x, data_y, theta)
print(f"At Iteration {i + 1} - Error is {error:.5f}")

return theta
Expand Down
Loading