Class MultinomialLogisticRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
Represents a multinomial logistic regression model for multi-class classification problems.
public class MultinomialLogisticRegression<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 type used for calculations, typically float or double.
- Inheritance
-
MultinomialLogisticRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
Multinomial logistic regression extends binary logistic regression to handle multiple classes. It models the probabilities of different possible outcomes using the softmax function. For each class, the model learns a set of coefficients that determine how each feature affects the probability of that class. During prediction, it assigns the input to the class with the highest probability.
For Beginners: Multinomial logistic regression is a method for classifying data into multiple categories.
Think of it like a voting system where:
- Each feature (input variable) gets to "vote" for different categories
- The importance of each feature's vote is learned from training data
- For any new data point, we count the weighted votes for each category
- The category with the most votes wins and becomes the prediction
For example, when classifying emails into categories like "work," "personal," or "spam," certain words might strongly suggest one category over others. The model learns which features (words) are most helpful for distinguishing between the different categories.
Constructors
MultinomialLogisticRegression(MultinomialLogisticRegressionOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Initializes a new instance of the MultinomialLogisticRegression<T> class with optional custom options and regularization.
public MultinomialLogisticRegression(MultinomialLogisticRegressionOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsMultinomialLogisticRegressionOptions<T>Custom options for the multinomial logistic regression algorithm. If null, default options are used.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Regularization method to prevent overfitting. If null, no regularization is applied.
Remarks
This constructor creates a new multinomial logistic regression model with the specified options and regularization. If no options are provided, default values are used. Regularization helps prevent overfitting by penalizing large coefficient values.
For Beginners: This creates a new multinomial logistic regression model with your chosen settings.
When creating the model:
- You can provide custom settings (options) or use the defaults
- You can add regularization, which helps prevent the model from memorizing the training data
Regularization is like adding a penalty for complexity, encouraging the model to keep things simple unless there's strong evidence for complexity. This typically helps the model perform better on new, unseen data.
Methods
CreateNewInstance()
Creates a new instance of the Multinomial Logistic 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 Multinomial Logistic Regression model.
Remarks
This method creates a deep copy of the current Multinomial Logistic Regression model, including its options, coefficients matrix, number of classes, 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 coefficient weights for all classes (the voting system for each category)
- It maintains information about how many categories the model can predict
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 multinomial logistic regression model from a byte array.
public override void Deserialize(byte[] data)
Parameters
databyte[]A byte array containing the serialized model data.
Remarks
This method restores a multinomial logistic regression model from a serialized byte array, reconstructing its parameters and configuration. This allows a previously trained model to be loaded from storage or after being received over a network.
For Beginners: This method rebuilds the model from a saved format.
Deserialization:
- Takes a sequence of bytes that represents a model
- Reconstructs the original model with all its learned patterns
- Allows you to use a previously trained model without retraining
Think of it like unpacking a model that was packed up for storage or shipping, so you can use it again exactly as it was before.
GetModelType()
Gets the type of regression model.
protected override ModelType GetModelType()
Returns
- ModelType
The model type, in this case, MultinomialLogisticRegression.
Remarks
This method returns an enumeration value indicating that this is a multinomial logistic regression model. This is used for type identification when working with different regression models in a unified manner.
For Beginners: This method simply tells what kind of model this is.
It returns a label (MultinomialLogisticRegression) that:
- Identifies this specific type of model
- Helps other code handle the model appropriately
- Is used for model identification and categorization
It's like a name tag that lets other parts of the program know what kind of model they're working with.
Predict(Matrix<T>)
Predicts the class labels for new data points using the trained multinomial logistic regression model.
public override Vector<T> Predict(Matrix<T> x)
Parameters
xMatrix<T>The feature matrix where each row is a sample to predict.
Returns
- Vector<T>
A vector containing the predicted class labels (as integers).
Remarks
This method predicts the class labels for new data points by computing the probabilities for each class and selecting the class with the highest probability for each sample. The class labels are returned as integers.
For Beginners: This is where the model makes predictions on new data.
The prediction process:
- Calculate the probability of each class for each data point
- For each data point, select the class with the highest probability
- Return these predicted classes as the results
It's like a voting system where each feature casts a weighted vote for each class, and the class with the most votes wins.
PredictProbabilities(Matrix<T>)
Predicts the probabilities of each class for new data points.
public Matrix<T> PredictProbabilities(Matrix<T> x)
Parameters
xMatrix<T>The feature matrix where each row is a sample to predict.
Returns
- Matrix<T>
A matrix where each row corresponds to a sample and each column to the probability of a class.
Remarks
This method computes the probabilities of each class for new data points using the trained model. The resulting matrix contains the probability of each class for each sample. These probabilities sum to 1 across the classes for each sample.
For Beginners: This method provides the likelihood of each class for each data point.
Rather than just giving the final prediction, it provides:
- The probability of each possible class
- A measure of the model's confidence in each prediction
- Values between 0 (impossible) and 1 (certain), with all classes summing to 1
This is useful when you need to know not just the predicted class but also how confident the model is in that prediction. For example, you might treat a prediction with 95% confidence differently than one with 51% confidence.
Serialize()
Serializes the multinomial logistic regression model to a byte array for storage or transmission.
public override byte[] Serialize()
Returns
- byte[]
A byte array containing the serialized model data.
Remarks
This method converts the entire multinomial logistic regression model, including its parameters and configuration, into a byte array that can be stored in a file or database, or transmitted over a network. The model can later be restored using the Deserialize method.
For Beginners: This method saves the model to a format that can be stored or shared.
Serialization:
- Converts all the model's data into a sequence of bytes
- Preserves all the important information about the model
- Allows you to save the trained model to a file
- Lets you load the model later without having to retrain it
It's like taking a snapshot of the model that you can use later or share with others.
Train(Matrix<T>, Vector<T>)
Trains the multinomial logistic regression model using the provided features and target values.
public override void Train(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The feature matrix where each row is a sample and each column is a feature.
yVector<T>The target vector containing the class labels (as integers) for each sample.
Remarks
This method trains the multinomial logistic regression model using Newton's method to find the maximum likelihood estimates of the coefficients. It iteratively computes the probabilities, gradient, and Hessian matrix, and updates the coefficients until convergence or until the maximum number of iterations is reached. Regularization is applied if specified.
For Beginners: This is where the model learns from your data.
During training:
- The model starts with initial guesses for the coefficients
- It calculates how likely each class is for each training example
- It calculates how to change the coefficients to improve the predictions
- It updates the coefficients based on a mathematically optimal approach (Newton's method)
- It repeats steps 2-4 until the changes become very small or a maximum number of iterations is reached
This process finds the best coefficients for distinguishing between the different classes based on the features in your training data.