Skip to content

Commit 5bf9b85

Browse files
committed
Resolved mypy checks
1 parent 8f1f091 commit 5bf9b85

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

machine_learning/ridge_regression.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
import pandas as pd
3+
from typing import Optional, Tuple
34

45

56
class RidgeRegression:
@@ -15,7 +16,7 @@ def __init__(
1516
self.alpha = alpha
1617
self.lambda_ = lambda_
1718
self.iterations = iterations
18-
self.theta = None
19+
self.theta: Optional[np.ndarray] = None # Initialize as None, later will be ndarray
1920

2021
def feature_scaling(
2122
self, features: np.ndarray
@@ -92,6 +93,9 @@ def predict(self, features: np.ndarray) -> np.ndarray:
9293
>>> predictions.shape == target.shape
9394
True
9495
"""
96+
if self.theta is None:
97+
raise ValueError("Model is not trained yet. Call the `fit` method first.")
98+
9599
features_scaled, _, _ = self.feature_scaling(
96100
features
97101
) # Scale features using training data
@@ -114,6 +118,9 @@ def compute_cost(self, features: np.ndarray, target: np.ndarray) -> float:
114118
>>> isinstance(cost, float)
115119
True
116120
"""
121+
if self.theta is None:
122+
raise ValueError("Model is not trained yet. Call the `fit` method first.")
123+
117124
features_scaled, _, _ = self.feature_scaling(
118125
features
119126
) # Scale features using training data

0 commit comments

Comments
 (0)