Simple Regression 用于使用 Apache Maths 在 Java 中计算斜率。

go评论73阅读模式

Simple Regression for calculating slope using apache maths in Java

问题

XLIST [9.570915222167969，7.601962566375732，6.5179524421691895，5.71270227432251，5.095747947692871，4.610823631286621，4.221418380737305，3.902977705001831，3.6384739875793457，3.4157819747924805]
YList [10.0，11.0，12.0，13.0，14.0，15.0，16.0，17.0，18.0，19.0]

``````double[][] pqr={ArrayUtils.toPrimitive(xList.toArray(new Double[0])),ArrayUtils.toPrimitive(yList.toArray(new Double[0]))};
System.out.println(" The slope" + simpleRegression.getSlope());
``````

I am trying to calculate the slope using simple regression.
Here is my two Arrays:

``````XLIST  [9.570915222167969 , 7.601962566375732 , 6.5179524421691895 , 5.71270227432251 , 5.095747947692871 , 4.610823631286621 , 4.221418380737305 , 3.902977705001831 , 3.6384739875793457 , 3.4157819747924805]
YList [10.0 , 11.0 , 12.0 , 13.0 , 14.0 , 15.0 , 16.0 , 17.0 , 18.0 , 19.0]
``````

However, the slope is `7.9192681940221545` using below:

``````double[][] pqr={ArrayUtils.toPrimitive(xList.toArray(new Double[0])),ArrayUtils.toPrimitive(yList.toArray(new Double[0]))};
System.out.println(&quot; The slope&quot; + simpleRegression.getSlope());
``````

However, I am able to see a discrepancy in the excel slope, i.e., -0.6132.

I am not sure where am I doing the mistake.

Any info in this regard will be helpful.

答案1

`addData`方法假定你传递的是一个包含x-y对的数组。而实际上，你传递的是两个分开的x值和y值数组。你可以创建一个正确形状的数组，或者使用其他可用的方法传递数据。例如：

``````for (int i=0; i<xList.size(); i++)
``````

You're passing the data wrong to the `addData(double[][])` method.

The `addData` method assumes that you're passing in an array of x-y pairs. Instead your passing in x values and y values in two separate arrays. You can either create an array of the right shape, or pass the data in with the other methods available. For example

``````for (int i=0; i&lt;xList.size(); i++)

</details>

``````

• 本文由 发表于 2020年8月3日 06:51:46
• 转载请务必保留本文链接：https://go.coder-hub.com/63221760.html
• java
• linear-regression

go 48

go 53

go 54

go 45