| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
DeliaPitt
9年前发布

在内存中执行k-means聚类算法

来自: http://blog.csdn.net/u012965373/article/details/50754381


/***   * @author YangXin   * @info 利用点集测试K-Means聚类算法   */  package unitNine;  import java.util.ArrayList;  import java.util.List;  import org.apache.mahout.clustering.Cluster;  import org.apache.mahout.clustering.UncommonDistributions;  import org.apache.mahout.common.distance.EuclideanDistanceMeasure;  import org.apache.mahout.math.DenseVector;  import org.apache.mahout.math.Vector;  public class KMeansExample {   private static void generateSamples(List<Vector> vectors, int num, double mx, double my, double sd){    for(int i = 0; i < num; i++){     vectors.add(new DenseVector(new double[]{UncommonDistributions.rNorm(mx, sd),UncommonDistributions.rNorm(my, sd) }));    }   }      public static void main(String[] args){    List<Vector> sampleData = new ArrayList<Vector>();      RandomPointsUtil.generateSamples(sampleData, 400, 1, 1, 3);    RandomPointsUtil.generateSamples(sampleData, 300, 1, 0, 0.5);    RandomPointsUtil.generateSamples(sampleData, 300, 0, 2, 0.1);      int k = 3;    List<Vector> randomPoints = RandomPointsUtil.chooseRandomPoints(      sampleData, k);    List<Cluster> clusters = new ArrayList<Cluster>();      int clusterId = 0;    for (Vector v : randomPoints) {     clusters.add(new Cluster(v, clusterId++, new EuclideanDistanceMeasure()));    }      List<List<Cluster>> finalClusters = KMeansClusterer.clusterPoints(      sampleData, clusters, new EuclideanDistanceMeasure(), 3, 0.01);    for (Cluster cluster : finalClusters.get(finalClusters.size() - 1)) {     System.out.println("Cluster id: " + cluster.getId() + " center: "       + cluster.getCenter().asFormatString());    }  }  </span></strong>


<strong><span style="font-size:18px;">/***   * @author YangXin   * @info 处理随机点的类   */  package unitNine;  import java.util.ArrayList;  import java.util.List;  import java.util.Random;  import org.apache.mahout.common.RandomUtils;  import org.apache.mahout.math.DenseVector;  import org.apache.mahout.math.Vector;  public class RandomPointsUtil {   public static void generateSamples(List<Vector> vectors, int num,     double mx, double my, double sd) {    for (int i = 0; i < num; i++) {     vectors.add(new DenseVector(new double[] {       org.apache.mahout.clustering.UncommonDistributions.rNorm(mx, sd),       org.apache.mahout.clustering.UncommonDistributions.rNorm(my, sd) }));    }   }        public static List<Vector> chooseRandomPoints(Iterable<Vector> vectors, int k) {      List<Vector> chosenPoints = new ArrayList<Vector>(k);      Random random = RandomUtils.getRandom();      for (Vector value : vectors) {        int currentSize = chosenPoints.size();        if (currentSize < k) {          chosenPoints.add(value);        } else if (random.nextInt(currentSize + 1) == 0) { // with chance 1/(currentSize+1) pick new element          int indexToRemove = random.nextInt(currentSize); // evict one chosen randomly          chosenPoints.remove(indexToRemove);          chosenPoints.add(value);        }      }      return chosenPoints;    }  }


 本文由用户 DeliaPitt 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1456659655562.html
算法