mlektic.linear_reg package

Submodules

mlektic.linear_reg.linear_regression_archt module

class mlektic.linear_reg.linear_regression_archt.LinearRegressionArcht(iterations: int = 50, use_intercept: bool = True, verbose: bool = True, regularizer: callable = None, optimizer: tuple = None, method: str = 'least_squares')[source]

Bases: object

Linear Regression model class supporting different training methods including least squares, batch gradient descent, stochastic gradient descent, and mini-batch gradient descent.

iterations

Number of training iterations.

Type:

int

use_intercept

Whether to include an intercept in the model.

Type:

bool

verbose

Whether to print training progress.

Type:

bool

weights

Model weights.

Type:

tf.Variable

cost_history

History of cost values during training.

Type:

list

n_features

Number of features in the input data.

Type:

int

regularizer

Regularization function.

Type:

callable

optimizer

Optimizer for gradient descent.

Type:

tf.optimizers.Optimizer

method

Training method to use.

Type:

str

train(train_set)[source]

Trains the model on the provided training set.

get_parameters()[source]

Returns the model parameters (weights).

get_intercept()[source]

Returns the model intercept.

get_cost_history()[source]

Returns the history of cost values.

predict(input_new)[source]

Predicts output for new input data.

eval(test_set, metric)[source]

Evaluates the model on a test set using the specified metric.

save_model(filepath)[source]

Saves the model to a file.

load_model(filepath)[source]

Loads the model from a file.

eval(test_set: tuple, metric: str) float[source]

Evaluates the model on a test set using the specified metric.

Parameters:
  • test_set (tuple) – Tuple containing test input data (np.ndarray) and output data (np.ndarray).

  • metric (str) – Metric to use for evaluation. Options are ‘mse’, ‘rmse’, ‘mae’, ‘mape’, ‘r2’, ‘corr’.

Returns:

Evaluation result.

Return type:

float

Raises:

ValueError – If the specified metric is not supported.

get_cost_history() list[source]

Returns the history of cost values during training.

Returns:

List of cost values.

Return type:

list

get_intercept() float[source]

Returns the model intercept.

Returns:

Intercept value if use_intercept is True, else None.

Return type:

float

get_parameters() ndarray[source]

Returns the model parameters (weights).

Returns:

Array of model parameters.

Return type:

np.ndarray

load_model(filepath: str) None[source]

Loads the model from a file.

Parameters:

filepath (str) – Path to the file from which the model will be loaded.

predict(input_new: Union[np.ndarray, list, float]) ndarray[source]

Predicts output for new input data.

Parameters:

input_new (Union[np.ndarray, list, float]) – New input data for prediction.

Returns:

Predicted output.

Return type:

np.ndarray

Raises:

ValueError – If the input does not have the expected number of features.

save_model(filepath: str) None[source]

Saves the model to a file.

Parameters:

filepath (str) – Path to the file where the model will be saved.

train(train_set: tuple) LinearRegressionArcht[source]

Trains the model on the provided training set.

Parameters:

train_set (tuple) – Tuple containing training input data (np.ndarray) and output data (np.ndarray).

Returns:

The trained model instance.

Return type:

LinearRegressionArcht

mlektic.linear_reg.linreg_utils module

mlektic.linear_reg.linreg_utils.calculate_mae(y_true: Tensor, y_pred: Tensor) Tensor[source]

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

Parameters:
  • y_true (tf.Tensor) – True values. Shape should be (n_samples, 1).

  • y_pred (tf.Tensor) – Predicted values. Shape should be (n_samples, 1).

Returns:

Mean Absolute Error.

Return type:

tf.Tensor

mlektic.linear_reg.linreg_utils.calculate_mape(y_true: Tensor, y_pred: Tensor) Tensor[source]

Calculates the Mean Absolute Percentage Error (MAPE) between the true and predicted values.

Parameters:
  • y_true (tf.Tensor) – True values. Shape should be (n_samples, 1).

  • y_pred (tf.Tensor) – Predicted values. Shape should be (n_samples, 1).

Returns:

Mean Absolute Percentage Error.

Return type:

tf.Tensor

Raises:

ValueError – If any value in y_true is zero, which would result in division by zero.

mlektic.linear_reg.linreg_utils.calculate_mse(y_true: Tensor, y_pred: Tensor) Tensor[source]

Calculates the Mean Squared Error (MSE) between the true and predicted values.

Parameters:
  • y_true (tf.Tensor) – True values. Shape should be (n_samples, 1).

  • y_pred (tf.Tensor) – Predicted values. Shape should be (n_samples, 1).

Returns:

Mean Squared Error.

Return type:

tf.Tensor

mlektic.linear_reg.linreg_utils.calculate_pearson_correlation(y_true: Tensor, y_pred: Tensor) Tensor[source]

Calculates the Pearson correlation coefficient between the true and predicted values.

Parameters:
  • y_true (tf.Tensor) – True values. Shape should be (n_samples, 1).

  • y_pred (tf.Tensor) – Predicted values. Shape should be (n_samples, 1).

Returns:

Pearson correlation coefficient.

Return type:

tf.Tensor

mlektic.linear_reg.linreg_utils.calculate_r2(y_true: Tensor, y_pred: Tensor) Tensor[source]

Calculates the R-squared (R2) score, which indicates the proportion of the variance in the dependent variable that is predictable from the independent variable(s).

Parameters:
  • y_true (tf.Tensor) – True values. Shape should be (n_samples, 1).

  • y_pred (tf.Tensor) – Predicted values. Shape should be (n_samples, 1).

Returns:

R-squared score.

Return type:

tf.Tensor

mlektic.linear_reg.linreg_utils.calculate_rmse(y_true: Tensor, y_pred: Tensor) Tensor[source]

Calculates the Root Mean Squared Error (RMSE) between the true and predicted values.

Parameters:
  • y_true (tf.Tensor) – True values. Shape should be (n_samples, 1).

  • y_pred (tf.Tensor) – Predicted values. Shape should be (n_samples, 1).

Returns:

Root Mean Squared Error.

Return type:

tf.Tensor

Module contents