public static double AdjustedRSquared(double[] expected, double[] predicted) { var loss = new RSquaredLoss(expected.Length, expected); loss.Adjust = true; return(loss.Loss(predicted)); }
public MultipleLinearRegression Learn(double[][] inputs, double[] outputs) { var ols = new OrdinaryLeastSquares() { UseIntercept = true }; // Use Ordinary Least Squares to estimate a regression model MultipleLinearRegression regression = ols.Learn(inputs, outputs); // As result, we will be given the following: //double a = regression.Weights[0]; // a = 0 //double b = regression.Weights[1]; // b = 0 //double c = regression.Intercept; // c = 1 // This is the plane described by the equation // ax + by + c = z => 0x + 0y + 1 = z => 1 = z. // We can compute the predicted points using double[] predicted = regression.Transform(inputs); // And the squared error loss using double error = new SquareLoss(outputs).Loss(predicted); // We can also compute other measures, such as the coefficient of determination r² double r2 = new RSquaredLoss(numberOfInputs: 2, expected: outputs).Loss(predicted); // should be 1 // We can also compute the adjusted or weighted versions of r² using var r2loss = new RSquaredLoss(numberOfInputs: 2, expected: outputs) { Adjust = true, // Weights = weights; // (if you have a weighted problem) }; double ar2 = r2loss.Loss(predicted); // should be 1 // Alternatively, we can also use the less generic, but maybe more user-friendly method directly: double ur2 = regression.CoefficientOfDetermination(inputs, outputs, adjust: true); // should be 1 Console.WriteLine("Weights:"); foreach (var w in regression.Weights) { Console.WriteLine($",{w}"); } Console.WriteLine("Intercept:"); Console.WriteLine($",{regression.Intercept}"); Console.WriteLine($"error:{error}"); Console.WriteLine($"r2:{r2}"); Console.WriteLine($"r2loss:{r2loss}"); Console.WriteLine($"ar2:{ar2}"); Console.WriteLine($"ur2:{ur2}"); return(regression); }
/// <summary> /// Gets the coefficient of determination, as known as R² (r-squared). /// </summary> /// /// <remarks> /// <para> /// The coefficient of determination is used in the context of statistical models /// whose main purpose is the prediction of future outcomes on the basis of other /// related information. It is the proportion of variability in a data set that /// is accounted for by the statistical model. It provides a measure of how well /// future outcomes are likely to be predicted by the model.</para> /// <para> /// The R² coefficient of determination is a statistical measure of how well the /// regression line approximates the real data points. An R² of 1.0 indicates /// that the regression line perfectly fits the data.</para> /// </remarks> /// /// <returns>The R² (r-squared) coefficient for the given data.</returns> /// public double CoefficientOfDetermination(double[] inputs, double[] outputs, bool adjust, double[] weights = null) { var rsquared = new RSquaredLoss(NumberOfInputs, outputs); rsquared.Adjust = adjust; if (weights != null) { rsquared.Weights = weights; } return(rsquared.Loss(Transform(inputs))); }
public void learn_test_2() { #region doc_learn_2 // Let's say we would like predict a continuous number from a set // of discrete and continuous input variables. For this, we will // be using the Servo dataset from UCI's Machine Learning repository // as an example: http://archive.ics.uci.edu/ml/datasets/Servo // Create a Servo dataset Servo servo = new Servo(); object[][] instances = servo.Instances; // 167 x 4 double[] outputs = servo.Output; // 167 x 1 // This dataset contains 4 columns, where the first two are // symbolic (having possible values A, B, C, D, E), and the // last two are continuous. // We will use a codification filter to transform the symbolic // variables into one-hot vectors, while keeping the other two // continuous variables intact: var codebook = new Codification <object>() { { "motor", CodificationVariable.Categorical }, { "screw", CodificationVariable.Categorical }, { "pgain", CodificationVariable.Continuous }, { "vgain", CodificationVariable.Continuous }, }; // Learn the codebook codebook.Learn(instances); // We can gather some info about the problem: int numberOfInputs = codebook.NumberOfInputs; // should be 4 (since there are 4 variables) int numberOfOutputs = codebook.NumberOfOutputs; // should be 12 (due their one-hot encodings) // Now we can use it to obtain double[] vectors: double[][] inputs = codebook.ToDouble().Transform(instances); // We will use Ordinary Least Squares to create a // linear regression model with an intercept term var ols = new OrdinaryLeastSquares() { UseIntercept = true }; // Use Ordinary Least Squares to estimate a regression model: MultipleLinearRegression regression = ols.Learn(inputs, outputs); // We can compute the predicted points using: double[] predicted = regression.Transform(inputs); // And the squared error using the SquareLoss class: double error = new SquareLoss(outputs).Loss(predicted); // We can also compute other measures, such as the coefficient of determination r² using: double r2 = new RSquaredLoss(numberOfOutputs, outputs).Loss(predicted); // should be 0.55086630162967354 // Or the adjusted or weighted versions of r² using: var r2loss = new RSquaredLoss(numberOfOutputs, outputs) { Adjust = true, // Weights = weights; // (uncomment if you have a weighted problem) }; double ar2 = r2loss.Loss(predicted); // should be 0.51586887058782993 // Alternatively, we can also use the less generic, but maybe more user-friendly method directly: double ur2 = regression.CoefficientOfDetermination(inputs, outputs, adjust: true); // should be 0.51586887058782993 #endregion Assert.AreEqual(4, numberOfInputs); Assert.AreEqual(12, numberOfOutputs); Assert.AreEqual(12, regression.NumberOfInputs); Assert.AreEqual(1, regression.NumberOfOutputs); Assert.AreEqual(1.0859586717266123, error, 1e-6); double[] expected = regression.Compute(inputs); double[] actual = regression.Transform(inputs); Assert.IsTrue(expected.IsEqual(actual, 1e-10)); Assert.AreEqual(0.55086630162967354, r2); Assert.AreEqual(0.51586887058782993, ar2); Assert.AreEqual(0.51586887058782993, ur2); }
public void learn_test() { #region doc_learn // We will try to model a plane as an equation in the form // "ax + by + c = z". We have two input variables (x and y) // and we will be trying to find two parameters a and b and // an intercept term c. // We will use Ordinary Least Squares to create a // linear regression model with an intercept term var ols = new OrdinaryLeastSquares() { UseIntercept = true }; // Now suppose you have some points double[][] inputs = { new double[] { 1, 1 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 0, 0 }, }; // located in the same Z (z = 1) double[] outputs = { 1, 1, 1, 1 }; // Use Ordinary Least Squares to estimate a regression model MultipleLinearRegression regression = ols.Learn(inputs, outputs); // As result, we will be given the following: double a = regression.Weights[0]; // a = 0 double b = regression.Weights[1]; // b = 0 double c = regression.Intercept; // c = 1 // This is the plane described by the equation // ax + by + c = z => 0x + 0y + 1 = z => 1 = z. // We can compute the predicted points using double[] predicted = regression.Transform(inputs); // And the squared error loss using double error = new SquareLoss(outputs).Loss(predicted); // We can also compute other measures, such as the coefficient of determination r² double r2 = new RSquaredLoss(numberOfInputs: 2, expected: outputs).Loss(predicted); // should be 1 // We can also compute the adjusted or weighted versions of r² using var r2loss = new RSquaredLoss(numberOfInputs: 2, expected: outputs) { Adjust = true, // Weights = weights; // (if you have a weighted problem) }; double ar2 = r2loss.Loss(predicted); // should be 1 // Alternatively, we can also use the less generic, but maybe more user-friendly method directly: double ur2 = regression.CoefficientOfDetermination(inputs, outputs, adjust: true); // should be 1 #endregion Assert.AreEqual(2, regression.NumberOfInputs); Assert.AreEqual(1, regression.NumberOfOutputs); Assert.AreEqual(0.0, a, 1e-6); Assert.AreEqual(0.0, b, 1e-6); Assert.AreEqual(1.0, c, 1e-6); Assert.AreEqual(0.0, error, 1e-6); double[] expected = regression.Compute(inputs); double[] actual = regression.Transform(inputs); Assert.IsTrue(expected.IsEqual(actual, 1e-10)); Assert.AreEqual(1.0, r2); Assert.AreEqual(1.0, ar2); Assert.AreEqual(1.0, ur2); }