machine_learning.ridge_regression

Attributes

data

Classes

RidgeRegression

Module Contents

class machine_learning.ridge_regression.RidgeRegression(alpha: float = 0.001, lambda_: float = 0.1, iterations: int = 1000)
compute_cost(features: numpy.ndarray, target: numpy.ndarray) float

Compute the cost function with regularization.

Parameters:
  • features – Input features, shape (m, n)

  • target – Target values, shape (m,)

Returns:

Computed cost

Example: >>> rr = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=10) >>> features = np.array([[1, 2], [2, 3], [4, 6]]) >>> target = np.array([1, 2, 3]) >>> rr.fit(features, target) >>> cost = rr.compute_cost(features, target) >>> isinstance(cost, float) True

feature_scaling(features: numpy.ndarray) tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]

Normalize features to have mean 0 and standard deviation 1.

Parameters:

features – Input features, shape (m, n)

Returns:

Tuple containing: - Scaled features - Mean of each feature - Standard deviation of each feature

Example: >>> rr = RidgeRegression() >>> features = np.array([[1, 2], [2, 3], [4, 6]]) >>> scaled_features, mean, std = rr.feature_scaling(features) >>> np.allclose(scaled_features.mean(axis=0), 0) True >>> np.allclose(scaled_features.std(axis=0), 1) True

fit(features: numpy.ndarray, target: numpy.ndarray) None

Fit the Ridge Regression model to the training data.

Parameters:
  • features – Input features, shape (m, n)

  • target – Target values, shape (m,)

Example: >>> rr = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=10) >>> features = np.array([[1, 2], [2, 3], [4, 6]]) >>> target = np.array([1, 2, 3]) >>> rr.fit(features, target) >>> rr.theta is not None True

mean_absolute_error(y_true: numpy.ndarray, y_pred: numpy.ndarray) float

Compute Mean Absolute Error (MAE) between true and predicted values.

Parameters:
  • y_true – Actual target values, shape (m,)

  • y_pred – Predicted target values, shape (m,)

Returns:

MAE

Example: >>> rr = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=10) >>> y_true = np.array([1, 2, 3]) >>> y_pred = np.array([1.1, 2.1, 2.9]) >>> mae = rr.mean_absolute_error(y_true, y_pred) >>> isinstance(mae, float) True

predict(features: numpy.ndarray) numpy.ndarray

Predict values using the trained model.

Parameters:

features – Input features, shape (m, n)

Returns:

Predicted values, shape (m,)

Example: >>> rr = RidgeRegression(alpha=0.01, lambda_=0.1, iterations=10) >>> features = np.array([[1, 2], [2, 3], [4, 6]]) >>> target = np.array([1, 2, 3]) >>> rr.fit(features, target) >>> predictions = rr.predict(features) >>> predictions.shape == target.shape True

alpha = 0.001
iterations = 1000
lambda_ = 0.1
theta: numpy.ndarray | None = None
machine_learning.ridge_regression.data = None