Class SplineRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
Implements spline regression, which models nonlinear relationships by fitting piecewise polynomial functions. This advanced regression technique offers more flexibility than simple linear regression by allowing the model to change its behavior across different regions of the data.
public class SplineRegression<T> : NonLinearRegressionBase<T>, INonLinearRegression<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
-
SplineRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
Spline regression uses basis functions centered at specific points called knots. The model combines: - A constant term - Polynomial terms of the input features (up to a specified degree) - Spline terms that activate beyond each knot
This creates a piecewise function that can smoothly adapt to local patterns in the data.
For Beginners: Spline regression is like drawing a curve through your data that can bend and adjust at specific points (called knots).
Think of it like this:
- Instead of forcing a single straight line through all your data
- The model places connection points (knots) where the curve can change direction
- These knots let the model adapt to different patterns in different regions of your data
For example, if modeling how temperature affects plant growth:
- Below freezing: plants don't grow at all (flat line)
- From freezing to optimal: growth increases rapidly (steep curve)
- Above optimal: growth slows again (flatter curve)
A spline regression can capture these changing relationships much better than a simple line.
Constructors
SplineRegression(SplineRegressionOptions?, IRegularization<T, Matrix<T>, Vector<T>>?)
Creates a new spline regression model.
public SplineRegression(SplineRegressionOptions? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsSplineRegressionOptionsOptional configuration settings for the spline regression model. These settings control aspects like:
- The number of knots to use for each feature
- The polynomial degree of the spline functions
- The matrix decomposition method used for solving the system If not provided, default options will be used.
regularizationIRegularization<T, Matrix<T>, Vector<T>>Optional regularization method to prevent overfitting. Regularization is a technique that helps the model perform better on new, unseen data by preventing it from fitting the training data too closely. If not provided, no regularization will be applied.
Remarks
This constructor creates a new spline regression model with the specified configuration options and regularization method. If options are not provided, default values are used. The constructor initializes the knots list and coefficients vector to empty collections, which will be populated during training.
For Beginners: This method sets up a new spline regression model before training.
Think of it like preparing to build a flexible curve:
- You decide how many bend points (knots) you want
- You choose how smooth the curve should be (degree)
- You can add safeguards to prevent the curve from becoming too wiggly (regularization)
After setting up the model with these options, you'll need to train it on your data to find the actual curve that best fits your points.
Methods
CreateInstance()
Creates a new instance of the Spline Regression model with the same configuration.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance of the Spline Regression model.
Remarks
This method creates a deep copy of the current Spline Regression model, including its options, knots, coefficients, 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 of your flexible curve:
- It copies all the configuration settings (like number of knots and degree)
- It preserves the locations of all bend points (knots)
- It duplicates all the coefficients that define the curve's shape
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 spline regression model from a byte array.
public override void Deserialize(byte[] modelData)
Parameters
modelDatabyte[]The byte array containing the serialized model data.
Remarks
This method reconstructs the model from a byte array created by the Serialize method. It restores the model's coefficients, knots, and configuration options, allowing a previously saved model to be loaded and used for predictions.
For Beginners: This method loads a saved model from computer memory.
Think of it like opening a saved document:
- It takes the byte array created by the Serialize method
- It rebuilds all the knots, coefficients, and settings
- The model is then ready to use for making predictions
This allows you to:
- Use a previously trained model without having to train it again
- Load models that others have shared with you
- Use the same model across different applications
GetModelType()
Returns the type identifier for this regression model.
protected override ModelType GetModelType()
Returns
- ModelType
The model type identifier for spline regression.
Remarks
This method returns the enum value that identifies this model as a spline regression model. This is used for model identification in serialization/deserialization and for logging purposes.
For Beginners: This method simply tells the system what kind of model this is.
It's like a name tag for the model that says "I am a spline regression model." This is useful when:
- Saving the model to a file
- Loading a model from a file
- Logging information about the model
You generally won't need to call this method directly in your code.
OptimizeModel(Matrix<T>, Vector<T>)
Optimizes the spline regression model using the provided input data and target values.
protected override void OptimizeModel(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input feature matrix, where rows represent observations and columns represent features.
yVector<T>The target values vector containing the actual output values to predict.
Remarks
This method implements the core optimization for spline regression. It: 1. Generates appropriate knots for each feature based on data distribution 2. Creates basis functions for the model (constant, polynomial, and spline terms) 3. Applies regularization to the basis functions if configured 4. Solves the linear system to find optimal coefficients 5. Applies regularization to the coefficients if configured
For Beginners: This method finds the best curve to fit your data points.
The process works like this:
- The model places knots (bend points) at strategic positions in your data
- It creates a set of special functions that can form curves with bends at these knots
- It finds the perfect combination of these functions to match your data
- If regularization is enabled, it ensures the curve stays reasonably smooth
After optimization, the model has learned the specific curve shape that best represents the pattern in your data.
Predict(Matrix<T>)
Predicts target values for a matrix of input features.
public override Vector<T> Predict(Matrix<T> input)
Parameters
inputMatrix<T>The input feature matrix for which to make predictions.
Returns
- Vector<T>
A vector of predicted values, one for each row in the input matrix.
Remarks
This method makes predictions for multiple input samples. It: 1. Generates the basis functions for the input data 2. Multiplies these basis functions by the model coefficients to get predictions
For Beginners: This method uses your trained model to make predictions for new data.
The prediction process works like this:
- The model transforms your input data into the special basis functions
- It applies the learned coefficients to these functions
- It combines the results to produce the final predictions
It's like using your recipe (the model) to prepare new dishes (predictions) using new ingredients (input data).
PredictSingle(Vector<T>)
Predicts a target value for a single input feature vector.
protected override T PredictSingle(Vector<T> input)
Parameters
inputVector<T>The input feature vector for which to make a prediction.
Returns
- T
The predicted value for the input vector.
Remarks
This method makes a prediction for a single input sample by: 1. Converting the input vector to a matrix with one row 2. Generating the basis functions for this matrix 3. Multiplying by the coefficients to get a prediction 4. Extracting the single prediction value
For Beginners: This method predicts the output for a single data point.
It works just like the batch prediction method, but for just one input:
- Transform the single input into basis functions
- Apply the learned coefficients
- Return the resulting prediction
For example, given a house's features, it would return a single predicted price.
Serialize()
Serializes the spline 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 model, including its coefficients, knots, and configuration options, into a byte array. This enables the model to be saved to a file, stored in a database, or transmitted over a network.
For Beginners: This method saves the model to computer memory so you can use it later.
Think of it like taking a snapshot of the model:
- It captures all the important values, knots, and coefficients
- It converts them into a format that can be easily stored
- The resulting byte array can be saved to a file or database
This is useful when you want to:
- Train the model once and use it many times
- Share the model with others
- Use the model in a different application