Class PolynomialRegression<T>
- Namespace
- AiDotNet.Regression
- Assembly
- AiDotNet.dll
Implements polynomial regression, which extends linear regression by fitting a polynomial equation to the data.
public class PolynomialRegression<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 (e.g., float, double).
- Inheritance
-
PolynomialRegression<T>
- Implements
-
IRegression<T>
- Inherited Members
- Extension Methods
Remarks
Polynomial regression is useful when the relationship between variables is not linear. It works by creating new features that are powers of the original features (x, x², x³, etc.), then applying linear regression techniques to these expanded features.
For Beginners: While linear regression fits a straight line to your data, polynomial regression can fit curves, allowing it to capture more complex patterns.
Constructors
PolynomialRegression(PolynomialRegressionOptions<T>?, IRegularization<T, Matrix<T>, Vector<T>>?)
Creates a new instance of the polynomial regression model.
public PolynomialRegression(PolynomialRegressionOptions<T>? options = null, IRegularization<T, Matrix<T>, Vector<T>>? regularization = null)
Parameters
optionsPolynomialRegressionOptions<T>Configuration options for the polynomial regression model, including the degree of the polynomial. The degree determines how complex the curve can be (e.g., degree 2 allows for parabolas).
regularizationIRegularization<T, Matrix<T>, Vector<T>>Optional regularization method to prevent overfitting. Regularization adds a penalty for large coefficient values, which helps create a more generalizable model.
Remarks
For Beginners: The degree of the polynomial determines how "curvy" your model can be. A higher degree (like 3 or 4) allows for more complex curves but may lead to overfitting if you don't have enough data.
Methods
CreateNewInstance()
Creates a new instance of the Polynomial 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 Polynomial Regression model.
Remarks
This method creates a deep copy of the current Polynomial Regression model, including its coefficients, intercept, and configuration options. 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 recipe:
- It copies all the configuration settings (like the polynomial degree)
- It preserves the coefficients (the weights for each polynomial term)
- It maintains the intercept (the starting point of your curve)
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.
GetModelType()
Gets the type of regression model.
protected override ModelType GetModelType()
Returns
- ModelType
The model type identifier for polynomial regression.
Predict(Matrix<T>)
Makes predictions using the trained polynomial regression model.
public override Vector<T> Predict(Matrix<T> input)
Parameters
inputMatrix<T>The input feature matrix where each row represents a data point to predict and each column represents a feature.
Returns
- Vector<T>
A vector of predicted values, one for each row in the input matrix.
Remarks
For Beginners: Once the model is trained, this method uses the discovered polynomial equation to predict outcomes for new data points. It first transforms the input features into polynomial features, then applies the learned coefficients to make predictions.
Train(Matrix<T>, Vector<T>)
Trains the polynomial regression model using the provided input features and target values.
public override void Train(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>The input feature matrix where each row represents a data point and each column represents a feature.
yVector<T>The target values vector where each element corresponds to a row in the input matrix.
Remarks
This method:
- Transforms the original features into polynomial features (x, x², x³, etc.)
- Adds a constant column for the intercept if specified in the options
- Solves the least squares equation to find the optimal coefficients
For Beginners: Training means finding the best curve that fits your data points. The algorithm creates additional features by raising your original features to different powers, then finds the best combination of these features to predict your target values.