import pandas as pd import numpy as np import matplotlib.pyplot as plt # Read data from CSV file data = pd.read_csv('linear_regression_data.csv') x = data['x'].values y = data['y'].values # Perform linear regression using explicit formula # For linear regression y = mx + c, the formulas are: # m = (n*Σ(xy) - Σ(x)*Σ(y)) / (n*Σ(x²) - (Σ(x))²) # c = (Σ(y) - m*Σ(x)) / n n = len(x) sum_x = np.sum(x) sum_y = np.sum(y) sum_xy = np.sum(x * y) sum_x_squared = np.sum(x * x) # Calculate slope (m) and intercept (c) using explicit formulas m = (n * sum_xy - sum_x * sum_y) / (n * sum_x_squared - sum_x * sum_x) c = (sum_y - m * sum_x) / n # Regression line y_pred = m * x + c # Plot data and fitted line plt.figure(figsize=(10, 6)) plt.scatter(x, y, alpha=0.6, label='Data points') plt.plot(x, y_pred, color='red', linewidth=2, label=f'Fit: y = {m:.2f}x + {c:.2f}') plt.xlabel('x') plt.ylabel('y') plt.title('Linear Regression Example (Using Explicit Formula)') plt.legend() plt.grid(True, alpha=0.3) plt.show() print(f"Slope (m): {m:.6f}") print(f"Intercept (c): {c:.6f}")