依赖 lib:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-math3</artifactId> <version>3.1.1</version> </dependency>
两个数组就计算其相关系数:
PearsonsCorrelation t=new PearsonsCorrelation();
RealMatrix matrix=new Array2DRowRealMatrix(3,2);
RealVector vector=new ArrayRealVector(2);
vector.setEntry(0,1.0);
vector.setEntry(1,2.0);
RealVector vector1=new ArrayRealVector(2);
vector1.setEntry(0,20);
vector1.setEntry(1,10);
RealVector vector2=new ArrayRealVector(2);
vector2.setEntry(0,100);
vector2.setEntry(1,200);
matrix.setRowVector(0, vector);
matrix.setRowVector(1, vector1);
matrix.setRowVector(2, vector2);
double p=t.correlation(vector.toArray(),vector1.toArray());
System.out.println(p);
本文介绍如何使用Apache Commons Math库中的PearsonsCorrelation类来计算两组数据的相关系数。通过构造具体的例子,展示了如何创建RealMatrix和RealVector,并利用它们计算两个数组之间的Pearson相关系数。
1万+

被折叠的 条评论
为什么被折叠?



