Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.examples.mllib;
0019 
0020 // $example on$
0021 import java.util.Arrays;
0022 import java.util.List;
0023 // $example off$
0024 import org.apache.spark.SparkConf;
0025 import org.apache.spark.api.java.JavaSparkContext;
0026 // $example on$
0027 import org.apache.spark.api.java.JavaRDD;
0028 import org.apache.spark.mllib.clustering.BisectingKMeans;
0029 import org.apache.spark.mllib.clustering.BisectingKMeansModel;
0030 import org.apache.spark.mllib.linalg.Vector;
0031 import org.apache.spark.mllib.linalg.Vectors;
0032 // $example off$
0033 
0034 /**
0035  * Java example for bisecting k-means clustering.
0036  */
0037 public class JavaBisectingKMeansExample {
0038   public static void main(String[] args) {
0039     SparkConf sparkConf = new SparkConf().setAppName("JavaBisectingKMeansExample");
0040     JavaSparkContext sc = new JavaSparkContext(sparkConf);
0041 
0042     // $example on$
0043     List<Vector> localData = Arrays.asList(
0044       Vectors.dense(0.1, 0.1),   Vectors.dense(0.3, 0.3),
0045       Vectors.dense(10.1, 10.1), Vectors.dense(10.3, 10.3),
0046       Vectors.dense(20.1, 20.1), Vectors.dense(20.3, 20.3),
0047       Vectors.dense(30.1, 30.1), Vectors.dense(30.3, 30.3)
0048     );
0049     JavaRDD<Vector> data = sc.parallelize(localData, 2);
0050 
0051     BisectingKMeans bkm = new BisectingKMeans()
0052       .setK(4);
0053     BisectingKMeansModel model = bkm.run(data);
0054 
0055     System.out.println("Compute Cost: " + model.computeCost(data));
0056 
0057     Vector[] clusterCenters = model.clusterCenters();
0058     for (int i = 0; i < clusterCenters.length; i++) {
0059       Vector clusterCenter = clusterCenters[i];
0060       System.out.println("Cluster Center " + i + ": " + clusterCenter);
0061     }
0062     // $example off$
0063 
0064     sc.stop();
0065   }
0066 }