NonNegativeLeastSquares Class |
Namespace: Accord.Statistics.Models.Regression.Fitting
public class NonNegativeLeastSquares : ISupervisedLearning<MultipleLinearRegression, double[], double>
The NonNegativeLeastSquares type exposes the following members.
Name | Description | |
---|---|---|
NonNegativeLeastSquares |
Initializes a new instance of the NonNegativeLeastSquares class.
| |
NonNegativeLeastSquares(MultipleLinearRegression) |
Initializes a new instance of the NonNegativeLeastSquares class.
|
Name | Description | |
---|---|---|
Coefficients |
Gets the coefficient vector being fitted.
| |
MaxIterations |
Gets or sets the maximum number of iterations to be performed.
| |
Token |
Gets or sets a cancellation token that can be used to
stop the learning algorithm while it is running.
| |
Tolerance |
Gets or sets the tolerance for detecting
convergence. Default is 0.001.
|
Name | Description | |
---|---|---|
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
Learn |
Learns a model that can map the given inputs to the given outputs.
| |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Run | Obsolete.
Runs the fitting algorithm.
| |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(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.) | |
ToT | 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.) |
References:
The following example shows how to fit a multiple linear regression model with the additional constraint that none of its coefficients should be negative. For this we can use the NonNegativeLeastSquares learning algorithm instead of the OrdinaryLeastSquares used above.
// Declare training samples var inputs = new double[][] { new[] { 1.0, 1.0, 1.0 }, new[] { 2.0, 4.0, 8.0 }, new[] { 3.0, 9.0, 27.0 }, new[] { 4.0, 16.0, 64.0 }, }; var outputs = new double[] { 0.23, 1.24, 3.81, 8.72 }; // Create a NN LS learning algorithm var nnls = new NonNegativeLeastSquares() { MaxIterations = 100 }; // Use the algorithm to learn a multiple linear regression MultipleLinearRegression regression = nnls.Learn(inputs, outputs); // None of the regression coefficients should be negative: double[] coefficients = regression.Weights; // should be // Check the quality of the regression: double[] prediction = regression.Transform(inputs); double error = new SquareLoss(expected: outputs) .Loss(actual: prediction); // should be 0