machine_learning.ridge_regression ================================= .. py:module:: machine_learning.ridge_regression Attributes ---------- .. autoapisummary:: machine_learning.ridge_regression.data Classes ------- .. autoapisummary:: machine_learning.ridge_regression.RidgeRegression Module Contents --------------- .. py:class:: RidgeRegression(alpha: float = 0.001, lambda_: float = 0.1, iterations: int = 1000) .. py:method:: compute_cost(features: numpy.ndarray, target: numpy.ndarray) -> float Compute the cost function with regularization. :param features: Input features, shape (m, n) :param target: Target values, shape (m,) :return: 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 .. py:method:: feature_scaling(features: numpy.ndarray) -> tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray] Normalize features to have mean 0 and standard deviation 1. :param features: Input features, shape (m, n) :return: 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 .. py:method:: fit(features: numpy.ndarray, target: numpy.ndarray) -> None Fit the Ridge Regression model to the training data. :param features: Input features, shape (m, n) :param 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 .. py:method:: mean_absolute_error(y_true: numpy.ndarray, y_pred: numpy.ndarray) -> float Compute Mean Absolute Error (MAE) between true and predicted values. :param y_true: Actual target values, shape (m,) :param y_pred: Predicted target values, shape (m,) :return: 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 .. py:method:: predict(features: numpy.ndarray) -> numpy.ndarray Predict values using the trained model. :param features: Input features, shape (m, n) :return: 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 .. py:attribute:: alpha :value: 0.001 .. py:attribute:: iterations :value: 1000 .. py:attribute:: lambda_ :value: 0.1 .. py:attribute:: theta :type: numpy.ndarray | None :value: None .. py:data:: data :value: None