Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
576 B
5
Indexable
Never
import numpy as np

def regression_line(X, Y):
    # Calculate mean of X and Y
    mean_X = np.mean(X)
    mean_Y = np.mean(Y)
    
    # Calculate slope (m) and y-intercept (b) of the regression line
    numerator = sum((X - mean_X) * (Y - mean_Y))
    denominator = sum((X - mean_X) ** 2)
    m = numerator / denominator
    b = mean_Y - m * mean_X
    
    return m, b

# Example points
X = np.array([1, 2, 3, 4, 5])
Y = np.array([2, 3, 4, 5, 6])

# Calculate regression line
slope, intercept = regression_line(X, Y)
print("Regression line: Y =", slope, "* X +", intercept)
Leave a Comment