如何合并bittrings JAVA

时间:2016-07-18 10:08:34

标签: java arraylist merge

我有ArrayList<Arraylist>     集群:[[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 1], [1, 1, 1, 0]]

对于每个bitStrings(内部arrayList),计算熵并产生如下:

inner index #0 : [1, 0, 0, 0] = 0.811
inner index #1 : [0, 0, 0, 0] = 0
inner index #2 : [0, 0, 0, 0] = 0
inner index #3 : [0, 0, 1, 1] = 1
inner index #4 : [1, 1, 1, 0] = 0.811

熵的计算如下:
内部索引#0

0的概率是3/4 = 0.75,概率1是1/4 = 0.25 然后,entropy = - ((0.75)(log(0.75)/ log(2))+(0.25)(log(0.25)/ log(2)))= 0.811

然后,在获得所有位的熵之后,我想将两者合并如下:

让我们说#0和#1所以这些位将成为:

**Before merge**
inner index #0 : [1,0,0,0]  
inner index #1 : [0,0,0,0]  

**After merge**  
inner index #0 and #1 : [ [1,0], [0,0], [0,0], [0,0] ]

然后在合并之后,我会发现存在[0,0][0,1][1,0][1,0](可能的排列)的概率再次计算熵。所以在上面的例子中:

P([0,0]) = 3/4 = 0.75 (because there are 3 [0,0] in outer array of 4)  
P([0,1]) = 0/4 = 0  
P([1,0]) = 1/4 = 0.25 (because there are 1 [1,0] in outer array of 4)    
P([1,1]) = 0/4 = 0

然后应用相同的计算熵的方法。

在#0和#1合并之后,让我们说我想将#0和#1与#3合并,以便它产生如下:

**Before merge**  
#0 and #1 : [ [1,0], [0,0], [0,0], [0,0] ]  
#3 : [0, 0, 1, 1]  

**After merge**  
#0, #1, #3 : [ [1,0,0], [0,0,0], [0,0,1], [0,0,1] ]  

然后计算所有可能排列[0,0,0],......,[1,1,1]的概率并重复计算熵。

因此,我如何在java中对其进行编码以灵活的方式查找概率,因为比特可以更长,并且用于查找概率的置换模式可能更长?

1 个答案:

答案 0 :(得分:0)

这是我尝试过的代码,但它只适用于第一个代码而不确定如何合并它并计算合并时的概率:

public static double calculateEntropy(ArrayList<Integer> bitSet){
     double entropy = 0.0;
     double P_C0,P_C1;
     int C0_count = 0, C1_count = 0;
     int bitSize = bitSet.size();

    System.out.println("bitSet to calculate: " + bitSet);
    System.out.println("bitSize = " + bitSize);

    for (int k = 0 ; k < bitSize; k++){
        if (bitSet.get(k) == 0)
            C0_count++;
        else if (bitSet.get(k) == 1)
            C1_count++;
    }

    System.out.println("CO: " + C0_count);
    System.out.println("C1: " + C1_count);

    P_C0 = (double)C0_count/bitSize;
    P_C1 = (double)C1_count/bitSize;

    System.out.println("P_C0: " + P_C0);
    System.out.println("P_C1: " + P_C1);

    entropy = -( (P_C0*(Math.log(P_C0)/Math.log(2))) + (P_C1*(Math.log(P_C1)/Math.log(2))) );

    if(Double.isNaN(entropy)){
        entropy = (double)0.00;

    }

    return entropy;
}