Click or drag to resize
Accord.NET (logo)

PolynomialLeastSquares Class

Polynomial Least-Squares.
Inheritance Hierarchy
SystemObject
  Accord.Statistics.Models.Regression.LinearPolynomialLeastSquares

Namespace:  Accord.Statistics.Models.Regression.Linear
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class PolynomialLeastSquares : ISupervisedLearning<PolynomialRegression, double, double>
Request Example View Source

The PolynomialLeastSquares type exposes the following members.

Constructors
  NameDescription
Public methodPolynomialLeastSquares
Initializes a new instance of the PolynomialLeastSquares class
Top
Properties
  NameDescription
Public propertyDegree
Gets or sets the polynomial degree to use in the polynomial regression.
Public propertyIsRobust
Gets or sets whether to always use a robust Least-Squares estimate using the SingularValueDecomposition. Default is false.
Public propertyToken
Gets or sets a cancellation token that can be used to stop the learning algorithm while it is running.
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLearn
Learns a model that can map the given inputs to the given outputs.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks
In linear regression, the model specification is that the dependent variable, y is a linear combination of the parameters (but need not be linear in the independent variables). As the linear regression has a closed form solution, the regression coefficients can be efficiently computed using the Regress method of this class.
Examples
// Let's say we would like to learn 2nd degree polynomial that 
// can map the first column X into its second column Y. We have 
// 5 examples of those (x,y) pairs that we can use to learn this 
// function:

double[,] data =
{
    // X       Y
    { 12,     144 }, // example #1
    { 15,     225 }, // example #2
    { 20,     400 }, // example #3
    { 25,     625 }, // example #4
    { 35,    1225 }, // example #5
};

// Let's retrieve the input and output data:
double[] inputs = data.GetColumn(0);  // X
double[] outputs = data.GetColumn(1); // Y

// We can create a learning algorithm
var ls = new PolynomialLeastSquares()
{
    Degree = 2
};

// Now, we can use the algorithm to learn a polynomial
PolynomialRegression poly = ls.Learn(inputs, outputs);

// The learned polynomial will be given by
string str = poly.ToString("N1"); // "y(x) = 1.0x^2 + 0.0x^1 + 0.0"

// Where its weights can be accessed using
double[] weights = poly.Weights;   // { 1.0000000000000024, -1.2407665029287351E-13 }
double intercept = poly.Intercept; // 1.5652369518855253E-12

// Finally, we can use this polynomial
// to predict values for the input data
double[] pred = poly.Transform(inputs);

// Where the mean-squared-error (MSE) should be
double error = new SquareLoss(outputs).Loss(pred); // 0.0
See Also