Skip to content

Linear Models

ChangLiu edited this page May 30, 2025 · 3 revisions

neuraldecoding.model.linear_models

Classes of linear models


LinearModel(Model)

class LinearModel(Model)

Base class for all linear models.

Parameters:

  • params :

    Model parameters.

Attributes:

  • params :

    Stored model parameters

Methods

__init__(params)

Initialize the model with parameters.

Parameters:

  • params :

    Model-specific parameters.

__call__(data)

Make predictions using the model.

Parameters:

  • data : Any

    Input data for prediction.

train_step(input_data)

Perform a single training step.

Parameters:

  • input_data : Any

    Training data for one step.

forward(input)

Forward pass through the model.

Parameters:

  • input : Any

    Model input data.

save_model(filepath)

Save model parameters to file.

Parameters:

  • filepath : str

    Path where model parameters should be saved.

load_model(filepath)

Load model parameters from file.

Parameters:

  • filepath : str

    Path to saved model parameters.


KalmanFilter(LinearModel)

class KalmanFilter(LinearModel)

Kalman Filter decoder.

Parameters:

  • model_params : dict

    Dictionary containing 'append_ones_y', 'device', 'return_tensor', and optional 'yhat'.

Attributes:

  • A : ndarray

    State transition matrix

  • C : ndarray

    Observation matrix

  • W : ndarray

    Process noise covariance

  • Q : ndarray

    Observation noise covariance

  • yh : ndarray

    Current state estimate

Methods

__init__(model_params)

Initialize the Kalman Filter.

Parameters:

  • model_params : dict

    Model configuration parameters.

__call__(data)

Makes the instance callable and returns the result of forward pass.

Parameters:

  • data : ndarray

    Observation data for prediction, expected size [n, m]

Returns:

  • ndarray

    Prediction results, size [n, k]

train_step(input_data)

Trains the matrices in the model. If append_ones_y is true, a column of ones is added to calculate bias.

Parameters:

  • input_data : tuple

    A tuple of (x, y) - x (ndarray) size [n, m]: observation features - y (ndarray) size [n, k]: hidden state features

forward(input)

Runs a forward pass, by calling a predict method (torch or numpy).

Parameters:

  • input : ndarray

    size [n, m], where n is the number of samples/data, and m is the number of observation features

Returns:

  • yhat : ndarray

    prediction of size [n, k], where n is the number of samples/data, and k is the number of hidden state features

predict_numpy(x)

Runs a forward pass, returning a prediction for all input datapoints. If start_y is true, initial state is added.

Parameters:

  • x : ndarray

    size [n, m], where n is the number of samples/data, and m is the number of observation features

Returns:

  • yhat : ndarray

    prediction of size [n, k], where n is the number of samples/data, and k is the number of hidden state features

save_model(fpath)

Saves the model in its current state at the specified filepath

Parameters:

  • fpath : str

    indicates the file path to save the model in

load_model(fpath)

Load model parameters from a specified location

Parameters:

  • fpath : str

    indicates the file path to load the model from

initialize(yhat, Pt=None)

Reset initialization state estimate and covariance.

Parameters:

  • yhat : ndarray Initial state estimate.

  • Pt : ndarray, optional Initial error covariance matrix.


Regression(LinearModel)

class Regression(LinearModel)

Base class for regression models using scikit-learn implementations.

Parameters:

  • params : dict

    Model parameters.

Attributes:

  • model : sklearn model

    Underlying scikit-learn model instance

Methods

__init__(params={})

Initialize the regression model.

Parameters:

  • params : dict

    Model parameters.

__call__(data, is_torch=False)

Make predictions using the model.

Parameters:

  • data : Any

    Input data for prediction.

  • is_torch : bool

    Whether to return torch tensor.

Returns:

  • Any

    Model predictions.

train_step(input_data)

Train the model using scikit-learn fit method.

Parameters:

  • input_data : Any

    Training data (X, y).

forward(data, is_torch=False)

Forward pass through the model.

Parameters:

  • data : Any

    Input data.

  • is_torch : bool

    Whether to return torch tensor.

Returns:

  • Any

    Model predictions.

save_model(filepath)

Save model to file using pickle.

Parameters:

  • filepath : str

    Path to save the model.

load_model(filepath)

Load model from file using pickle.

Parameters:

  • filepath : str

    Path to load the model from.


LinearRegression(Regression)

class LinearRegression(Regression)

Linear regression model using scikit-learn LinearRegression.

Parameters:

  • params : dict

    Parameters passed to sklearn LinearRegression.

Methods

__init__(params={})

Initialize the linear regression model.

Parameters:

  • params : dict

    Parameters for sklearn LinearRegression.


RidgeRegression(Regression)

class RidgeRegression(Regression)

Ridge regression model using scikit-learn Ridge.

Parameters:

  • params : dict

    Parameters passed to sklearn Ridge.

Methods

__init__(params={})

Initialize the ridge regression model.

Parameters:

  • params : dict

    Parameters for sklearn Ridge.

Clone this wiki locally