亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? benchmarkedkmeans.java

?? java 語言編寫的kmeans聚類算法源代碼
?? JAVA
?? 第 1 頁 / 共 2 頁
字號:
package kmeans;

import java.io.*;
import java.util.*;

/**
 * Same as BasicKMeans, but with the addition of 
 * timing statements.
 */
public class BenchmarkedKMeans implements KMeans {

    // Temporary clusters used during the clustering process.  Converted to
    // an array of the simpler class Cluster at the conclusion.
    private ProtoCluster[] mProtoClusters;

    // Cache of coordinate-to-cluster distances. Number of entries = 
    // number of clusters X number of coordinates.
    private double[][] mDistanceCache;

    // Used in makeAssignments() to figure out how many moves are made
    // during each iteration -- the cluster assignment for coordinate n is
    // found in mClusterAssignments[n] where the N coordinates are numbered
    // 0 ... (N-1)
    private int[] mClusterAssignments;

    // 2D array holding the coordinates to be clustered.
    private double[][] mCoordinates;
    // The desired number of clusters and maximum number
    // of iterations.
    private int mK, mMaxIterations;
    // Seed for the random number generator used to select
    // coordinates for the initial cluster centers.
    private long mRandomSeed;
    
    // An array of Cluster objects: the output of k-means.
    private Cluster[] mClusters;

    // Listeners to be notified of significant happenings.
    private List<KMeansListener> mListeners = new ArrayList<KMeansListener>(1);
    
    // Fields to accumulate the time in milliseconds for 
    // initializing the centers, computing the distances,
    // computing the centers, and making the assignments.
    private long mInitCentersMS, mComputeDistancesMS, 
        mComputeCentersMS, mAssignmentMS;
    
    /**
     * Constructor
     * 
     * @param coordinates two-dimensional array containing the coordinates to be clustered.
     * @param k  the number of desired clusters.
     * @param maxIterations the maximum number of clustering iterations.
     * @param randomSeed seed used with the random number generator.
     */
    public BenchmarkedKMeans(double[][] coordinates, int k, int maxIterations, 
            long randomSeed) {
        mCoordinates = coordinates;
        // Can't have more clusters than coordinates.
        mK = Math.min(k, mCoordinates.length);
        mMaxIterations = maxIterations;
        mRandomSeed = randomSeed;
    }

    /** 
     * Adds a KMeansListener to be notified of significant happenings.
     * 
     * @param l  the listener to be added.
     */
    public void addKMeansListener(KMeansListener l) {
        synchronized (mListeners) {
            if (!mListeners.contains(l)) {
                mListeners.add(l);
            }
        }
    }
    
    /**
     * Removes a KMeansListener from the listener list.
     * 
     * @param l the listener to be removed.
     */
    public void removeKMeansListener(KMeansListener l) {
        synchronized (mListeners) {
            mListeners.remove(l);
        }
    }
    
    /**
     * Posts a message to registered KMeansListeners.
     * 
     * @param message
     */
    private void postKMeansMessage(String message) {
        if (mListeners.size() > 0) {
            synchronized (mListeners) {
                int sz = mListeners.size();
                for (int i=0; i<sz; i++) {
                    mListeners.get(i).kmeansMessage(message);
                }
            }
        }
    }
    
    /**
     * Notifies registered listeners that k-means is complete.
     * 
     * @param clusters the output of clustering.
     * @param executionTime the number of milliseconds taken to cluster.
     */
    private void postKMeansComplete(Cluster[] clusters, long executionTime) {
        if (mListeners.size() > 0) {
            synchronized (mListeners) {
                int sz = mListeners.size();
                for (int i=0; i<sz; i++) {
                    mListeners.get(i).kmeansComplete(clusters, executionTime);
                }
            }
        }
    }
    
    /**
     * Notifies registered listeners that k-means has failed because of
     * a Throwable caught in the run method.
     * 
     * @param err 
     */
    private void postKMeansError(Throwable err) {
        if (mListeners.size() > 0) {
            synchronized (mListeners) {
                int sz = mListeners.size();
                for (int i=0; i<sz; i++) {
                    mListeners.get(i).kmeansError(err);
                }
            }
        }
    }
    
    /**
     * Get the clusters computed by the algorithm.  This method should
     * not be called until clustering has completed successfully.
     * 
     * @return an array of Cluster objects.
     */
    public Cluster[] getClusters() {
        return mClusters;
    }
    
    /**
     * Run the clustering algorithm.
     */
    public void run() {

        try {
            
            // Note the start time.
            long startTime = System.currentTimeMillis();
            
            postKMeansMessage("K-Means clustering started");
            
            // Randomly initialize the cluster centers creating the
            // array mProtoClusters.
            initCenters();
            
            postKMeansMessage("... centers initialized");

            // Perform the initial computation of distances.
            computeDistances();

            // Make the initial cluster assignments.
            makeAssignments();

            // Number of moves in the iteration and the iteration counter.
            int moves = 0, it = 0;
            
            // Main Loop:
            //
            // Two stopping criteria:
            // - no moves in makeAssignments 
            //   (moves == 0)
            // OR
            // - the maximum number of iterations has been reached
            //   (it == mMaxIterations)
            //
            do {

                // Compute the centers of the clusters that need updating.
                computeCenters();
                
                // Compute the stored distances between the updated clusters and the
                // coordinates.
                computeDistances();

                // Make this iteration's assignments.
                moves = makeAssignments();

                it++;
                
                postKMeansMessage("... iteration " + it + " moves = " + moves);

            } while (moves > 0 && it < mMaxIterations);

            // Transform the array of ProtoClusters to an array
            // of the simpler class Cluster.
            mClusters = generateFinalClusters();
            
            long executionTime = System.currentTimeMillis() - startTime;
            
            // Post the time statistics.
            postKMeansMessage("TIME STATISTICS:\nInitializing centers: " + 
                    percentString(mInitCentersMS, executionTime));
            postKMeansMessage("   Computing centers: " +
                    percentString(mComputeCentersMS, executionTime));
            postKMeansMessage(" Computing distances: " +
                    percentString(mComputeDistancesMS, executionTime));
            postKMeansMessage("  Making assignments: " +
                    percentString(mAssignmentMS, executionTime));
            
            postKMeansComplete(mClusters, executionTime);
            
        } catch (Throwable t) {
           
            postKMeansError(t);
            
        } finally {

            // Clean up temporary data structures used during the algorithm.
            cleanup();
            
        }
    }
    
    /**
     * Convenience method for generating percentage string.
     * 
     * @param numerator
     * @param denominator
     * 
     * @return a string of the form 10.2%
     */
    private static String percentString(long numerator, long denominator) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        double percent = (100.0*numerator)/denominator;
        pw.printf("%02.1f", percent);
        return sw.toString();
    }

    /**
     * Randomly select coordinates to be the initial cluster centers.
     */
    private void initCenters() {

        long t = System.currentTimeMillis();
        
        Random random = new Random(mRandomSeed);
        
        int coordCount = mCoordinates.length;

        // The array mClusterAssignments is used only to keep track of the cluster 
        // membership for each coordinate.  The method makeAssignments() uses it
        // to keep track of the number of moves.
        if (mClusterAssignments == null) {
            mClusterAssignments = new int[coordCount];
            // Initialize to -1 to indicate that they haven't been assigned yet.
            Arrays.fill(mClusterAssignments, -1);
        }

        // Place the coordinate indices into an array and shuffle it.
        int[] indices = new int[coordCount];
        for (int i = 0; i < coordCount; i++) {
            indices[i] = i;
        }
        for (int i = 0, m = coordCount; m > 0; i++, m--) {
            int j = i + random.nextInt(m);
            if (i != j) {
                // Swap the indices.
                indices[i] ^= indices[j];
                indices[j] ^= indices[i];
                indices[i] ^= indices[j];
            }
        }

        mProtoClusters = new ProtoCluster[mK];
        for (int i=0; i<mK; i++) {
            int coordIndex = indices[i];
            mProtoClusters[i] = new ProtoCluster(mCoordinates[coordIndex], coordIndex);
            mClusterAssignments[indices[i]] = i;
        }
        
        mInitCentersMS += (System.currentTimeMillis() - t);
    }

    /**
     * Recompute the centers of the protoclusters with 
     * update flags set to true.
     */
    private void computeCenters() {
        
        long t = System.currentTimeMillis();
        
        int numClusters = mProtoClusters.length;
        
        // Sets the update flags of the protoclusters that haven't been deleted and
        // whose memberships have changed in the iteration just completed.
        //
        for (int c = 0; c < numClusters; c++) {
            ProtoCluster cluster = mProtoClusters[c];
            if (cluster.getConsiderForAssignment()) {
                if (!cluster.isEmpty()) {
                    // This sets the protocluster's update flag to
                    // true only if its membership changed in last call
                    // to makeAssignments().  
                    cluster.setUpdateFlag();
                    // If the update flag was set, update the center.
                    if (cluster.needsUpdate()) {
                        cluster.updateCenter(mCoordinates);
                    }
                } else {
                    // When a cluster loses all of its members, it
                    // falls out of contention.  So it is possible for
                    // k-means to return fewer than k clusters.
                    cluster.setConsiderForAssignment(false);
                }
            }
        }
        
        mComputeCentersMS += (System.currentTimeMillis() - t);
    }

    /** 
     * Compute distances between coodinates and cluster centers,

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美女孩性生活视频| 色综合久久中文综合久久97| 天堂成人国产精品一区| 亚洲国产欧美另类丝袜| 亚洲一区电影777| 亚洲chinese男男1069| 午夜欧美在线一二页| 午夜久久电影网| 免费日本视频一区| 久88久久88久久久| 国产.欧美.日韩| 99久久精品情趣| 欧美影片第一页| 欧美一区二区福利视频| 久久综合狠狠综合久久综合88| 精品国产网站在线观看| 国产欧美精品一区二区三区四区| 久久日一线二线三线suv| 欧美激情资源网| 亚洲乱码国产乱码精品精98午夜| 亚洲国产精品久久久男人的天堂| 青青草原综合久久大伊人精品优势| 看片网站欧美日韩| 成人精品一区二区三区中文字幕| 91麻豆国产精品久久| 欧美男同性恋视频网站| 2017欧美狠狠色| 亚洲欧美一区二区三区国产精品| 亚洲综合在线五月| 九九精品视频在线看| 99精品欧美一区| 91精品国产福利| **欧美大码日韩| 麻豆专区一区二区三区四区五区| 国产不卡免费视频| 欧美巨大另类极品videosbest | 免费在线观看一区| 国产成人亚洲综合a∨猫咪 | 亚洲一线二线三线久久久| 日韩激情一区二区| 99视频一区二区| 欧美成人猛片aaaaaaa| 亚洲视频你懂的| 极品瑜伽女神91| 在线视频欧美区| 国产亚洲精品久| 日韩黄色一级片| 色天使久久综合网天天| 久久欧美一区二区| 日韩在线播放一区二区| 91在线免费视频观看| 日韩欧美精品在线| 一区二区三区四区国产精品| 国产美女主播视频一区| 欧美精品视频www在线观看| 亚洲欧美综合另类在线卡通| 国产资源在线一区| 欧美一区二区视频在线观看| 亚洲制服丝袜在线| 91蜜桃免费观看视频| 欧美国产综合色视频| 激情伊人五月天久久综合| 欧美日韩国产大片| 亚洲一区二区在线播放相泽| 91在线精品一区二区三区| 欧美国产一区二区在线观看| 国产伦精品一区二区三区免费迷 | 免费一级欧美片在线观看| 91福利区一区二区三区| 中文字幕日本不卡| 成人a免费在线看| 国产欧美一区二区精品忘忧草| 久久9热精品视频| 欧美一区二区国产| 日本aⅴ亚洲精品中文乱码| 91精品国产免费| 日韩av高清在线观看| 91精品久久久久久久91蜜桃| 亚洲成人一区二区| 欧美福利视频一区| 男人的j进女人的j一区| 91精品国产日韩91久久久久久| 亚洲国产一区二区视频| 欧美日本不卡视频| 奇米一区二区三区av| 精品国产伦理网| 懂色av中文一区二区三区| 国产日韩精品一区| 97久久精品人人做人人爽| 亚洲精品日产精品乱码不卡| 91豆麻精品91久久久久久| 亚洲va欧美va人人爽午夜 | 蜜桃精品在线观看| 精品蜜桃在线看| 成人国产精品免费观看| 亚洲你懂的在线视频| 6080午夜不卡| 国产精品99久| 亚洲欧美激情视频在线观看一区二区三区 | 91成人国产精品| 婷婷激情综合网| 久久免费看少妇高潮| 99久久久久免费精品国产| 亚洲成va人在线观看| 2021久久国产精品不只是精品| 大桥未久av一区二区三区中文| 伊人色综合久久天天| 欧美一区二区免费| 成人精品国产一区二区4080| 亚洲国产日日夜夜| 精品久久久久香蕉网| 91啦中文在线观看| 精品亚洲porn| 亚洲色图视频免费播放| 欧美一区二区福利视频| 91色porny| 韩国av一区二区| 亚洲电影一级黄| 国产精品―色哟哟| 欧美电影免费观看完整版| 91女神在线视频| 国产精品自拍毛片| 免费在线观看视频一区| 亚洲乱码国产乱码精品精98午夜| 久久婷婷成人综合色| 欧美精品一级二级| 91在线精品一区二区三区| 国产伦精品一区二区三区视频青涩| 亚洲成人免费视| 日韩美女精品在线| 国产免费观看久久| 精品久久久久久久久久久久久久久久久 | 日本美女视频一区二区| 亚洲视频免费在线| 日本一区二区三区视频视频| 日韩一二三区不卡| 3d成人动漫网站| 欧美三级日韩三级国产三级| 94-欧美-setu| 91视频在线观看免费| 丁香婷婷综合色啪| 国产精品一区二区果冻传媒| 精品一区二区三区在线播放视频 | 国产一区啦啦啦在线观看| 日韩电影免费在线看| 午夜精品久久久久久久99水蜜桃| 亚洲天堂网中文字| 亚洲人成在线播放网站岛国| 国产精品伦理一区二区| 欧美激情一区二区三区全黄| 欧美经典一区二区三区| 久久蜜臀中文字幕| 依依成人综合视频| 亚洲伦在线观看| 一区二区激情小说| 亚洲国产一区二区在线播放| 亚洲国产日韩综合久久精品| 亚洲永久精品国产| 亚洲成人三级小说| 蜜臂av日日欢夜夜爽一区| 久久se这里有精品| 国产麻豆成人精品| 成av人片一区二区| 色偷偷久久人人79超碰人人澡| 在线区一区二视频| 91精品国产91久久久久久一区二区| 欧美嫩在线观看| 26uuu久久天堂性欧美| 中文字幕国产精品一区二区| 中文字幕一区av| 亚洲午夜久久久久久久久电影院| 日韩在线一二三区| 国产精品一区免费在线观看| zzijzzij亚洲日本少妇熟睡| 欧美中文字幕不卡| 日韩欧美色综合| 亚洲欧洲国产日本综合| 一区二区日韩av| 精品一区二区三区av| 成人国产在线观看| 欧美丰满高潮xxxx喷水动漫| 久久综合99re88久久爱| 中文字幕中文乱码欧美一区二区| 一级女性全黄久久生活片免费| 日韩成人精品在线| 成人av午夜影院| 6080午夜不卡| 一区二区中文字幕在线| 日韩影院在线观看| 成人精品亚洲人成在线| 欧美精品xxxxbbbb| 中文幕一区二区三区久久蜜桃| 亚洲电影一级黄| 成人午夜私人影院| 日韩一级大片在线观看| 国产精品国产成人国产三级| 丁香啪啪综合成人亚洲小说| 91精品婷婷国产综合久久竹菊| 国产亲近乱来精品视频 | 日韩一区二区三区在线|