You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Oscar Bastidas <ob...@umn.edu> on 2020/01/08 20:12:01 UTC

Apache Commons Math Linear Regression Question

Hello,

Would someone please tell me how I am supposed to acquire the actual
numeric coefficients for a linear regression using the
PolynomialCurveFitter class?

Below is my code (it's supposed to be a simple example) and when I print
out the variable that stores the coefficients, ("coeff" in the below
example) what is printed to the command line is a strange code:
*[D@1610302.*

This happens regardless of the order of the polynomial I request.

Would someone please help me with this?  Thanks.

*********************** CODE BELOW ******************************

import java.util.*;
import org.apache.commons.math3.fitting.PolynomialCurveFitter;
import org.apache.commons.math3.fitting.*;

public class MathTester
{
        public static void main(String[] args) {
        // Testing linear regression
        final WeightedObservedPoints obs = new WeightedObservedPoints();

                // Acquiring data points
                obs.add(-1.00, 2.021170);
                obs.add(-0.99, 2.221135);
                obs.add(-0.98, 2.099852);

               // Instantiate 2nd degree polynomial fitting
               final PolynomialCurveFitter fitter =
PolynomialCurveFitter.create(2);

                // Retrieve fitted coefficients of the fitted data
                final double[] coeff = fitter.fit(obs.toList());

               // Printing out coefficients
               System.out.println("The coefficients are: " + coeff);

       } // END 'main'
} // END class

************************************************************

Oscar

Oscar Bastidas, Ph.D.
Postdoctoral Research Associate
University of Minnesota

Re: Apache Commons Math Linear Regression Question

Posted by Oscar Bastidas <ob...@umn.edu>.
Please disregard my previous question!  I figured out what I was missing.

Oscar

Oscar Bastidas, Ph.D.
Postdoctoral Research Associate
University of Minnesota

On Wed, Jan 8, 2020, 2:12 PM Oscar Bastidas <ob...@umn.edu> wrote:

> Hello,
>
> Would someone please tell me how I am supposed to acquire the actual
> numeric coefficients for a linear regression using the
> PolynomialCurveFitter class?
>
> Below is my code (it's supposed to be a simple example) and when I print
> out the variable that stores the coefficients, ("coeff" in the below
> example) what is printed to the command line is a strange code:
> *[D@1610302.*
>
> This happens regardless of the order of the polynomial I request.
>
> Would someone please help me with this?  Thanks.
>
> *********************** CODE BELOW ******************************
>
> import java.util.*;
> import org.apache.commons.math3.fitting.PolynomialCurveFitter;
> import org.apache.commons.math3.fitting.*;
>
> public class MathTester
> {
>         public static void main(String[] args) {
>         // Testing linear regression
>         final WeightedObservedPoints obs = new WeightedObservedPoints();
>
>                 // Acquiring data points
>                 obs.add(-1.00, 2.021170);
>                 obs.add(-0.99, 2.221135);
>                 obs.add(-0.98, 2.099852);
>
>                // Instantiate 2nd degree polynomial fitting
>                final PolynomialCurveFitter fitter =
> PolynomialCurveFitter.create(2);
>
>                 // Retrieve fitted coefficients of the fitted data
>                 final double[] coeff = fitter.fit(obs.toList());
>
>                // Printing out coefficients
>                System.out.println("The coefficients are: " + coeff);
>
>        } // END 'main'
> } // END class
>
> ************************************************************
>
> Oscar
>
> Oscar Bastidas, Ph.D.
> Postdoctoral Research Associate
> University of Minnesota
>

RE: Apache Commons Math Linear Regression Question

Posted by Jo...@wellsfargo.com.INVALID.
Just wrap with Arrays.toString():

System.out.println("The coefficients are: " + Arrays.toString(coeff));

You'll get this:

The coefficients are: [-1568.1599299988338, -3176.4210999976494, -1606.2399999988156]



> -----Original Message-----
> From: Oscar Bastidas <ob...@umn.edu>
> Sent: Wednesday, January 08, 2020 2:12 PM
> To: user@commons.apache.org
> Subject: Apache Commons Math Linear Regression Question
> 
> Hello,
> 
> Would someone please tell me how I am supposed to acquire the actual
> numeric coefficients for a linear regression using the PolynomialCurveFitter
> class?
> 
> Below is my code (it's supposed to be a simple example) and when I print out
> the variable that stores the coefficients, ("coeff" in the below
> example) what is printed to the command line is a strange code:
> *[D@1610302.*
> 
> This happens regardless of the order of the polynomial I request.
> 
> Would someone please help me with this?  Thanks.
> 
> *********************** CODE BELOW
> ******************************
> 
> import java.util.*;
> import org.apache.commons.math3.fitting.PolynomialCurveFitter;
> import org.apache.commons.math3.fitting.*;
> 
> public class MathTester
> {
>         public static void main(String[] args) {
>         // Testing linear regression
>         final WeightedObservedPoints obs = new WeightedObservedPoints();
> 
>                 // Acquiring data points
>                 obs.add(-1.00, 2.021170);
>                 obs.add(-0.99, 2.221135);
>                 obs.add(-0.98, 2.099852);
> 
>                // Instantiate 2nd degree polynomial fitting
>                final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(2);
> 
>                 // Retrieve fitted coefficients of the fitted data
>                 final double[] coeff = fitter.fit(obs.toList());
> 
>                // Printing out coefficients
>                System.out.println("The coefficients are: " + coeff);
> 
>        } // END 'main'
> } // END class
> 
> **********************************************************
> **
> 
> Oscar
> 
> Oscar Bastidas, Ph.D.
> Postdoctoral Research Associate
> University of Minnesota

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org