您好我想问一下2d数组中的比较问题。 我跟随2d数组,每行有双参数。 我想比较前5行(即:期间0)和secont 5行(即:Period1) 我想比较一下 比较将逐行作为1d数组我需要两个1 d数组来相互比较..
示例;
期间0(第0行)与期间1(第0行,第1行,第2行,第3行,第4行)
第0期第1行与第1期(第0行,第1行,第2行,第3行,第4行)
第0期第2行与第1期(第0行,第1行,第2行,第3行,第4行)
第0期第3行与期间1(第0行,第1行,第2行,第3行,第4行)
第0期第4行与期间1(第0行,第1行,第2行,第3行,第4行)
然后
第1期第0行对第2期(第0行,第1行,第2行,第3行,第4行)
第1期第1行与第2期(第0行,第1行,第2行,第3行,第4行)
第1期第2行与第2期(第0行,第1行,第2行,第3行,第4行)
第1期第3行与第2期(第0行,第1行,第2行,第3行,第4行)
第1期第4行与第2期(第0行,第1行,第2行,第3行,第4行)
然后
第2期第0行对第3期(第0行,第1行,第2行,第3行,第4行)
第2期第1行与第3期(第0行,第1行,第2行,第3行,第4行)
第2期第2行与第3期(第0行,第1行,第2行,第3行,第4行)
第2期第3行与期间3(第0行,第1行,第2行,第3行,第4行)
第2期第4行与第3期(第0行,第1行,第2行,第3行,第4行)
我的目标是获取Period 0的第一行然后将其转换为1d数组,然后获取Period1的第一行,然后将其转换为1d数组,依此类推..
2d数组如下;
double [] [] myDistributions = new double [] [] {
row0 {0.15250886479593964,0.2610516793197853,0.11441768814194446,0.1265241345428162,0.3454976331995246},
row1 {0.14389124837314887,0.10513281153155449,0.1833462741873425,0.36788054102686596,0.1997491248810186},
Period0 row2 {0.1111207312911868,0.17499901413730568,0.2914581757577288,0.20433331657432438,0.2180887622394655},
row3 {0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
row4 {0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552},
row0 {0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
row1 {0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
Period1 row2 {0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
row3 {0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552},
row4 {0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175},
row0 {0.2311120677942418,0.07901250493611567,0.15795189397863776,0.24546018732208122,0.28646334596884354},
row1 {0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
Period2 row2 {0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
row3 {0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
row4 {0.11962680738039468,0.1590225091909952,0.24009305610431117,0.11189649648370673,0.36936113084052996},
row0 {0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175},
row1 {0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
Period3 row2 {0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
row3 {0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
row4 {0.3962773409768214,0.12002724792315751,0.15722439889764284,0.11854502459707339,0.20792598760527947}};
答案 0 :(得分:1)
现在你已经在你的一条评论中提到了你在做什么,我们可以 那种 了解你在这里想要完成的事情。坦率地说,不要以任何方式不尊重,你的解释很糟糕。 :P
我收集它的方式是,您尝试从两个特定的二维(2D)双类型数组中获取行,并从那些特定的双类型数据行中检索散度值。你当然没有透露这种类型,如果你有分歧,我将不得不假设它是Kullback–Leibler Divergence。无论哪种方式,您都可以使用我在下面提供的代码并对其进行修改,以调用您希望收集所需差异的任何Java方法。
为了执行这项任务,我们需要一些特定的方法,其中最重要的方法当然是为我们提供分歧的方法。我在下面提供的方法就是这样做的,它是不久前由大学的 Charles Sutton 编写的。 of Massachusetts Amherst,计算机科学系。该方法是" MALLET" (MAchine Learning for LanguagE Toolkit)。这是Mr. Sutton's Divergence method,它是OpenSource:
/**
* Returns the Kullback–Leibler (KL) Divergence, K(p1 || p2).
*
* The log is w.r.t. base 2. <p>
*
* *Note*: If any value in <tt>p2</tt> is <tt>0.0</tt> then the KL-divergence
* is <tt>infinite</tt>. Limin changes it to zero instead of infinite.
*
*/
public static double klDivergence(double[] p1, double[] p2) {
double log2 = Math.log(2);
double klDiv = 0.0;
for (int i = 0; i < p1.length; ++i) {
if (p1[i] == 0) { continue; }
if (p2[i] == 0.0) { continue; } // Limin
klDiv += p1[i] * Math.log( p1[i] / p2[i] );
}
return klDiv / log2; // moved this division out of the loop -DM
}
对于我们的下一个方法,我们需要一种方法来获取两个提供的2D双类型数组并从中提取每行数据,以便从这两个特定行中检索它们的差异。我在下面提供的方法( kldFromDoubleArrays())执行此操作。它是尽可能基本的,因此很容易遵循:
/**
* This method will take each row from the supplied 2D double type array1 and
* each row from the supplied 2D double type array2 and display the
* Kullback–Leibler Divergence for each of those rows of data processed.<br><br>
*
* Note: This method outputs its results into the Console Window.<br><br>
*
* Note: This method utilizes <b>Charles Sutton's</b> method named klDivergence() to
* acquire Kullback–Leibler Divergence values.<br><br>
*
* @param array1 (2D Double Type Array)<br>
*
* @param nameForArray1 (String) The string name to use for array1 for console
* display purposes.<br>
*
* @param array2 (2D Double Type Array)<br>
*
* @param nameForArray2 (String) The string name to use for array2 for console
* display purposes.
*/
private void kldFromDoubleArrays(double[][] array1, String nameForArray1,
double[][] array2, String nameForArray2) {
//Iterate through Rows of array1...
for (int i = 0; i < array1.length; i++) {
//Declare a 1D Array to hold current row from array1
double[] p0 = new double [array1[i].length];
//Iterate through Columns of current array1 Row...
for (int j = 0; j < array1[i].length; j++) {
//Place current array1 row into a 1D Array p0
p0[j] = array1[i][j];
//Iterate through Rows of array2...
for (int k = 0; k < array2.length; k++) {
//Declare a 1D Array to hold current row from array2
double[] p1 = new double[array2[k].length];
//Iterate through Columns of current array2 Row...
for (int l = 0; l < array2[k].length; l++) {
//Place current array2 row into a 1D Array p1
p1[l] = array2[k][l];
//Get the KL Divergence fpr p0 and p1 1D arrays
//and display it within the Console window
double kld = klDivergence(p0, p1);
//Display to Console
System.out.println("The Divergence between Row " + i +
" of " + nameForArray1 + " and Row " + k + " of " +
nameForArray2 + " is: --> " + kld);
}
}
}
}
}
您可以轻松修改此方法以完全满足您的需求。
从你原来的帖子开始,我假设你的2D数组与特定的时期相关,因此period0 [] [],period1 [] [],period2 [] [],最后是period3 [] [ ]。因此,四个2D双类型阵列通常被构造为:
double[][] period0 = {
{0.15250886479593964,0.2610516793197853,0.11441768814194446,0.1265241345428162,0.3454976331995246},
{0.14389124837314887,0.10513281153155449,0.1833462741873425,0.36788054102686596,0.1997491248810186},
{0.1111207312911868,0.17499901413730568,0.2914581757577288,0.20433331657432438,0.2180887622394655},
{0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
{0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552}
};
double[][] period1 = {
{0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
{0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
{0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
{0.18877271931210932,0.26521449714587747,0.13230019262559328,0.27631809895720494,0.13739449195931552},
{0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175}
};
double[][] period2 = {
{0.2311120677942418,0.07901250493611567,0.15795189397863776,0.24546018732208122,0.28646334596884354},
{0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
{0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
{0.3016290729137225,0.09902028076323677,0.17515717333485062,0.35620664945852193,0.06798682352968743},
{0.11962680738039468,0.1590225091909952,0.24009305610431117,0.11189649648370673,0.36936113084052996}
};
double[][] period3 = {
{0.17804585265772793,0.25651982583759037,0.3860867129515085,0.09453797159458521,0.08480963695844175},
{0.08823944830766299,0.26313933789756516,0.10406419933285384,0.3365866979847223,0.20797031647719286},
{0.1274517393962726,0.10526928843184565,0.35751329613481436,0.12240396832200726,0.2873617077151316},
{0.0948730457966342,0.19288720600625753,0.19471332499886804,0.18018001280247228,0.3373464103957629},
{0.3962773409768214,0.12002724792315751,0.15722439889764284,0.11854502459707339,0.20792598760527947}
};
现在我们需要做的就是调用我们的 kldFromDoubleArrays()方法来处理你想要的时期。在您的帖子中,您指定要处理的行是:
所以,知道这一点,我们将调用我们的方法三次:
//Create a Underline for Console window display.
String ul = String.join("", Collections.nCopies(100, "=")) + "\n";
//Period0 To Period1 Comparison:
kldFromDoubleArrays(period0, "Period 0", period1, "Period 1");
System.out.println(ul);
//Period1 To Period2 Comparison:
kldFromDoubleArrays(period1, "Period 1", period2, "Period 2");
System.out.println(ul);
//Period2 To Period3 Comparison:
kldFromDoubleArrays(period2, "Period 2", period3, "Period 3");
System.out.println(ul);
以下是一些示例输出:
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 1 of Period 1 is: --> -0.16008580289377392
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 2 of Period 1 is: --> 0.025341952815786797
The Divergence between Row 2 of Period 0 and Row 3 of Period 1 is: --> -0.08495427575998642
The Divergence between Row 2 of Period 0 and Row 3 of Period 1 is: --> -0.08495427575998642
The Divergence between Row 2 of Period 0 and Row 3 of Period 1 is: --> -0.08495427575998642
那就是把它包起来。我希望这对你有所帮助。祝你的项目好运。
答案 1 :(得分:0)
我找到了解决方案。
int numOfTopicsInEachPeriod=5;
double[][] myFirstRow=new double [numOfTopicsInEachPeriod][myDistributions[0].length];
double[][] mySecondRow=new double[numOfTopicsInEachPeriod][myDistributions[0].length];
int count =0;
int counter =0;
int temp=0;
for(int j =0;j<myDistributions.length-numOfTopicsInEachPeriod;j++){
int mod=j%numOfTopicsInEachPeriod;
int countOfTopics = j/numOfTopicsInEachPeriod;
count=0;
if(mod==0){
temp=1;
count=numOfTopicsInEachPeriod* countOfTopics;
}
if(temp==1&&mod!=0){count=numOfTopicsInEachPeriod* countOfTopics; }
for(int i=counter;i<numOfTopicsInEachPeriod;i++){
myFirstRow[i]=myDistributions[count];
mySecondRow[i]=myDistributions[count+5];
System.out.print("myFunction(Row["+j+"],Row ["+(count+5)+"]),"+"\t" );
count++;
}
System.out.println();
}