This Python program first reads a value x, then reads n data points as decimal pairs (xᵢ, yᵢ).
It calculates the slope m and intercept b using linear regression formulas, and displays the final equation y = mx + b.
x̄ = (∑xᵢ) / n → Mean of x valuesȳ = (∑yᵢ) / n → Mean of y values∑xᵢyᵢ = x₁y₁ + x₂y₂ + ... + xₙyₙ∑xᵢ² = x₁² + x₂² + ... + xₙ²m = (∑xᵢyᵢ - ȳ ∑xᵢ) / (∑xᵢ² - x̄ ∑xᵢ)b = ȳ - m x̄
n = int(input("Enter number of points: "))
x_vals = []
y_vals = []
for i in range(n):
x_i = float(input(f"x[{i+1}] = "))
y_i = float(input(f"y[{i+1}] = "))
x_vals.append(x_i)
y_vals.append(y_i)
sum_x = sum(x_vals)
sum_y = sum(y_vals)
sum_xy = sum(x * y for x, y in zip(x_vals, y_vals))
sum_x2 = sum(x ** 2 for x in x_vals)
x_mean = sum_x / n
y_mean = sum_y / n
m = (sum_xy - y_mean * sum_x) / (sum_x2 - x_mean * sum_x)
b = y_mean - m * x_mean
print(f"Linear equation: y = {round(m, 4)}x + {round(b, 4)}")
Enter number of points: 3
x[1] = 1
y[1] = 2
x[2] = 2
y[2] = 3
x[3] = 3
y[3] = 5
Linear equation: y = 1.5x + 0.0
- The program collects x and y values into lists
- It computes the required sums and means
- Then it applies the regression formulas to calculate m and b
- Finally, it prints the linear equation rounded to 4 decimal places