Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
576 B
2
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((Y - mean_Y) * (X - mean_X))
    denominator = sum((Y - mean_Y) ** 2)
    m = numerator / denominator
    b = mean_X - m * mean_Y
    
    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: X =", slope, "* Y +", intercept)
Leave a Comment