Class PoissonRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
Implements Poisson regression, a generalized linear model used for modeling count data and contingency tables.
public class PoissonRegression<T> : RegressionBase<T>, IRegression<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>
Type Parameters
TThe numeric data type used for calculations (e.g., float, double).
- Inheritance
-
PoissonRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
Poisson regression is appropriate when the dependent variable represents counts, such as the number of occurrences of an event in a fixed period of time or space. It assumes that the response variable follows a Poisson distribution and uses a log link function to relate the expected value of the response to the linear predictor.
The model is fitted using iteratively reweighted least squares (IRLS), a form of maximum likelihood estimation.
For Beginners: Poisson regression is used when you're trying to predict counts (like number of customer visits, number of accidents, etc.). Unlike linear regression, it ensures predictions are always non-negative and can handle cases where the variance increases with the mean, which is common in count data.
Constructors
PoissonRegression(PoissonRegressionOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the PoissonRegression class with the specified options and regularization.
public PoissonRegression(PoissonRegressionOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsPoissonRegressionOptions<T>Configuration options for the Poisson regression model. If null, default options will be used.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Regularization method to prevent overfitting. If null, no regularization will be applied.
Remarks
The constructor initializes the model with either the provided options or default settings.
For Beginners: This constructor sets up the Poisson regression model with your specified settings or uses default settings if none are provided. Regularization is an optional technique to prevent the model from becoming too complex and overfitting to the training data.
Methods
CreateNewInstance()
Creates a new instance of the Poisson Regression model with the same configuration.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateNewInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance of the Poisson Regression model.
Remarks
This method creates a deep copy of the current Poisson Regression model, including its options, coefficients, intercept, and regularization settings. The new instance is completely independent of the original, allowing modifications without affecting the original model.
For Beginners: This method creates an exact copy of your trained model.
Think of it like making a perfect duplicate:
- It copies all the configuration settings (like maximum iterations and tolerance)
- It preserves the coefficients (the weights for each feature)
- It maintains the intercept (the starting point of your model)
Creating a copy is useful when you want to:
- Create a backup before further modifying the model
- Create variations of the same model for different purposes
- Share the model with others while keeping your original intact
Exceptions
- InvalidOperationException
Thrown when the creation fails or required components are null.
Deserialize(byte[])
Deserializes the model from a byte array.
public override void Deserialize(byte[] data)
Parameters
databyte[]The byte array containing the serialized model data.
Remarks
This method deserializes both the base class data and the Poisson regression specific options, reconstructing the model's state from the serialized data.
For Beginners: Deserialization is the opposite of serialization - it takes the saved model data and reconstructs the model's internal state. This allows you to load a previously trained model and use it to make predictions without having to retrain it. It's like loading a saved game to continue where you left off.
GetModelType()
Gets the type of the model.
protected override ModelType GetModelType()
Returns
- ModelType
The model type identifier for Poisson regression.
Remarks
This method is used for model identification and serialization purposes.
For Beginners: This method simply returns an identifier that indicates this is a Poisson regression model. It's used internally by the library to keep track of different types of models.
Predict(Matrix<T>)
Makes predictions for the given input data.
public override Vector<T> Predict(Matrix<T> x)
Parameters
xMatrix<T>The input features matrix where each row is an example and each column is a feature.
Returns
- Vector<T>
A vector of predicted count values for each input example.
Remarks
This method adds an intercept column to the input matrix, combines the coefficients and intercept, and calls PredictMean to compute the predicted counts.
For Beginners: After training, this method is used to make predictions on new data. It takes your input features, applies the learned coefficients, and returns the predicted counts. The predictions are always non-negative, which is appropriate for count data.
Serialize()
Serializes the model to a byte array.
public override byte[] Serialize()
Returns
- byte[]
A byte array containing the serialized model data.
Remarks
This method serializes both the base class data and the Poisson regression specific options, including maximum iterations and convergence tolerance.
For Beginners: Serialization converts the model's internal state into a format that can be saved to disk or transmitted over a network. This allows you to save a trained model and load it later without having to retrain it. Think of it like saving your progress in a video game.
Train(Matrix<T>, Vector<T>)
Trains the Poisson regression model on the provided data.
public override void Train(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input features matrix where each row is a training example and each column is a feature.
yVector<T>The target count values vector corresponding to each training example.
Remarks
This method implements the iteratively reweighted least squares (IRLS) algorithm to fit the Poisson regression model. The steps are: 1. Initialize coefficients and intercept 2. For each iteration: a. Compute the predicted mean (mu) using the current coefficients b. Compute the weights matrix (W) based on mu c. Compute the working response (z) d. Solve the weighted least squares problem to get new coefficients e. Check for convergence
For Beginners: Training is the process where the model learns from your data. The algorithm starts with initial guesses for the coefficients and then iteratively improves them until they converge to the best values. At each step, it calculates predicted values, compares them to the actual values, and adjusts the coefficients to reduce the difference. This process continues until the changes become very small (convergence) or until a maximum number of iterations is reached.