Skip to content

Commit 16430ba

Browse files
author
Jeel Gajera
committed
Add: Ordinary Least Squares Regression Algorithm
1 parent 0601b56 commit 16430ba

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@
553553
* [Loss Functions](machine_learning/loss_functions.py)
554554
* [Mfcc](machine_learning/mfcc.py)
555555
* [Multilayer Perceptron Classifier](machine_learning/multilayer_perceptron_classifier.py)
556+
* [Ordinary Least Squares Regression](machine_learning/ordinary_least_squares_regression.py)
556557
* [Polynomial Regression](machine_learning/polynomial_regression.py)
557558
* [Scoring Functions](machine_learning/scoring_functions.py)
558559
* [Self Organizing Map](machine_learning/self_organizing_map.py)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Ordinary Least Squares Regression (OLSR):
3+
4+
Ordinary Least Squares Regression (OLSR) is a statistical method for
5+
estimating the parameters of a linear regression model.
6+
It is the most commonly used regression method,
7+
and it is based on the principle of minimizing
8+
the sum of the squared residuals.
9+
10+
Below is simple implementation of OLSR
11+
without using any external libraries.
12+
13+
WIKI: https://en.wikipedia.org/wiki/Ordinary_least_squares
14+
"""
15+
16+
import numpy as np
17+
18+
def ols_regression(x, y):
19+
"""
20+
Performs Ordinary Least Squares Regression (OLSR) on the given data.
21+
22+
Args:
23+
x (numpy.ndarray): The independent variable.
24+
y (numpy.ndarray): The dependent variable.
25+
26+
Returns:
27+
a (float): The intercept of the regression line.
28+
b (float): The slope of the regression line.
29+
30+
Examples:
31+
>>> x = np.array([1, 2, 3, 4, 5])
32+
>>> y = np.array([2, 4, 6, 8, 10])
33+
>>> a, b = ols_regression(x, y)
34+
>>> a # Intercept should be 0.0
35+
0.0
36+
>>> round(b, 2) # Slope should be 2.0
37+
2.0
38+
"""
39+
40+
# Calculate the mean of the independent variable and
41+
# the dependent variable.
42+
x_mean = np.mean(x)
43+
y_mean = np.mean(y)
44+
45+
# Calculate the slope of the regression line.
46+
b = np.sum((x - x_mean) * (y - y_mean)) / np.sum((x - x_mean)**2)
47+
48+
# Calculate the intercept of the regression line.
49+
a = y_mean - b * x_mean
50+
51+
return a, b
52+
53+
if __name__ == "__main__":
54+
import doctest
55+
56+
doctest.testmod()
57+
58+
# Load the data
59+
x = np.array([1, 2, 3, 4, 5])
60+
y = np.array([2, 4, 6, 8, 10])
61+
62+
# Perform OLS regression
63+
a, b = ols_regression(x, y)
64+
65+
# Intercept (a) and slope (b) of the regression line
66+
print('Intercept:', a)
67+
print('Slope:', b)
68+
69+
# Predict the target variable for a new data point with
70+
# an independent variable value of 6
71+
x_new = 6
72+
73+
# Make a prediction
74+
y_pred = a + b * x_new
75+
76+
print('Prediction:', y_pred)

0 commit comments

Comments
 (0)