Math 240 Linear Regression Code

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/8

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 9:31 PM on 4/7/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

9 Terms

1
New cards

Write the code for defining a class LinearRegression and instance for m and b.

import numpy as np

from typing import callable, union, optional

class LinearRegression:
	def __init__(self):
		self.m: Optional[float] = None
		self.b: Optional[float] = None

2
New cards

For the LinearRegression class, define the fit function

def fit(self, X, y):
      N = len(X)

      sum_x = np.sum(X)
      sum_y = np.sum(y)
      sum_xy = np.sum(X*y)
      sum_x2 = np.sum(X**2)

      numerator_m = sum_xy - (1/N) * (sum_x * sum_y)
      denominator_m = sum_x2 - (1/N) * (sum_x**2)

      self.m = numerator_m / denominator_m
      self.b = (1/N) * (sum_y - self.m * sum_x)

      return self.m, self.b

3
New cards

For the LinearRegression class, define the predict function

def predict(self, X) -> np.array:
      if self.m is None or self.b is None:
        raise ValueError("Model must be fitted before calling predict")

      y_pred = self.m * X + self.b
      return y_pred

4
New cards

For the LinearRegression class, define the mse function

def mse(self, X, y):
	N = len(y)
	y_pred = self.predict(X)
	return np.sum((y - y_pred)**2) / N

5
New cards

For the LinearRegression class, define the R² function

def R2(self, X, y):
      y_pred = self.predict(X)

      ss_res = np.sum((y - y_pred)**2)
      ss_tot = np.sum((y - np.mean(y))**2)

      if ss_tot == 0:
        return 1.0 if ss_res == 0 else 0.0
      return 1 - (ss_res/ss_tot)

6
New cards

For the LinearRegression class, define the string.

def __str__(self):
      if self.m is None or self.b is None: 
        return "Linear Regression (Not fitted)"
      return f"y = {self.m:.4f}X + {self.b:.4f}"

7
New cards

For the LinearRegression class, write the representation of the object

def __repr__(self):
	return f"Linear Regression(m={self.m}, b={self.b})"

8
New cards

Outside of the LinearRegression class, define a function run_test that takes test_name as a string argument, X and y as np.ndarrays, passes the LinearRegression class through a variable called model, and prints the Equation, MSE, and R² of the model.

def run_test(test_name: str, X: np.ndarray, y: np.ndarray):
	print(f"---{test_name}---")
	model = LinearRegression()
	model.fit(X,y)

	print(f"Equation: {model}")
	print(f"MSE: {model.mse(X, y):.4f}")
	print(f"R-squared: {model.R2(X, y):.4f}\n")

9
New cards

Test the LinearRegression class by writing under if __name__ == "__main__": , test X1 = np.linspace(1, 10, 10)

y = 2*X1 + 3

X2 = np.linspace(1, 10, 50)

y2 = 2.5 * X2 + 3 + np.random.normal(0, 2, 50)

if __name__ == "__main__":
	X1 = np.linspace(1, 10, 10)
	y1 = 2*X1 +3
	run_test("Test 1: Linear line", X1, y1)

	np.random.seed(42)
	X2 = np.linspace(1, 10, 50)
	y2 = 2.5 * X2 + 3 + np.random.normal(0, 2, 50)
	run_test("Test 2: Line with noise", X2, y2)