1616import numpy as np
1717
1818
19- def ols_regression (x : np .ndarray , y : np .ndarray ) -> tuple :
19+ def ols_regression (x_point : np .ndarray , y_point : np .ndarray ) -> tuple :
2020 """
2121 Performs Ordinary Least Squares Regression (OLSR) on the given data.
2222
@@ -40,16 +40,16 @@ def ols_regression(x: np.ndarray, y: np.ndarray) -> tuple:
4040
4141 # Calculate the mean of the independent variable and
4242 # the dependent variable.
43- x_mean = np .mean (x )
44- y_mean = np .mean (y )
43+ x_mean = np .mean (x_point )
44+ y_mean = np .mean (y_point )
4545
4646 # Calculate the slope of the regression line.
47- b = np .sum ((x - x_mean ) * (y - y_mean )) / np .sum ((x - x_mean ) ** 2 )
47+ slope = np .sum ((x_point - x_mean ) * (y_point - y_mean )) / np .sum ((x_point - x_mean ) ** 2 )
4848
4949 # Calculate the intercept of the regression line.
50- a = y_mean - b * x_mean
50+ intercept = y_mean - slope * x_mean
5151
52- return a , b
52+ return intercept , slope
5353
5454
5555if __name__ == "__main__" :
@@ -58,21 +58,21 @@ def ols_regression(x: np.ndarray, y: np.ndarray) -> tuple:
5858 doctest .testmod ()
5959
6060 # Load the data
61- x = np .array ([1 , 2 , 3 , 4 , 5 ])
62- y = np .array ([2 , 4 , 6 , 8 , 10 ])
61+ x_points = np .array ([1 , 2 , 3 , 4 , 5 ])
62+ y_points = np .array ([2 , 4 , 6 , 8 , 10 ])
6363
6464 # Perform OLS regression
65- a , b = ols_regression (x , y )
65+ intercept , slope = ols_regression (x_points , y_points )
6666
6767 # Intercept (a) and slope (b) of the regression line
68- print ("Intercept:" , a )
69- print ("Slope:" , b )
68+ print ("Intercept:" , intercept )
69+ print ("Slope:" , slope )
7070
7171 # Predict the target variable for a new data point with
7272 # an independent variable value of 6
7373 x_new = 6
7474
7575 # Make a prediction
76- y_pred = a + b * x_new
76+ y_pred = intercept + slope * x_new
7777
7878 print ("Prediction:" , y_pred )
0 commit comments