private static void multivariateLinear() { double[][] inputs = { // variables: x1 x2 x3 new double[] { 1, 1, 1 }, // input sample 1 new double[] { 2, 1, 1 }, // input sample 2 new double[] { 3, 1, 1 }, // input sample 3 }; double[][] outputs = { // variables: y1 y2 new double[] { 2, 3 }, // corresponding output to sample 1 new double[] { 4, 6 }, // corresponding output to sample 2 new double[] { 6, 9 }, // corresponding output to sample 3 }; // Use Ordinary Least Squares to create the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Now, compute the multivariate linear regression: MultivariateLinearRegression regression = ols.Learn(inputs, outputs); // We can obtain predictions using double[][] predictions = regression.Transform(inputs); // The prediction error is double error = new SquareLoss(outputs).Loss(predictions); // 0 }
//EXAMPLE: http://accord-framework.net/docs/html/T_Accord_Statistics_Models_Regression_Linear_MultivariateLinearRegression.htm static void Main(string[] args) { CSV_Parser parser = new CSV_Parser(); RegressionData data = parser.ParseDataFile(); // Use Ordinary Least Squares to create the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Now, compute the multivariate linear regression: MultivariateLinearRegression regression = ols.Learn(data.InterestRatings, data.MajorRatings); // We can obtain predictions using double[][] predictions = regression.Transform(data.InterestRatings); // The prediction error is double error = new SquareLoss(data.MajorRatings).Loss(predictions); // 0 // We can also check the r-squared coefficients of determination: //double[] r2 = regression.CoefficientOfDetermination(topicRatings, majorRatings); double[][] r2 = regression.Weights; Console.WriteLine("WEIGHTS:"); //writeCSVfile(data, r2); GenerateCSFile(data, r2); Console.WriteLine("Coefficient Of Determination"); double[] r3 = regression.CoefficientOfDetermination(data.InterestRatings, data.MajorRatings); for (int i = 0; i < r3.Length; i++) { Console.WriteLine(r3[i]); } Console.Read(); }
public void learn_test_2() { #if NETCORE var culture = CultureInfo.CreateSpecificCulture("en-US"); CultureInfo.CurrentCulture = culture; #endif #region doc_learn // 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 #endregion Assert.AreEqual(0, error, 1e-10); string ex = weights.ToCSharp(); double[] expected = { 1, 0 }; Assert.AreEqual("y(x) = 1.0x^2 + 0.0x^1 + 0.0", str); Assert.IsTrue(weights.IsEqual(expected, 1e-6)); Assert.AreEqual(0, intercept, 1e-6); }
/// <summary> /// Print test results to console. /// </summary> private static void PrintTestResult() { var originalOutputs = data.GetExpectedClassificationOutput(); var originalOuputsRegression = data.GetExpectedRegressionOutput(); var matrix = new ConfusionMatrix(originalOutputs, resultClassification); Console.WriteLine("Test results:"); Console.WriteLine("True positive\t" + matrix.TruePositives); Console.WriteLine("True negative\t" + matrix.TrueNegatives); Console.WriteLine("False positive\t" + matrix.FalsePositives); Console.WriteLine("False negative\t" + matrix.FalseNegatives); Console.WriteLine(); Console.WriteLine("Sensitivity\t" + matrix.Sensitivity); Console.WriteLine("Specificity\t" + matrix.Specificity); Console.WriteLine("F-Score\t\t" + matrix.FScore); Console.WriteLine("MCC\t\t" + matrix.MatthewsCorrelationCoefficient); Console.WriteLine("Precision\t" + matrix.Precision); Console.WriteLine("Accuracy\t" + matrix.Accuracy); Console.WriteLine(); var mse = new SquareLoss(originalOuputsRegression).Loss(resultRegression); Console.WriteLine("MSE: " + mse); }
public static void test2() { var ols = new OrdinaryLeastSquares() { UseIntercept = true }; double[][] inputs = { new double[] { 1, 1 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 0, 0 }, }; double[] outputs = { 1, 1, 1, 1 }; MultipleLinearRegression regression = ols.Learn(inputs, outputs); double a = regression.Weights[0]; // a = 0 double b = regression.Weights[1]; // b = 0 double c = regression.Intercept; // c = 1 double[] predicted = regression.Transform(inputs); double error = new SquareLoss(outputs).Loss(predicted); }
private static void LinearRegressionLearning(IEnumerable <MatchingPair> trainingData, IEnumerable <MatchingPair> testData, IDictionary <string, IndexableAttributeMetadata> actualMetadata) { var stopWatch = new Stopwatch(); stopWatch.Start(); var trainingInputs = trainingData.Select(data => data.ToVectorArray(actualMetadata)).ToArray(); var trainingOutputs = trainingData.Select(data => new[] { data.PercentMatch }).ToArray(); var testInputs = testData.Select(data => data.ToVectorArray(actualMetadata)).ToArray(); var testOutputs = testData.Select(data => new[] { data.PercentMatch }).ToArray(); var leastSquares = new OrdinaryLeastSquares(); var regression = leastSquares.Learn(trainingInputs, trainingOutputs); var predictions = regression.Transform(trainingInputs); var error = new SquareLoss(trainingOutputs).Loss(predictions); Logger.InfoFormat("Linear Regression: In-sample error: {0}", error); predictions = regression.Transform(testInputs); error = new SquareLoss(testOutputs).Loss(predictions); Logger.InfoFormat("Linear Regression: Out-of-sample error: {0}", error); stopWatch.Stop(); Logger.InfoFormat("Linear Regression learning took {0}", stopWatch.Elapsed); }
public MultivariateLinearRegressionBenchmarkModel(List <ProcessedSurveyRecordModel> models) { var inputs = new double[models.Count][]; var outputs = new double[models.Count]; for (var i = 0; i < models.Count; i++) { var currentModel = models[i]; var currentInputs = new List <double>(); outputs[i] = (double)currentModel.Salary; currentInputs.Add((double)currentModel.YearsCoding); currentInputs.Add((double)currentModel.YearsProfessionalCoding); currentInputs.Add(Convert.ToDouble(currentModel.HasAdditionalEducation)); inputs[i] = currentInputs.ToArray(); } var regressionModel = new OrdinaryLeastSquares().Learn(inputs, outputs); var predictions = regressionModel.Transform(inputs); var squareLoss = new SquareLoss(outputs) { Mean = true }; Error = new SquareLoss(outputs).Loss(predictions); }
public static double ComputeError(IEnumerable <XtoY> ds, IEnumerable <XtoY> predicted) { double [] outputs = ds.Select(i => i.Y).ToArray(); double [] preds = predicted.Select(i => i.Y).ToArray(); double error = new SquareLoss(outputs).Loss(preds); return(error); }
public void SquareLoss_Loss() { var targets = Matrix <float> .Build.Dense(6, 1, new float[] { 0, 0, 0, 0, 0, 0 }); var predictions = Matrix <float> .Build.Dense(6, 1, new float[] { 0, 0, 0, 0, 0, 0 }); var sut = new SquareLoss(); var actual = sut.Loss(targets, predictions); Assert.AreEqual(0f, actual); }
public void SquareLoss_Loss_1() { var targets = Matrix <float> .Build.Dense(5, 1, new float[] { 1.0f, 2.3f, 3.1f, 4.4f, 5.8f }); var predictions = Matrix <float> .Build.Dense(5, 1, new float[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f }); var sut = new SquareLoss(); var actual = sut.Loss(targets, predictions); Assert.AreEqual(0.09f, actual, 0.0001); }
public void SquareLoss_Loss_Multi_Dimensional() { var targets = Matrix <float> .Build.Dense(3, 3, new float[] { 1.0f, 2.3f, 3.1f, 4.4f, 5.8f, 1.0f, 3.5f, 2f, 5f }); var predictions = Matrix <float> .Build.Dense(3, 3, new float[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 1.0f, 3.7f, 1.6f, 5.4f }); var sut = new SquareLoss(); var actual = sut.Loss(targets, predictions); Assert.AreEqual(0.07f, actual, 0.0001); }
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); }
public void learn_test() { #region doc_learn Accord.Math.Random.Generator.Seed = 0; // Example regression problem. Suppose we are trying // to model the following equation: f(x, y) = 2x + y double[][] inputs = // (x, y) { new double[] { 0, 1 }, // 2*0 + 1 = 1 new double[] { 4, 3 }, // 2*4 + 3 = 11 new double[] { 8, -8 }, // 2*8 - 8 = 8 new double[] { 2, 2 }, // 2*2 + 2 = 6 new double[] { 6, 1 }, // 2*6 + 1 = 13 new double[] { 5, 4 }, // 2*5 + 4 = 14 new double[] { 9, 1 }, // 2*9 + 1 = 19 new double[] { 1, 6 }, // 2*1 + 6 = 8 }; double[] outputs = // f(x, y) { 1, 11, 8, 6, 13, 14, 19, 8 }; // Create the sequential minimal optimization teacher var learn = new SequentialMinimalOptimizationRegression <Polynomial>() { Kernel = new Polynomial(2), // Polynomial Kernel of 2nd degree Complexity = 100 }; // Run the learning algorithm SupportVectorMachine <Polynomial> svm = learn.Learn(inputs, outputs); // Compute the predicted scores double[] predicted = svm.Score(inputs); // Compute the error between the expected and predicted double error = new SquareLoss(outputs).Loss(predicted); // Compute the answer for one particular example double fxy = svm.Score(inputs[0]); // 1.0003849827673186 #endregion Assert.AreEqual(1.0, fxy, 1e-2); for (int i = 0; i < outputs.Length; i++) { Assert.AreEqual(outputs[i], predicted[i], 1e-2); } }
public void MultipleLinearRegressionLearning() { int n = LearningData.Length; double[] dependentVariables = LearningData.GetColumn(3); double[][] independentVariables = new double[n][]; for (int j = 0; j < n; j++) { dependentVariables[j] = LearningData[j][3]; } for (int j = 0; j < n; j++) { independentVariables[j] = new double[3]; independentVariables[j][0] = NormalizedInputData[j][0]; independentVariables[j][1] = NormalizedInputData[j][1]; independentVariables[j][2] = NormalizedInputData[j][2]; } multipleLinearRegressionObj = MultipleLinearRegression.FromData(independentVariables, dependentVariables); double[] prediction = multipleLinearRegressionObj.Transform(independentVariables); PredictedData[0] = prediction; ErrorMLR = new SquareLoss(dependentVariables).Loss(prediction); double[] coefOfFunction = new double[multipleLinearRegressionObj.Weights.Length + 1]; coefOfFunction[0] = multipleLinearRegressionObj.Intercept; int index = multipleLinearRegressionObj.Weights.Length - 1; for (int i = 1; i <= multipleLinearRegressionObj.Weights.Length; i++) { coefOfFunction[i] = multipleLinearRegressionObj.Weights[index]; index--; } double func(double _x1, double _x2, double _x3) => (coefOfFunction[0] + coefOfFunction[3] * _x1 + coefOfFunction[2] * _x2 + coefOfFunction[1] * _x3); XYpairMLR[0] = new double[100]; XYpairMLR[1] = new double[100]; for (int i = 0; i < 100; i++) { XYpairMLR[0][i] = i; XYpairMLR[1][i] = func(i, i, i); } }
public void PolyRegression() { if (dgvTestingSource.DataSource == null) { MessageBox.Show("Please Select a data set"); return; } // Creates a matrix from the source data table double[,] table = (dgvLearningSource.DataSource as DataTable).ToMatrix(); double[,] data = table; // 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 double[][] tmpInputs = new double[inputs.Length][]; for (int i = 0; i < inputs.Length; i++) { tmpInputs[i] = new double[1] { inputs[i] }; } CreateResultScatterplot(zedGraphControl1, tmpInputs, outputs, pred); }
private static double GetRMSE(IEnumerable <BasicDataset> expected, IEnumerable <BasicDataset> predicted) { double[] expectedValues = expected.Select(a => (double)a.Close).ToArray(), predictedValues = predicted.Select(a => (double)a.Close).ToArray(); var squareLoss = new SquareLoss(expectedValues) { Mean = false, Root = true }; var rmse = squareLoss.Loss(predictedValues); return(rmse); }
public double ComputeError(IEnumerable <XtoY> ds, IEnumerable <XtoY> predicted) { List <double> data = new List <double>(mylist.Count); for (int i = 0; i < mylist.Count; i++) { data.Add(mylist[i].Cases); } double[] outputs = data.ToArray(); double[] preds = predicted.Select(i => i.Y).ToArray(); double error = new SquareLoss(outputs).Loss(preds); return(error); }
private static void optimization(double[][] inputs, double[] outputs) { // Non-linear regression can also be solved using arbitrary models // that can be defined by the user. For example, let's say we know // the overall model for the outputs but we do not know the value // of its parameters: log(w0 * x) / sqrt(w1 * y + w2) Func <double[], double[], double> model = (double[] x, double[] w) => Math.Log(w[0] * x[0]) / Math.Sqrt(w[1] * x[1] + w[2]); // Now that we have the model, we want to find which values we // can plug in its parameters such that the error when evaluating // in our data is as close to zero as possible. Mathematically, we // would like to find the best parameters w that minimizes: Func <double[], double> objective = (double[] w) => { double sumOfSquares = 0.0; for (int i = 0; i < inputs.Length; i++) { double expected = outputs[i]; double actual = model(inputs[i], w); sumOfSquares += Math.Pow(expected - actual, 2); } return(sumOfSquares); }; // Now, let's use a gradient-free optimization algorithm to // find the best parameters for our model's equations: var cobyla = new Cobyla(numberOfVariables: 3) // we have 3 parameters: w0, w1, and w2 { Function = objective, MaxIterations = 100, Solution = new double[] { 1.0, 6.4, 100 } // start with some random values }; bool success = cobyla.Minimize(); // should be true double[] solution = cobyla.Solution; // Get machine's predictions for inputs double[] prediction = inputs.Apply(x => model(x, solution)); // Compute the error in the prediction (should be 0.0) double error = new SquareLoss(outputs).Loss(prediction); Console.WriteLine(error); // should be 0.000 }
private static void kernelSvm2(double[][] inputs, double[] outputs) { // Create a new Sequential Minimal Optimization (SMO) learning // algorithm and estimate the complexity parameter C from data var teacher = new SequentialMinimalOptimizationRegression <Gaussian>() { UseComplexityHeuristic = true, UseKernelEstimation = true // estimate the kernel from the data }; // Teach the vector machine var svm = teacher.Learn(inputs, outputs); // Classify the samples using the model double[] answers = svm.Score(inputs); double error = new SquareLoss(outputs).Loss(answers); // should be }
public void learn_test() { #region doc_learn // 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 #endregion Assert.AreEqual(0, error, 1e-10); Assert.AreEqual(0.1, nnls.Coefficients[0], 1e-3); Assert.AreEqual(0, nnls.Coefficients[1], 1e-3); Assert.AreEqual(0.13, nnls.Coefficients[2], 1e-3); Assert.AreEqual(0.1, regression.Coefficients[0], 1e-3); Assert.AreEqual(0, regression.Coefficients[1], 1e-3); Assert.AreEqual(0.13, regression.Coefficients[2], 1e-3); }
private static void linearSvm1() { // Declare a very simple regression problem // with only 2 input variables (x and y): double[][] inputs = { new[] { 3.0, 1.0 }, new[] { 7.0, 1.0 }, new[] { 3.0, 1.0 }, new[] { 3.0, 2.0 }, new[] { 6.0, 1.0 }, }; // The task is to output a weighted sum of those numbers // plus an independent constant term: 7.4x + 1.1y + 42 double[] outputs = { 7.4 * 3.0 + 1.1 * 1.0 + 42.0, 7.4 * 7.0 + 1.1 * 1.0 + 42.0, 7.4 * 3.0 + 1.1 * 1.0 + 42.0, 7.4 * 3.0 + 1.1 * 2.0 + 42.0, 7.4 * 6.0 + 1.1 * 1.0 + 42.0, }; // Create a new Sequential Minimal Optimization (SMO) learning // algorithm and estimate the complexity parameter C from data var teacher = new SequentialMinimalOptimizationRegression <Linear>() { UseComplexityHeuristic = true, Complexity = 100000.0 // Note: do not do this in an actual application! // Setting the Complexity property to a very high value forces the SVM // to "believe literally" in whatever the data says. Normally, the SVM // would be more cautions under the (valid) assumption that the data // might actually contain noise and/or incorrect measurements. }; // Teach the vector machine var svm = teacher.Learn(inputs, outputs); // Classify the samples using the model double[] answers = svm.Score(inputs); double error = new SquareLoss(outputs).Loss(answers); // should be 0.0 }
private static void linearSvm2() { // Declare a very simple regression problem // with only 2 input variables (x and y): double[][] inputs = { new[] { 3.0, 1.0 }, new[] { 7.0, 1.0 }, new[] { 3.0, 1.0 }, new[] { 3.0, 2.0 }, new[] { 6.0, 1.0 }, }; // The task is to output a weighted sum of those numbers // plus an independent constant term: 7.4x + 1.1y + 42 double[] outputs = { 7.4 * 3.0 + 1.1 * 1.0 + 42.0, 7.4 * 7.0 + 1.1 * 1.0 + 42.0, 7.4 * 3.0 + 1.1 * 1.0 + 42.0, 7.4 * 3.0 + 1.1 * 2.0 + 42.0, 7.4 * 6.0 + 1.1 * 1.0 + 42.0, }; // Create Newton-based support vector regression var teacher = new LinearRegressionNewtonMethod() { Tolerance = 1e-5, Complexity = 10000 }; // Use the algorithm to learn the machine var svm = teacher.Learn(inputs, outputs); // Get machine's predictions for inputs double[] prediction = svm.Score(inputs); // Compute the error in the prediction (should be 0.0) double error = new SquareLoss(outputs).Loss(prediction); Console.WriteLine(error); }
public static void test3() { var poly2 = CreateFunc(1, 1); Random rnd = new Random(); var pos = Enumerable.Range(0, 20).Select(x => new double [] { x, poly2(x) + rnd.NextDouble() }).ToArray(); double[] inputs = pos.Select(x => x[0]).ToArray(); double[] outputs = pos.Select(x => x[1]).ToArray(); var ls = new PolynomialLeastSquares() { Degree = 2 }; PolynomialRegression poly = ls.Learn(inputs, outputs); double a = poly.Weights[0]; // a = 0 double b = poly.Weights[1]; // b = 0 double c = poly.Intercept; // c = 1 double[] predicted = poly.Transform(inputs); double error = new SquareLoss(outputs).Loss(predicted); var ols = new OrdinaryLeastSquares(); SimpleLinearRegression mul = ols.Learn(inputs, outputs); double a1 = mul.Slope; // a = 0 double b1 = mul.Intercept; // b = 0 double[] simplepredict = mul.Transform(inputs); double erroe2 = new SquaredHingeLoss(outputs).Loss(simplepredict); Console.WriteLine("Done"); }
private double[] learning() { ////// bool success = cobyla.Minimize(); // should be true Debug.Log("success " + success); double[] solution = cobyla.Solution; display_array("solution", solution); // Get machine's predictions for inputs double[] prediction = inputs.Apply(x => model(x, solution)); display_array("prediction", prediction); display_array("output", outputs); // Compute the error in the prediction (should be 0.0) double error = new SquareLoss(outputs).Loss(prediction); display_array("error", new double[] { error }); return(prediction); }
private static void kernelSvm1(double[][] inputs, double[] outputs) { // Create a LibSVM-based support vector regression algorithm var teacher = new FanChenLinSupportVectorRegression <Gaussian>() { Tolerance = 1e-5, Complexity = 10000, Kernel = new Gaussian(0.1) }; // Use the algorithm to learn the machine var svm = teacher.Learn(inputs, outputs); // Get machine's predictions for inputs double[] prediction = svm.Score(inputs); // Compute the error in the prediction (should be 0.0) double error = new SquareLoss(outputs).Loss(prediction); Console.WriteLine(error); }
private static void multipleLinearRegression() { // 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 }; // 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); // 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); }
public void logarithm_learn() { #region doc_learn // This is the same data from the example available at // http://mathbits.com/MathBits/TISection/Statistics2/logarithmic.htm // Declare your inputs and output data double[] inputs = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; double[] outputs = { 6, 9.5, 13, 15, 16.5, 17.5, 18.5, 19, 19.5, 19.7, 19.8 }; // Transform inputs to logarithms double[] logx = Matrix.Log(inputs); // Use Ordinary Least Squares to learn the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Use OLS to learn the simple linear regression SimpleLinearRegression lr = ols.Learn(logx, outputs); // Compute predicted values for inputs double[] predicted = lr.Transform(logx); // Get an expression representing the learned regression model // We just have to remember that 'x' will actually mean 'log(x)' string result = lr.ToString("N4", CultureInfo.InvariantCulture); // Result will be "y(x) = 6.1082x + 6.0993" // The mean squared error between the expected and the predicted is double error = new SquareLoss(outputs).Loss(predicted); // 0.261454 #endregion Assert.AreEqual(0.26145460024250794, error, 1e-8); Assert.AreEqual(6.1081800414945704, lr.Slope, 1e-8); Assert.AreEqual(6.0993411396126653, lr.Intercept, 1e-8); Assert.AreEqual("y(x) = 6.1082x + 6.0993", result); }
public void SinTest1() { const int batchSize = 10; const double batchStep = 0.1; const int epochs = 10000; const int testBatchSize = 5; Random random = new Random(0); Network network = Network.FromArchitecture("1x1x1~10-10-1SRN"); (Tensor, Tensor) createSample(int size) { Tensor input = new Tensor(null, new[] { size, 1, 1, 1 }); Tensor expected = new Tensor(null, new[] { size, 1 }); double rv = (float)(random.NextDouble() * Math.PI * 2); for (int b = 0; b <= size; b++, rv += batchStep) { float value = (float)((Math.Sin(rv) / 2.0) + 0.5); if (b < size) { input.Weights[b] = value; } if (b - 1 >= 0) { expected.Weights[b - 1] = value; } } return(input, expected); } // train the network SquareLoss loss = new SquareLoss(); Trainer <Tensor> trainer = new Trainer <Tensor>() /*ClipValue = 2.0f*/ }
public void linear_regression_test() { #region doc_linreg // Declare some training data. This is exactly the same // data used in the MultipleLinearRegression documentation page // 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. // Create the linear-SVM learning algorithm var teacher = new LinearDualCoordinateDescent() { Tolerance = 1e-10, Complexity = 1e+10, // learn a hard-margin model }; // 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 }; // Learn the support vector machine var svm = teacher.Learn(inputs, outputs); // Convert the svm to logistic regression var regression = (MultipleLinearRegression)svm; // 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); #endregion var rsvm = (SupportVectorMachine)regression; Assert.AreEqual(2, rsvm.NumberOfInputs); Assert.AreEqual(2, rsvm.NumberOfOutputs); double[] svmpred = svm.Score(inputs); Assert.IsTrue(predicted.IsEqual(svmpred)); 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)); double r = regression.CoefficientOfDetermination(inputs, outputs); Assert.AreEqual(1.0, r); }
public void learn_test() { #region doc_learn Accord.Math.Random.Generator.Seed = 0; // Example regression problem. Suppose we are trying // to model the following equation: f(x, y) = 2x + y double[][] inputs = // (x, y) { new double[] { 0, 1 }, // 2*0 + 1 = 1 new double[] { 4, 3 }, // 2*4 + 3 = 11 new double[] { 8, -8 }, // 2*8 - 8 = 8 new double[] { 2, 2 }, // 2*2 + 2 = 6 new double[] { 6, 1 }, // 2*6 + 1 = 13 new double[] { 5, 4 }, // 2*5 + 4 = 14 new double[] { 9, 1 }, // 2*9 + 1 = 19 new double[] { 1, 6 }, // 2*1 + 6 = 8 }; double[] outputs = // f(x, y) { 1, 11, 8, 6, 13, 14, 19, 8 }; // Create the sequential minimal optimization teacher var learn = new SequentialMinimalOptimizationRegression<Polynomial>() { Kernel = new Polynomial(2), // Polynomial Kernel of 2nd degree Complexity = 100 }; // Run the learning algorithm SupportVectorMachine<Polynomial> svm = learn.Learn(inputs, outputs); // Compute the predicted scores double[] predicted = svm.Score(inputs); // Compute the error between the expected and predicted double error = new SquareLoss(outputs).Loss(predicted); // Compute the answer for one particular example double fxy = svm.Score(inputs[0]); // 1.0003849827673186 #endregion Assert.AreEqual(1.0, fxy, 1e-2); for (int i = 0; i < outputs.Length; i++) Assert.AreEqual(outputs[i], predicted[i], 1e-2); }
private void btnLoadCsv_Click(object sender, RoutedEventArgs e) { try { var filepath = Dialog.OpenFileDia(); if (filepath == "") { return; } CsvTool cv = new CsvTool(); var res = cv.ReadCsv2String(filepath, ','); double head = 0; if (!Double.TryParse(res [0] [0], out head)) { res = res.Skip(1).ToArray(); } var numberList = res.Select(x => x.Select(f => f.ToDouble()).ToArray()).ToArray(); if (numberList.SelectMany( x => x.Select(k => k), (f, s) => s) .Where(x => x == 123456789) .Count() > 0) { Console.WriteLine("Invalid string number is contained."); return; } var inputs = numberList.Select(x => new double[] { x[0], x[1] }).ToArray(); var outputs = numberList.Select(x => x[2]).ToArray(); // LS방식으로는 잘 안되는것 같다. ...... 어코드에서는 다른 알고리즘은 지원을 안하기 때문에, 일단 테스트를 해보고, 잘 맞는 알고리즘을 c#으로 구현을 해야 될 것 같다. var ols = new OrdinaryLeastSquares() { UseIntercept = true }; MultipleLinearRegression regression = ols.Learn(inputs, outputs); double[] predicted = regression.Transform(inputs); double error = new SquareLoss(outputs).Loss(predicted); var testdata = GridZ(300, 15); var zdata = regression.Transform(testdata); var z2ddata = zdata.Reshape(300, 300); var jagged = z2ddata.ToJagged(); StringBuilder sb = new StringBuilder(); foreach (double[] item in jagged) { foreach (var el in item) { sb.Append(el.ToString()); sb.Append(","); } sb.Append(Environment.NewLine); } File.WriteAllText(@"E:\Temp\haha2.csv", sb.ToString()); Console.WriteLine("done"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
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.Coefficients[0]; // a = 0 double b = regression.Coefficients[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); #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)); double r = regression.CoefficientOfDetermination(inputs, outputs); Assert.AreEqual(1.0, r); }
public void learn_test1() { #region doc_learn // The multivariate linear regression is a generalization of // the multiple linear regression. In the multivariate linear // regression, not only the input variables are multivariate, // but also are the output dependent variables. // In the following example, we will perform a regression of // a 2-dimensional output variable over a 3-dimensional input // variable. double[][] inputs = { // variables: x1 x2 x3 new double[] { 1, 1, 1 }, // input sample 1 new double[] { 2, 1, 1 }, // input sample 2 new double[] { 3, 1, 1 }, // input sample 3 }; double[][] outputs = { // variables: y1 y2 new double[] { 2, 3 }, // corresponding output to sample 1 new double[] { 4, 6 }, // corresponding output to sample 2 new double[] { 6, 9 }, // corresponding output to sample 3 }; // With a quick eye inspection, it is possible to see that // the first output variable y1 is always the double of the // first input variable. The second output variable y2 is // always the triple of the first input variable. The other // input variables are unused. Nevertheless, we will fit a // multivariate regression model and confirm the validity // of our impressions: // Use Ordinary Least Squares to create the regression OrdinaryLeastSquares ols = new OrdinaryLeastSquares(); // Now, compute the multivariate linear regression: MultivariateLinearRegression regression = ols.Learn(inputs, outputs); // We can obtain predictions using double[][] predictions = regression.Transform(inputs); // The prediction error is double error = new SquareLoss(outputs).Loss(predictions); // 0 // At this point, the regression error will be 0 (the fit was // perfect). The regression coefficients for the first input // and first output variables will be 2. The coefficient for // the first input and second output variables will be 3. All // others will be 0. // // regression.Coefficients should be the matrix given by // // double[,] coefficients = { // { 2, 3 }, // { 0, 0 }, // { 0, 0 }, // }; // // We can also check the r-squared coefficients of determination: double[] r2 = regression.CoefficientOfDetermination(inputs, outputs); #endregion // The first input variable coefficients will be 2 and 3: Assert.AreEqual(2, regression.Coefficients[0, 0], 1e-10); Assert.AreEqual(3, regression.Coefficients[0, 1], 1e-10); // And all other coefficients will be 0: Assert.AreEqual(0, regression.Coefficients[1, 0], 1e-10); Assert.AreEqual(0, regression.Coefficients[1, 1], 1e-10); Assert.AreEqual(0, regression.Coefficients[2, 0], 1e-10); Assert.AreEqual(0, regression.Coefficients[2, 1], 1e-10); Assert.AreEqual(3, regression.NumberOfInputs); Assert.AreEqual(2, regression.NumberOfOutputs); // Which should be one for both output variables: Assert.AreEqual(1, r2[0]); Assert.AreEqual(1, r2[1]); foreach (var e in regression.Coefficients) Assert.IsFalse(double.IsNaN(e)); Assert.AreEqual(0, error, 1e-10); Assert.IsFalse(double.IsNaN(error)); }