Simple Linear Regression from Scratch

EasyRegressionLinear AlgebraModel FittingVisualization

Implement a simple linear regression model without using any ML libraries.

Problem:

Implement a simple linear regression model without using any ML libraries.

Examples:

Input: X = np.array([1, 2, 3, 4, 5])
y = np.array([2, 4, 6, 8, 10])
Output: Coefficients: [2.0, 0.0]
R-squared: 1.0
Perfect linear relationship with slope 2
Input: X = np.random.randn(100)
y = 2 * X + np.random.randn(100) * 0.1
Output: Coefficients ≈ [2.0, 0.0]
R-squared > 0.95
Linear relationship with small random noise

Constraints:

  • Must handle division by zero in coefficient calculation
  • Must calculate R-squared correctly
  • Must handle numerical stability

Code Editorpython

Run your code to see the output here.

Output

Run your code to see the output here.