2

より簡単に2つのことを行うために利用可能なライブラリが必要だと思います.A)倍精度の場合は配列のモードを見つけ、B)特定の周波数に達するまで精度を適切に低下させます。

したがって、次のような配列を想像してください。

double[] a = {1.12, 1.15, 1.13, 2.0, 3.4, 3.44, 4.1, 4.2, 4.3, 4.4};

周波数 3 を探していた場合、小数点以下 2 桁から 1 桁になり、最終的にモードとして 1.1 が返されます。周波数要件が 4 の場合、モードとして 4 が返されます。

希望どおりに動作し、期待どおりの結果を返す一連のコードがありますが、これを達成するためのより効率的な方法、または同じことを行うのに役立つ既存のライブラリが必要だと感じています。添付されているのは私のコードです。私が取るべきだったさまざまなアプローチについての考え/コメントに興味があります....精度がどれだけ低下するかを制限するために、反復をリストしています。

public static double findMode(double[] r, int frequencyReq)
{
    double mode = 0d;
    int frequency = 0;
    int iterations = 4;

    HashMap<Double, BigDecimal> counter = new HashMap<Double, BigDecimal>();

    while(frequency < frequencyReq && iterations > 0){
        String roundFormatString = "#.";
        for(int j=0; j<iterations; j++){
            roundFormatString += "#";
        }
        DecimalFormat roundFormat = new DecimalFormat(roundFormatString);
        for(int i=0; i<r.length; i++){

            double element = Double.valueOf(roundFormat.format(r[i]));

            if(!counter.containsKey(element))
                counter.put(element, new BigDecimal(0));

            counter.put(element,counter.get(element).add(new BigDecimal(1)));
        }

        for(Double key : counter.keySet()){

            if(counter.get(key).compareTo(new BigDecimal(frequency))>0){
                mode = key;
                frequency = counter.get(key).intValue();
                log.debug("key: " + key + " Count: " + counter.get(key));
            }
        }
        iterations--;
    }

    return mode;
}

編集

frequencyPaulo のコメントによると、質問を言い換える別の方法: 目標は、近隣の半径ができるだけ小さい、近隣に少なくとも配列要素がある数値を見つけることです。

4

2 に答える 2

1

ここで、再定式化された質問の解決策:

目標は、近傍内に少なくともfrequency配列要素があり、近傍の半径ができるだけ小さい数値を見つけることです。

(入力配列の1.15との順序を自由に切り替えることができました。)1.13

基本的な考え方は、入力が既にソートされており (つまり、隣接する要素が連続している)、近隣に必要な要素の数がわかっているということです。したがって、この配列を 1 回ループして、左側の要素とさらに右側の要素要素の間の距離を測定しfrequencyます。それらの間にfrequency要素があるため、これが近隣を形成します。次に、そのような最小距離を取ります。(私のメソッドには結果を返す複雑な方法があります。もっとうまくやりたいと思うかもしれません。)

これは元の質問と完全に同等ではありません (固定された数字のステップでは機能しません) が、おそらくこれはあなたが本当に望んでいるものです:-)

ただし、結果をフォーマットするより良い方法を見つける必要があります。

package de.fencing_game.paul.examples;

import java.util.Arrays;

/**
 * searching of dense points in a distribution.
 *
 * Inspired by http://stackoverflow.com/questions/5329628/finding-a-mode-with-decreasing-precision.
 */
public class InpreciseMode {

    /** our input data, should be sorted ascending. */
    private double[] data;

    public InpreciseMode(double ... data) {
        this.data = data;
    }


    /**
     * searchs the smallest neighbourhood (by diameter) which
     * contains at least minSize elements.
     *
     * @return an array of two arrays:
     *     {   { the middle point of the neighborhood,
     *           the diameter of the neighborhood  },
     *        all the elements of the neigborhood }
     *
     * TODO: better return an object of a class encapsuling these.
     */
    public double[][] findSmallNeighbourhood(int minSize) {
        int currentLeft = -1;
        int currentRight = -1;
        double currentMinDiameter = Double.POSITIVE_INFINITY;

        for(int i = 0; i + minSize-1 < data.length; i++) {
            double diameter = data[i+minSize-1] - data[i];
            if(diameter < currentMinDiameter) {
                currentMinDiameter = diameter;
                currentLeft = i;
                currentRight = i + minSize-1;
            }
        }
        return
            new double[][] {
            { 
                (data[currentRight] + data[currentLeft])/2.0,
                currentMinDiameter
            },
            Arrays.copyOfRange(data, currentLeft, currentRight+1)
        };
    }

    public void printSmallNeighbourhoods() {
        for(int frequency = 2; frequency <= data.length; frequency++) {
            double[][] found = findSmallNeighbourhood(frequency);

            System.out.printf("There are %d elements in %f radius "+
                              "around %f:%n     %s.%n",
                              frequency, found[0][1]/2, found[0][0],
                              Arrays.toString(found[1]));
        }
    }


    public static void main(String[] params) {
        InpreciseMode m =
            new InpreciseMode(1.12, 1.13, 1.15, 2.0, 3.4, 3.44, 4.1,
                              4.2, 4.3, 4.4);
        m.printSmallNeighbourhoods();
    }

}

出力は

There are 2 elements in 0,005000 radius around 1,125000:
     [1.12, 1.13].
There are 3 elements in 0,015000 radius around 1,135000:
     [1.12, 1.13, 1.15].
There are 4 elements in 0,150000 radius around 4,250000:
     [4.1, 4.2, 4.3, 4.4].
There are 5 elements in 0,450000 radius around 3,850000:
     [3.4, 3.44, 4.1, 4.2, 4.3].
There are 6 elements in 0,500000 radius around 3,900000:
     [3.4, 3.44, 4.1, 4.2, 4.3, 4.4].
There are 7 elements in 1,200000 radius around 3,200000:
     [2.0, 3.4, 3.44, 4.1, 4.2, 4.3, 4.4].
There are 8 elements in 1,540000 radius around 2,660000:
     [1.12, 1.13, 1.15, 2.0, 3.4, 3.44, 4.1, 4.2].
There are 9 elements in 1,590000 radius around 2,710000:
     [1.12, 1.13, 1.15, 2.0, 3.4, 3.44, 4.1, 4.2, 4.3].
There are 10 elements in 1,640000 radius around 2,760000:
     [1.12, 1.13, 1.15, 2.0, 3.4, 3.44, 4.1, 4.2, 4.3, 4.4].
于 2011-03-16T21:32:56.300 に答える
1

あなたのコードには何も問題はないと思いますし、これほど具体的なことを行うライブラリが見つかるとは思えません。しかし、Java コレクションを再利用する OOP アプローチを使用してこの問題にアプローチするアイデアが必要な場合は、別のアプローチを使用します。

  • 小数点以下の桁数が異なる数値を表すクラスを作成します。VariableDecimal(double d,int ndecimals)コンストラクタのようなものがあります。
  • そのクラスでは、オブジェクト メソッドequalshashCode. の実装は、値と小数点以下の桁数を考慮して、 のequals2 つのインスタンスが同じかどうかをテストします。単純に整数にキャストして返すことができます。VariableDecimaldhashCoded*exp(10,ndecimals)

HashMapsオブジェクトを再利用できるようにロジックで使用します。

HashMap<VariableDecimal, AtomicInteger> counters = new HashMap<VariableDecimal, AtomicInteger>();
for (double d : a) {
     VariableDecimal vd = new VariableDecimal(d,ndecimals);
     if (counters.get(vd)!=null)
         counters.set(vd,new AtomicInteger(0));
     counters.get(vd).incrementAndGet();

}
/* at the end of this loop counters should hold a map with frequencies of 
   each double for the selected precision so that you can simply traverse and 
   get the max */

このコードでは、小数点以下の桁数を減らす反復処理を示していませんが、これは些細なことです。

于 2011-03-16T18:51:16.920 に答える