簡體   English   中英

kmeans聚類算法中的多維ArrayList

[英]MultiDimensional ArrayList in kmeans clustering algorithm

我正在嘗試為Java中的某個音樂推薦系統實現kmeans算法。
我已經生成了2個數組,即playsFinal[] (數據集中所有用戶的藝術家總播放次數)和artFinal[] (整個數據集中的唯一藝術家)。 每個artFinal[i]的播放量是playsFinal[i] 對於k,我選擇了kclusters=Math.sqrt(playsFinal.length)/2
我有一個數組clusters[kclusters][playsFinal.length]並且每0<i<kclusters的第一個位置clusters[i][0]都填充有一定的值,這基本上是kmeans算法中的初始均值。

int j = 0;
for (int i = 0; i < n && j < kclusters; i += kclusters) {
    clusters[j][0] = weighty[j];//initial means
    System.out.println(clusters[j][0]);
    j++;
}  

在這里, weight[]是給每個藝術家的一定分數。
現在,在下面的函數中,我返回索引,即plays[i]應該添加到哪個集群。

public static int smallestdistance(double a, double[][] clusters) {
    a = (double) a;
    double smallest = 0;
    double d[] = new double[kclusters];
    for (int i = 0; i < kclusters; i++) {
        d[i] = a - clusters[i][0];

    }
    int index = -1;
    double d1 = Double.POSITIVE_INFINITY;
    for (int i = 0; i < d.length; i++)
        if (d[i] < d1) {
            d1 = d[i];
            index = i;
        }
    return index;
}

如果不是很明顯,我會在playsFinal[i]和每個clusters[j][0]的初始元素之間找到最小的距離,而最小的則是返回其索引(kfound)。 現在在clusters[kfound][]的索引處,我想添加playsFinal[i]但這是我playsFinal[i] 我不能像ArrayList中那樣使用.add()函數。 而且我猜想使用ArrayList會更好。 我瀏覽了ArrayList上的大多數文章,但沒有發現任何可以幫助我的東西。
如何使用多維ArrayList實現此功能? 提前致謝。 我的代碼如下:

int j = 0;
for (int i = 0; i < n && j < kclusters; i += kclusters) {
    clusters[j][0] = weighty[j];//initial means
    System.out.println(clusters[j][0]);
    j++;
}


    double[] weighty = new double[artFinal.length];
    for (int i = 0; i < artFinal.length; i++) {
        weighty[i] = (playsFinal[i] * 10000 / playsFinal.length);

    }
    n = playsFinal.length;

    kclusters = (int) (Math.sqrt(n) / 2);
    double[][] clusters = new double[kclusters][playsFinal.length];

    int j = 0;
    for (int i = 0; i < n && j < kclusters; i += kclusters) {
        clusters[j][0] = weighty[j];//initial means
        System.out.println(clusters[j][0]);
        j++;
    }

    int kfound;

    for (int i = 0; i < playsFinal.length; i++) {
        kfound = smallestdistance(playsFinal[i], clusters);
        //HERE IS WHERE I AM STUCK. I want to add playsFinal[i] to the corresponding clusters[kfound][]

    }

}


public static int smallestdistance(double a, double[][] clusters) {
    a = (double) a;
    double smallest = 0;
    double d[] = new double[kclusters];
    for (int i = 0; i < kclusters; i++) {
        d[i] = a - clusters[i][0];

    }
    int index = -1;
    double d1 = Double.POSITIVE_INFINITY;
    for (int i = 0; i < d.length; i++)
        if (d[i] < d1) {
            d1 = d[i];
            index = i;
        }
    return index;
}

Java的“多維數組”實際上只是其元素本身(對數組的引用)的數組。 ArrayList等效項是創建一個包含其他列表的列表:

List<List<Foo>> l = new ArrayList<>(); //create outer ArrayList
for (int i = 0; i < 10; i++) //create 10 inner ArrayLists
    l.add(new ArrayList<Foo>());
l.get(5).add(foo1); //add an element to the sixth inner list
l.get(5).set(0, foo2); //set that element to a different value

與數組不同,列表創建為空(與任何列表一樣),而不是使用指定數量的插槽。 如果要將它們視為多維數組的直接替代品,則必須手動填寫。 這意味着您的內部列表可以具有不同的長度。 (通過僅指定外部尺寸( int[][] x = new int[10][]; ),然后手動初始化插槽( for (int i = 0; i < x.length; ++i) x[i] = new int[i];對於“三角形”數組),但是用於多維數組創建的特殊語法強烈地使大多數程序員傾向於只考慮“矩形”數組。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM