Introduction
In this project, you will learn how to implement the Ordinary Least Squares (OLS) method in Python. OLS is a fundamental mathematical optimization technique used in machine learning, particularly in linear regression problems.
🎯 Tasks
In this project, you will learn:
- How to implement the OLS function to calculate the slope (w1) and intercept (w0) of a linear equation based on sample data.
- How to test the OLS function with sample data and verify the correctness of the results.
🏆 Achievements
After completing this project, you will be able to:
- Derive the OLS formulas for calculating the slope and intercept of a linear equation.
- Implement the OLS function in Python without using any external libraries.
- Test and validate the OLS function with sample data.
- Understand the importance of the OLS method in machine learning and linear regression problems.
Implement the Least Squares Function
In this step, you will implement the Ordinary Least Squares (OLS) function in Python. Follow the steps below to complete this step:
Open the
least_squares.pyfile in your text editor.Implement the
least_squares_functionfunction according to the provided specifications:- The function should take two input parameters:
x(a list of sample x values) andy(a list of sample y values). - The function should calculate the slope (
w1) and intercept (w0) of the linear equation using the OLS formulas provided in the challenge description. - The function should round the calculated
w0andw1values to two decimal places before returning them. - The function should return the
w0andw1values in that order.
- The function should take two input parameters:
Here's the completed least_squares_function implementation:
def least_squares_function(x, y):
"""
Parameters:
x -- list of values of sample x
y -- list of values of sample y
Returns:
w0 -- linear equation parameter, rounded to two decimal places
w1 -- linear equation parameter, rounded to two decimal places
"""
n = len(x)
## Calculate the sums needed for the slope (w1) and intercept (w0)
sum_x = sum(x)
sum_y = sum(y)
sum_xy = sum(x_i * y_i for x_i, y_i in zip(x, y))
sum_x_squared = sum(x_i**2 for x_i in x)
## Calculate the slope (w1) and intercept (w0) using the OLS formulas
w1 = (n * sum_xy - sum_x * sum_y) / (n * sum_x_squared - sum_x**2)
w0 = (sum_y - w1 * sum_x) / n
## Round to two decimal places
w0 = round(w0, 2)
w1 = round(w1, 2)
return w0, w1
- Save the
least_squares.pyfile.
Test the Least Squares Function
In this step, you will test the least_squares_function you implemented in the previous step.
- You can see the following code at the end of the file to test the
least_squares_function:
## Example usage
if __name__ == "__main__":
x_example = [1, 2, 3, 4]
y_example = [4, 5, 6, 7]
result = least_squares_function(x_example, y_example)
print("w0:", result[0])
print("w1:", result[1])
- Run the
least_squares.pyscript in your terminal:
python3 least_squares.py
You should see the following output:
w0: 3.0
w1: 1.0
This output confirms that the least_squares_function is working as expected.
Congratulations! You have successfully implemented the Ordinary Least Squares function in Python. In the next steps, you can explore how to use this function in a real-world machine learning problem.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



