pylwl.models package

pylwl.models.base_model module

class pylwl.models.base_model.BaseModel[source]

Bases: BaseEstimator

Base class for all models

evaluate(y_true, y_pred, list_metrics=None)[source]

Evaluate the model using specified metrics.

Parameters:
  • y_true (array-like) – True target values.

  • y_pred (array-like) – Model’s predicted values.

  • list_metrics (list of str, optional) – Names of metrics for evaluation (e.g., “MSE”, “MAE”).

Returns:

Evaluation metrics and their values.

Return type:

dict

fit(X, y)[source]

Fit the model to the training data.

static load_model(load_path='history', filename='model.pkl')[source]

Load a model from a pickle file.

Parameters:
  • load_path (str, optional) – Path to load the model from (default: “history”).

  • filename (str, optional) – Filename of the saved model (default: “model.pkl”).

Returns:

The loaded model.

Return type:

BaseMlp

predict(X)[source]

Predict using the trained model.

save_evaluation_metrics(y_true, y_pred, list_metrics=('RMSE', 'MAE'), save_path='history', filename='metrics.csv')[source]

Save evaluation metrics to a CSV file.

Parameters:
  • y_true (array-like) – Ground truth values.

  • y_pred (array-like) – Model predictions.

  • list_metrics (list of str, optional) – Metrics for evaluation (default: (“RMSE”, “MAE”)).

  • save_path (str, optional) – Path to save the file (default: “history”).

  • filename (str, optional) – Filename for saving metrics (default: “metrics.csv”).

save_model(save_path='history', filename='model.pkl')[source]

Save the trained model to a pickle file.

Parameters:
  • save_path (str, optional) – Path to save the model (default: “history”).

  • filename (str, optional) – Filename for saving model, with “.pkl” extension (default: “model.pkl”).

save_training_loss(save_path='history', filename='loss.csv')[source]

Save training loss history to a CSV file.

Parameters:
  • save_path (str, optional) – Path to save the file (default: “history”).

  • filename (str, optional) – Filename for saving loss history (default: “loss.csv”).

save_y_predicted(X, y_true, save_path='history', filename='y_predicted.csv')[source]

Save true and predicted values to a CSV file.

Parameters:
  • X (array-like or torch.Tensor) – Input features.

  • y_true (array-like) – True values.

  • save_path (str, optional) – Path to save the file (default: “history”).

  • filename (str, optional) – Filename for saving predicted values (default: “y_predicted.csv”).

score(X, y)[source]

Evaluate the model’s performance on the given data.

pylwl.models.classic_lw module

class pylwl.models.classic_lw.BaseLW(kernel='gaussian', tau=1.0)[source]

Bases: BaseModel

Base class for locally weighted models.

This class provides the foundation for locally weighted regression and classification models. It includes methods for computing kernel weights based on a specified kernel function.

Parameters:
  • kernel (str or callable, optional) – The kernel function to use. If a string is provided, it should match the name of a kernel function in the kernel_module. If a callable is provided, it should accept distances and tau as arguments and return weights.

  • tau (float, optional) – The bandwidth parameter for the kernel function (default: 1.0).

kernel

The kernel function used for computing weights.

Type:

str or callable

kernel_func_

The resolved kernel function (either from kernel_module or the provided callable).

Type:

callable

tau

The bandwidth parameter for the kernel function.

Type:

float

class pylwl.models.classic_lw.LwClassifier(kernel='gaussian', tau=1.0)[source]

Bases: BaseLW, ClassifierMixin

Locally Weighted Classifier.

This class implements a locally weighted classification model using a specified kernel function and bandwidth parameter. It predicts class probabilities and labels by fitting a weighted linear model for each query point.

Parameters:
  • kernel (str or callable, optional) – The kernel function to use. If a string is provided, it should match the name of a kernel function in the kernel_module. If a callable is provided, it should accept distances and tau as arguments and return weights.

  • tau (float, optional) – The bandwidth parameter for the kernel function (default: 1.0).

X_

The training data.

Type:

ndarray, shape (n_samples, n_features)

y_raw_

The raw target values for the training data.

Type:

ndarray, shape (n_samples,)

classes_

The unique class labels.

Type:

ndarray, shape (n_classes,)

n_classes_

The number of unique classes.

Type:

int

lb_

The label binarizer used for encoding class labels.

Type:

LabelBinarizer

y_bin_

The binarized target values for the training data.

Type:

ndarray, shape (n_samples, n_classes) or (n_samples,)

get_prob

The method used to compute class probabilities (binary or multiclass).

Type:

callable

evaluate(y_true, y_pred, list_metrics=('AS', 'RS'))[source]

Evaluate the classification model using specified metrics.

Parameters:
  • y_true (array-like) – True target class labels.

  • y_pred (array-like) – Predicted class labels.

  • list_metrics (tuple of str, optional) – List of metrics for evaluation (default: (“AS”, “RS”)).

Returns:

Dictionary of calculated metric values.

Return type:

dict

fit(X, y)[source]

Fit the locally weighted classification model.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – The training data.

  • y (array-like, shape (n_samples,)) – The target class labels.

Returns:

self – The fitted model.

Return type:

LwClassifier

predict(X)[source]

Predict class labels for the given input data.

Parameters:

X (array-like, shape (n_samples, n_features)) – The input data.

Returns:

y_pred – The predicted class labels.

Return type:

ndarray, shape (n_samples,)

predict_proba(X)[source]

Predict class probabilities for the given input data.

Parameters:

X (array-like, shape (n_samples, n_features)) – The input data.

Returns:

probas – The predicted class probabilities.

Return type:

ndarray, shape (n_samples, n_classes)

score(X, y)[source]

Compute the accuracy score for the model.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – The input data.

  • y (array-like, shape (n_samples,)) – The true target class labels.

Returns:

score – The accuracy score of the predictions.

Return type:

float

scores(X, y, list_metrics=('AS', 'RS'))[source]

Compute evaluation metrics for the model on the given data.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – The input data.

  • y (array-like, shape (n_samples,)) – The true target class labels.

  • list_metrics (tuple of str, optional) – List of metrics for evaluation (default: (“AS”, “RS”)).

Returns:

Dictionary of calculated metric values.

Return type:

dict

class pylwl.models.classic_lw.LwRegressor(kernel='gaussian', tau=1.0)[source]

Bases: BaseLW, RegressorMixin

Locally Weighted Regressor.

This class implements a locally weighted regression model using a specified kernel function and bandwidth parameter. It predicts target values by fitting a weighted linear model for each query point.

Parameters:
  • kernel (str or callable, optional) – The kernel function to use. If a string is provided, it should match the name of a kernel function in the kernel_module. If a callable is provided, it should accept distances and tau as arguments and return weights.

  • tau (float, optional) – The bandwidth parameter for the kernel function (default: 1.0).

X_

The training data.

Type:

ndarray, shape (n_samples, n_features)

y_

The target values for the training data.

Type:

ndarray, shape (n_samples,)

evaluate(y_true, y_pred, list_metrics=('MSE', 'MAE'))[source]

Evaluate the regression model using specified metrics.

Parameters:
  • y_true (array-like) – True target values.

  • y_pred (array-like) – Predicted target values.

  • list_metrics (tuple of str, optional) – List of metrics for evaluation (default: (“MSE”, “MAE”)).

Returns:

Dictionary of calculated metric values.

Return type:

dict

fit(X, y)[source]

Fit the locally weighted regression model.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – The training data.

  • y (array-like, shape (n_samples,)) – The target values.

Returns:

self – The fitted model.

Return type:

LwRegressor

predict(X)[source]

Predict target values for the given input data.

Parameters:

X (array-like, shape (n_samples, n_features)) – The input data.

Returns:

y_pred – The predicted target values.

Return type:

ndarray, shape (n_samples,)

score(X, y)[source]

Compute the R^2 score for the model.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – The input data.

  • y (array-like, shape (n_samples,)) – The true target values.

Returns:

score – The R^2 score of the predictions.

Return type:

float

scores(X, y, list_metrics=('MSE', 'MAE'))[source]

Compute evaluation metrics for the model on the given data.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – The input data.

  • y (array-like, shape (n_samples,)) – The true target values.

  • list_metrics (tuple of str, optional) – List of metrics for evaluation (default: (“MSE”, “MAE”)).

Returns:

Dictionary of calculated metric values.

Return type:

dict

pylwl.models.gd_lw module

class pylwl.models.gd_lw.GdLwClassifier(kernel='gaussian', tau=1.0, epochs=20, optim='Adam', optim_paras=None, seed=42, verbose=True, device=None)[source]

Bases: BaseModel, ClassifierMixin

Gradient-Descent-based Locally Weighted Classifier.

This class implements a locally weighted classification model using gradient descent optimization. It supports binary and multiclass classification with a specified kernel function and bandwidth parameter.

Parameters:
  • kernel (str or callable, optional) – The kernel function to use. If a string is provided, it should match the name of a kernel function in the kernel_module. If a callable is provided, it should accept distances and tau as arguments and return weights.

  • tau (float, optional) – The bandwidth parameter for the kernel function (default: 1.0).

  • epochs (int, optional) – The number of training epochs for the local model (default: 20).

  • optim (str, optional) – The optimizer to use for training (default: “Adam”).

  • optim_paras (dict, optional) – Additional parameters for the optimizer (default: None).

  • seed (int, optional) – Random seed for reproducibility (default: 42).

  • verbose (bool, optional) – Whether to print training progress (default: True).

  • device (str, optional) – The device to use for training (“cpu” or “gpu”). If “gpu” is specified but not available, an error is raised (default: None).

kernel

The kernel function used for computing weights.

Type:

str or callable

tau

The bandwidth parameter for the kernel function.

Type:

float

epochs

The number of training epochs for the local model.

Type:

int

optim

The optimizer used for training.

Type:

str

optim_paras

Additional parameters for the optimizer.

Type:

dict

seed

Random seed for reproducibility.

Type:

int

verbose

Whether to print training progress.

Type:

bool

device

The device used for training (“cpu” or “cuda”).

Type:

str

X_

The training data.

Type:

ndarray, shape (n_samples, n_features)

y_

The target class labels for the training data.

Type:

ndarray, shape (n_samples,)

classes_

The unique class labels.

Type:

ndarray, shape (n_classes,)

is_binary_

Whether the classification task is binary.

Type:

bool

kernel_func_

The resolved kernel function (either from kernel_module or the provided callable).

Type:

callable

_trainer

The method used to train the local model (binary or multiclass).

Type:

callable

evaluate(y_true, y_pred, list_metrics=('AS', 'RS'))[source]

Evaluate the model using the specified metrics.

fit(X, y)[source]

Fit the model to the training data.

predict(X)[source]

Predict the class labels for the input samples.

predict_proba(X)[source]

Predict the class probabilities for the input samples.

score(X, y)[source]

Returns the accuracy of the model on the given data and labels.

scores(X, y, list_metrics=('AS', 'RS'))[source]

Compute the evaluation metrics for the model on the given data and labels.

class pylwl.models.gd_lw.GdLwRegressor(kernel='gaussian', tau=1.0, epochs=20, optim='Adam', optim_paras=None, seed=42, verbose=True, device=None)[source]

Bases: BaseModel, RegressorMixin

Gradient-Descent-based Locally Weighted Regressor.

This class implements a locally weighted regression model using gradient descent optimization. It predicts target values by training a local model for each query point with a specified kernel function and bandwidth parameter.

Parameters:
  • kernel (str or callable, optional) – The kernel function to use. If a string is provided, it should match the name of a kernel function in the kernel_module. If a callable is provided, it should accept distances and tau as arguments and return weights.

  • tau (float, optional) – The bandwidth parameter for the kernel function (default: 1.0).

  • epochs (int, optional) – The number of training epochs for the local model (default: 20).

  • optim (str, optional) – The optimizer to use for training (default: “Adam”).

  • optim_paras (dict, optional) – Additional parameters for the optimizer (default: None).

  • seed (int, optional) – Random seed for reproducibility (default: 42).

  • verbose (bool, optional) – Whether to print training progress (default: True).

  • device (str, optional) – The device to use for training (“cpu” or “gpu”). If “gpu” is specified but not available, an error is raised (default: None).

kernel

The kernel function used for computing weights.

Type:

str or callable

tau

The bandwidth parameter for the kernel function.

Type:

float

epochs

The number of training epochs for the local model.

Type:

int

optim

The optimizer used for training.

Type:

str

optim_paras

Additional parameters for the optimizer.

Type:

dict

seed

Random seed for reproducibility.

Type:

int

verbose

Whether to print training progress.

Type:

bool

device

The device used for training (“cpu” or “cuda”).

Type:

str

X_

The training data.

Type:

ndarray, shape (n_samples, n_features)

y_

The target values for the training data.

Type:

ndarray, shape (n_samples,)

kernel_func_

The resolved kernel function (either from kernel_module or the provided callable).

Type:

callable

evaluate(y_true, y_pred, list_metrics=('MSE', 'MAE'))[source]

Evaluate the regression model using specified metrics.

Parameters:
  • y_true (array-like) – True target values.

  • y_pred (array-like) – Predicted target values.

  • list_metrics (tuple of str, optional) – List of metrics for evaluation (default: (“MSE”, “MAE”)).

Returns:

Dictionary of calculated metric values.

Return type:

dict

fit(X, y)[source]

Fit the locally weighted regression model.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – The training data.

  • y (array-like, shape (n_samples,)) – The target values.

Returns:

self – The fitted model.

Return type:

GdLwRegressor

predict(X)[source]

Predict target values for the given input data.

Parameters:

X (array-like, shape (n_samples, n_features)) – The input data.

Returns:

preds – The predicted target values.

Return type:

ndarray, shape (n_samples,)

score(X, y)[source]

Compute the R2 score for the model.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – The input data.

  • y (array-like, shape (n_samples,)) – The true target values.

Returns:

score – The R2 score of the predictions.

Return type:

float

scores(X, y, list_metrics=('MSE', 'MAE'))[source]

Compute evaluation metrics for the model on the given data.

Parameters:
  • X (array-like, shape (n_samples, n_features)) – The input data.

  • y (array-like, shape (n_samples,)) – The true target values.

  • list_metrics (tuple of str, optional) – List of metrics for evaluation (default: (“MSE”, “MAE”)).

Returns:

Dictionary of calculated metric values.

Return type:

dict