RANSACTModel Class |
Namespace: Accord.MachineLearning
The RANSACTModel type exposes the following members.
Name | Description | |
---|---|---|
RANSACTModel(Int32) |
Constructs a new RANSAC algorithm.
| |
RANSACTModel(Int32, Double) |
Constructs a new RANSAC algorithm.
| |
RANSACTModel(Int32, Double, Double) |
Constructs a new RANSAC algorithm.
|
Name | Description | |
---|---|---|
Degenerate |
Degenerative set detection function.
| |
Distances |
Distance function.
| |
Fitting |
Model fitting function.
| |
MaxEvaluations |
Maximum number of trials. Default is 1000.
| |
MaxSamplings |
Maximum number of attempts to select a
non-degenerate data set. Default is 100.
| |
Probability |
Gets or sets the probability of obtaining a random
sample of the input points that contains no outliers.
Default is 0.99.
| |
Samples |
Gets or sets the minimum number of samples from the data
required by the fitting function to fit a model.
| |
Threshold |
Gets or sets the minimum distance between a data point and
the model used to decide whether the point is an inlier or not.
| |
TrialsNeeded |
Gets the current estimate of trials needed.
| |
TrialsPerformed |
Gets the current number of trials performed.
|
Name | Description | |
---|---|---|
Compute(Int32) |
Computes the model using the RANSAC algorithm.
| |
Compute(Int32, Int32) |
Computes the model using the RANSAC algorithm.
| |
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.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
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.) |
RANSAC is an abbreviation for "RANdom SAmple Consensus". It is an iterative method to estimate parameters of a mathematical model from a set of observed data which contains outliers. It is a non-deterministic algorithm in the sense that it produces a reasonable result only with a certain probability, with this probability increasing as more iterations are allowed.
References:
// Fix the random number generator Accord.Math.Random.Generator.Seed = 0; double[,] data = // This is the same data used in the RANSAC sample app { { 1.0, 0.79 }, { 3, 2.18 }, { 5, 5.99 }, { 7.0, 7.65 }, { 9.0, 9.55 }, { 11, 11.89 }, { 13, 13.73 }, { 15.0, 14.77 }, { 17.0, 18.00 }, { 1.2, 1.45 }, { 1.5, 1.18 }, { 1.8, 1.92 }, { 2.1, 1.47 }, { 2.4, 2.41 }, { 2.7, 2.35 }, { 3.0, 3.41 }, { 3.3, 3.78 }, { 3.6, 3.21 }, { 3.9, 4.76 }, { 4.2, 5.03 }, { 4.5, 4.19 }, { 4.8, 3.81 }, { 5.1, 6.07 }, { 5.4, 5.74 }, { 5.7, 6.39 }, { 6, 6.11 }, { 6.3, 6.86 }, { 6.6, 6.35 }, { 6.9, 7.9 }, { 7.2, 8.04 }, { 7.5, 8.48 }, { 7.8, 8.07 }, { 8.1, 8.22 }, { 8.4, 8.41 }, { 8.7, 9.4 }, { 9, 8.8 }, { 9.3, 8.44 }, { 9.6, 9.32 }, { 9.9, 9.18 }, { 10.2, 9.86 }, { 10.5, 10.16 }, { 10.8, 10.28 }, { 11.1, 11.07 }, { 11.4, 11.66 }, { 11.7, 11.13 }, { 12, 11.55 }, { 12.3, 12.62 }, { 12.6, 12.27 }, { 12.9, 12.33 }, { 13.2, 12.37 }, { 13.5, 12.75 }, { 13.8, 14.44 }, { 14.1, 14.71 }, { 14.4, 13.72 }, { 14.7, 14.54 }, { 15, 14.67 }, { 15.3, 16.04 }, { 15.6, 15.21 }, { 1, 3.9 }, { 2, 11.5 }, { 3.0, 13.0 }, { 4, 0.9 }, { 5, 5.5 }, { 6, 16.2 }, { 7.0, 0.8 }, { 8, 9.4 }, { 9, 9.5 }, { 10, 17.5 }, { 11.0, 6.3 }, { 12, 12.6 }, { 13, 1.5 }, { 14, 1.5 }, { 2.0, 10 }, { 3, 9 }, { 15, 2 }, { 15.5, 1.2 }, }; // First, fit simple linear regression directly for comparison reasons. double[] x = data.GetColumn(0); // Extract the independent variable double[] y = data.GetColumn(1); // Extract the dependent variable // Use Ordinary Least Squares to learn the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Estimate a line passing through the (x, y) points SimpleLinearRegression regression = ols.Learn(x, y); // Now, compute the values predicted by the // regression for the original input points double[] commonOutput = regression.Transform(x); // Now, fit simple linear regression using RANSAC int maxTrials = 1000; int minSamples = 20; double probability = 0.950; double errorThreshold = 1000; // Create a RANSAC algorithm to fit a simple linear regression var ransac = new RANSAC<SimpleLinearRegression>(minSamples) { Probability = probability, Threshold = errorThreshold, MaxEvaluations = maxTrials, // Define a fitting function Fitting = (int[] sample) => { // Build a Simple Linear Regression model return new OrdinaryLeastSquares() .Learn(x.Get(sample), y.Get(sample)); }, // Define a inlier detector function Distances = (SimpleLinearRegression r, double threshold) => { var inliers = new List<int>(); for (int i = 0; i < x.Length; i++) { // Compute error for each point double error = r.Transform(x[i]) - y[i]; // If the square error is low enough, if (error * error < threshold) inliers.Add(i); // the point is considered an inlier. } return inliers.ToArray(); } }; // Now that the RANSAC hyperparameters have been specified, we can // compute another regression model using the RANSAC algorithm: int[] inlierIndices; SimpleLinearRegression robustRegression = ransac.Compute(data.Rows(), out inlierIndices); // Compute the output of the model fitted by RANSAC double[] ransacOutput = robustRegression.Transform(x);