Linear regression
import numpy as np
def linear_regression_from_scratch(x, y):
"""
Simple linear regression using least squares.
Args:
- x: numpy array or a list of independent variable.
- y: numpy array or a list of dependent variable.
Returns:
- coefficients: Tuple containing slope and intercept for the best fit line.
"""
# Convert lists to numpy arrays if necessary
x = np.array(x)
y = np.array(y)
# Calculating the mean of x and y
mean_x = np.mean(x)
mean_y = np.mean(y)
# Total number of values
n = len(x)
# Using the formula to calculate 'm' and 'c'
numerator = 0
denominator = 0
for i in range(n):
numerator += (x[i] - mean_x) * (y[i] - mean_y)
denominator += (x[i] - mean_x) ** 2
m = numerator / denominator # Slope of the line
c = mean_y - (m * mean_x) # Intercept of the line
return (m, c)
# Example usage
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 7, 11]
slope, intercept = linear_regression_from_scratch(x, y)
print(f"Slope: {slope}, Intercept: {intercept}")
Last update:
March 21, 2024