无法从main中的方法返回一个数组

时间:2018-04-21 16:32:42

标签: java arrays k-means

我是Java的初学者。这是我创建KMeans算法的大学工作。一个人在SAMPLES中写入数字,在NUM_CLUSTERS中写入数量。算法给出了集群中心Points

我需要从这个方法返回一个数组,但我不能:

public static int[][] getCentroid_arr(int SAMPLES[],int NUM_CLUSTERS,int centroid_arr[][])

究竟是什么阵列? - 它显示在控制台中:

Points
1, 2, 7, 22, 44,

返回数组是必要的,因为我想用它来检查JUnit测试。 而不是return centroid_arr必须是这个排序的数组:  printArray(arr_sort_asc(sorted_distTocentroid_arr))

在这个问题上花了大约3天后,我仍然不知道如何解决问题。我只能打印出来。

我要感谢任何帮助。

    package kmeans;

import java.util.*;
import java.util.Map.Entry;

public class KM_don_touch {

    private static final int NUM_CLUSTERS = 5;    // Total clusters.

    private static final int SAMPLES[] = new int[]{1, 2, 3, 6, 7, 9, 11, 22, 44, 50};

    public static void main(String[] args) {
        System.out.println("data size is: " + SAMPLES.length);
        System.out.println("number of clusters is: " + NUM_CLUSTERS);
        System.out.print("dataset is: ");
        printArray(SAMPLES);
        System.out.println();

        int maxDiffData_arr[]=new int[NUM_CLUSTERS];        

        Kmeans_test3 get_prepare_data = new Kmeans_test3();
        maxDiffData_arr=get_prepare_data.prepare_data(SAMPLES);

        int centroid_arr[][]=new int[2][NUM_CLUSTERS];
        for (int i=0; i<NUM_CLUSTERS; i++) {
            centroid_arr[0][i] = 0;
            centroid_arr[1][i] = SAMPLES[maxDiffData_arr[i]];
        }

        System.out.println();
        System.out.print("centroid_arr 0 is: ");
        printArray(centroid_arr[0]);

        System.out.println();

        System.out.print("centroid_arr 1 is: ");
        printArray(centroid_arr[1]);

        System.out.println();

        getCentroid_arr(SAMPLES, NUM_CLUSTERS, centroid_arr);       
    }   

    public static void firstDiffSort(int[] difference){
        int tmp[]=new int[difference.length];
        tmp = arr_sort_desc(difference);
        for (int i=0; i<difference.length; i++){
        }
    }

    public static int[] arr_sort_desc(int[] arr) {
        int tmp;    
        for (int k = 0; k < arr.length; k++) {
            for (int j = k + 1; j < arr.length; j++) {
                if (arr[k] < arr[j]) { 
                tmp = arr[k];       
                arr[k] = arr[j];        
                arr[j] = tmp;
                }
            }
        } 
        return arr;
    }

    public static int[] arr_sort_asc(int[] arr) {
        int tmp;    
        for (int k = 0; k < arr.length; k++) {
            for (int j = k + 1; j < arr.length; j++) {
                if (arr[k] > arr[j]) { 
                tmp = arr[k];       
                arr[k] = arr[j];        
                arr[j] = tmp;
                }
            }
        } 
        return arr;
    }

    public static int getMinValue(int[] numbers){
          int minValue = numbers[0];
          for(int i=1; i<numbers.length; i++){
            if(numbers[i] < minValue){
                minValue = numbers[i];
            }
          }
          return minValue;
    }

    public static int FindSmallest (int [] arr1){
           int index = 0;
           int min = arr1[index];
           for (int i=1; i<arr1.length; i++){
               if (arr1[i] < min ){
                   min = arr1[i];
                   index = i;
               }
           }
           return index;
    }

    public static int[][] getCentroid_arr(int SAMPLES[],int NUM_CLUSTERS,int centroid_arr[][]){
            int distance[][]=new int[NUM_CLUSTERS][SAMPLES.length];
            int cluster[]=new int[SAMPLES.length];
            int clusternodecount[]=new int[NUM_CLUSTERS];
            int storedData[][]=new int[1][NUM_CLUSTERS];
            int clusterelements[][]=new int[NUM_CLUSTERS][SAMPLES.length];          

            centroid_arr[0] = centroid_arr[1];
            centroid_arr[1]=new int[NUM_CLUSTERS];
            for (int i=0; i<NUM_CLUSTERS; i++) {
                centroid_arr[1][i] = 0;
            }               

            System.out.println("========== Starting to get new centroid_arr =========");

            //сложность алгоритма С2
            for(int i=0;i<NUM_CLUSTERS;i++){
                for(int j=0;j<SAMPLES.length;j++){
                    distance[i][j]=Math.abs(SAMPLES[j]-centroid_arr[0][i]);
                    System.out.print(distance[i][j]+" ,");
                }
                System.out.println();
            }           

            System.out.println("========== Just ranspose =========");
            int smallerDistance=0;
            int[][] numbers = new int[SAMPLES.length][NUM_CLUSTERS];

            for(int i=0;i<SAMPLES.length;i++){
                for(int j=0; j<NUM_CLUSTERS; j++){
                    numbers[i][j]=distance[j][i];
                    System.out.print(numbers[i][j]+", ");
                }
                System.out.println();
                smallerDistance = FindSmallest(numbers[i]);
                centroid_arr[1][smallerDistance] = centroid_arr[1][smallerDistance]+SAMPLES[i];
                clusternodecount[smallerDistance]=clusternodecount[smallerDistance]+1;
                cluster[i]=smallerDistance;             
            }

            System.out.println("New clusters are ");

            for (int i = 0; i < NUM_CLUSTERS; i++) {
                System.out.print("C" + (i + 1) + ": ");
                for (int j = 0; j < SAMPLES.length; j++) {
                    if (cluster[j] == i)
                        System.out.print(SAMPLES[j] + ", ");
                }
                System.out.println();
            }
            System.out.println("======================================== ");

            System.out.println("New centroid_arr is ");

            for (int k = 0; k < NUM_CLUSTERS; k++) {
                centroid_arr[1][k] = centroid_arr[1][k] / clusternodecount[k];
                System.out.print(centroid_arr[1][k] + ", ");
            }
            System.out.println();

            boolean isAchived = true;
            for (int j = 0; j < NUM_CLUSTERS; j++) {
                if (isAchived && centroid_arr[0][j] == centroid_arr[1][j]) {
                    isAchived = true;
                    continue;
                }
                isAchived = false;
            }

            if (!isAchived) {
                getCentroid_arr(SAMPLES, NUM_CLUSTERS, centroid_arr);
            }

            if (isAchived) {
                System.out.println("======================================== ");
                System.out.println(" Final Cluster is ");
                for (int i = 0; i < NUM_CLUSTERS; i++) {
                    System.out.print("C" + (i + 1) + ": ");
                    for (int j = 0; j < SAMPLES.length; j++) {
                        if (cluster[j] == i)
                            System.out.print(SAMPLES[j] + ", ");
                    }
                    System.out.println();
                }
            for (int i=0; i<SAMPLES.length; i++){
                for (int j=0; j<NUM_CLUSTERS; j++){
                    if (j==cluster[i])
                        clusterelements[j][i]=SAMPLES[i];   
                }
            }

            int distTocentroid_arr[][]=new int[NUM_CLUSTERS][SAMPLES.length];   
            for (int i=0; i<NUM_CLUSTERS; i++){
                for (int j=0; j<SAMPLES.length; j++){
                    distTocentroid_arr[i][j]=Math.abs(centroid_arr[1][i]-clusterelements[i][j]);
                }
            }

            System.out.println("Points:");
            int sorted_distTocentroid_arr[] = new int[NUM_CLUSTERS];
            for (int i=0; i<NUM_CLUSTERS; i++){ 
                smallerDistance = FindSmallest(distTocentroid_arr[i]);
                sorted_distTocentroid_arr[i]=clusterelements[i][smallerDistance];
            }

            printArray(arr_sort_asc(sorted_distTocentroid_arr));        
            }
            return centroid_arr;
    }   

    public int[] prepare_data(int[] arr) {
        int sorted_arr[] = arr_sort_desc(SAMPLES);
        int maxDiff_arr[] = new int[SAMPLES.length];
        int sorted_diff[] = new int[SAMPLES.length];

        maxDiff_arr[0] = Math.abs(sorted_arr[0] - sorted_arr[1]);
        for (int i = 1; i < SAMPLES.length; i++) {
            maxDiff_arr[i] = Math.abs(sorted_arr[i] - sorted_arr[i - 1]);
        }

        Map<Integer, Integer> m1= new HashMap<Integer, Integer>();
        int k=0;
        for(int i:maxDiff_arr){
            m1.put(k++, i);
        }
        m1= sortByValues(m1);

        int i=0;
        for (int key: m1.keySet()){
             sorted_diff[i++]=key;
        }
        return sorted_diff;

    }

    public static <K extends Comparable,V extends Comparable> Map<K,V> sortByValues(Map<K,V> map){
        List<Map.Entry<K,V>> entries = new LinkedList<Map.Entry<K,V>>(map.entrySet());
        Collections.sort(entries, new Comparator<Map.Entry<K,V>>() {
            @Override
            public int compare(Entry<K, V> o1, Entry<K, V> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        Map<K,V> sortedMap = new LinkedHashMap<K,V>();
        for(Map.Entry<K,V> entry: entries){
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        return sortedMap;
    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + ", ");
        }
    }

    public static void printArray2(int[][] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[1][i] + ", ");
        }
    }
}

Console output

0 个答案:

没有答案