Apache Simple Linear Regression In Java using Math Library

huangapple go评论77阅读模式
英文:

Apache Simple Linear Regression In Java using Math Library

问题

I am quite a newbie in using the apache library for a simple linear regress.
Task: I want to calculate the slope.

我在使用Apache库进行简单线性回归方面还很新手。
任务:我想计算斜率。

I have two linear lists, i.e., x-list and y-list. Y list values are the series of number. I am populating x-list by fetching value from a hashmap.

我有两个线性列表,即x列表和y列表。Y列表的值是一系列数字。我通过从哈希映射中获取值来填充x列表。

However, when I am trying to apply simpleRegression utility of apache library I am facing below difficulty:

然而,当我尝试使用Apache库的simpleRegression工具时,我遇到了以下困难:

Here is my code:
这是我的代码:

while(i < segmentI) {
    xList.add(Double.parseDouble(timeStamp.get(i)));
    yList.add(Double.parseDouble("1"));
    i++;
}
for(int m=0; i < segmentI; i++) {
    simpleRegression.addData(new double[][]{
        {xList.get(m),yList.get(m)}
    });
}

Doubt: Is there any way can I create new double[][] before passing it to simpleRegression.
疑问:在将其传递给simpleRegression之前是否有任何方法可以创建新的double[][]。

Also, new double[][]-it is a matrix then what would be the value of [0,1], [0,2]... so on because we don't have anything like that in single ArrayList.
另外,new double[][] - 它是一个矩阵,那么[0,1],[0,2]...等等的值将是什么,因为在单个ArrayList中我们没有这样的内容。

Anything in this regard will be helpful.
在这方面的任何信息都将很有帮助。

英文:

I am quite a newbie in using the apache library for a simple linear regress.
Task: I want to calculate the slope.

I have two linear lists, i.e., x-list and y-list. Y list values are the series of number. I am populating x-list by fetching value from a hashmap.

However, when I am trying to apply simpleRegression utility of apache library I am facing below difficulty:

Here is my code:

while(i&lt; segmentI)
                {
                    xList.add(Double.parseDouble(timeStamp.get(i)));
                    yList.add(Double.parseDouble(&quot;1&quot;));
                    i++;
                }
                for(int m=0; i&lt; segmentI; i++)
                {
                        simpleRegression.addData(new double[][]{
                                {xList.get(m),yList.get(m)}
                        });

                }

Doubt: Is there any way can I create new double[][] before passing it to simpleRegression.
Also, new double[][]-it is a matrix then what would be the value of [0,1], [0,2]... so on because we don't have anything like that in single ArrayList.

Anything in this regard will be helpful.

答案1

得分: 1

是的。例如:

double[][] data = new double[1][2];

for(int m=0; m < segmentI; m++)
{
    data[0][0] = xList.get(m);
    data[0][1] = yList.get(m);
    simpleRegression.addData(data);
}

你知道还有一个可以在没有数组的情况下调用的方法吗?

simpleRegression.addData(xList.get(m), yList.get(m));
英文:

> Is there any way can I create new double[][] before passing it to simpleRegression

Yes. For example

            double[][] data = new data[1][2];

            for(int m=0; m &lt; segmentI; m++)
            {
                    data[0][0] = xList.get(m);
                    data[0][1] = yList.get(m);
                    simpleRegression.addData(data);
            }

You know there's also a method that you can call without an array?

simpleRegression.addData(xList.get(m), yList.get(m));

答案2

得分: 0

这个部分的中文翻译是:
这些更改后的代码如下:
double[][] pqr={ArrayUtils.toPrimitive(yList.toArray(new Double[0])),ArrayUtils.toPrimitive(xList.toArray(new Double[0]))};
simpleRegression.addData(pqr);

英文:

This worked with below changes:
double[][] pqr={ArrayUtils.toPrimitive(yList.toArray(new Double[0])),ArrayUtils.toPrimitive(xList.toArray(new Double[0]))};
simpleRegression.addData(pqr);

huangapple
  • 本文由 发表于 2020年8月2日 07:46:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63211079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定