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 import org.apache.spark.SparkConf;
0021 import org.apache.spark.api.java.JavaSparkContext;
0022 
0023 // $example on$
0024 import org.apache.spark.api.java.JavaRDD;
0025 import org.apache.spark.mllib.clustering.GaussianMixture;
0026 import org.apache.spark.mllib.clustering.GaussianMixtureModel;
0027 import org.apache.spark.mllib.linalg.Vector;
0028 import org.apache.spark.mllib.linalg.Vectors;
0029 // $example off$
0030 
0031 public class JavaGaussianMixtureExample {
0032   public static void main(String[] args) {
0033 
0034     SparkConf conf = new SparkConf().setAppName("JavaGaussianMixtureExample");
0035     JavaSparkContext jsc = new JavaSparkContext(conf);
0036 
0037     // $example on$
0038     // Load and parse data
0039     String path = "data/mllib/gmm_data.txt";
0040     JavaRDD<String> data = jsc.textFile(path);
0041     JavaRDD<Vector> parsedData = data.map(s -> {
0042       String[] sarray = s.trim().split(" ");
0043       double[] values = new double[sarray.length];
0044       for (int i = 0; i < sarray.length; i++) {
0045         values[i] = Double.parseDouble(sarray[i]);
0046       }
0047       return Vectors.dense(values);
0048     });
0049     parsedData.cache();
0050 
0051     // Cluster the data into two classes using GaussianMixture
0052     GaussianMixtureModel gmm = new GaussianMixture().setK(2).run(parsedData.rdd());
0053 
0054     // Save and load GaussianMixtureModel
0055     gmm.save(jsc.sc(), "target/org/apache/spark/JavaGaussianMixtureExample/GaussianMixtureModel");
0056     GaussianMixtureModel sameModel = GaussianMixtureModel.load(jsc.sc(),
0057       "target/org.apache.spark.JavaGaussianMixtureExample/GaussianMixtureModel");
0058 
0059     // Output the parameters of the mixture model
0060     for (int j = 0; j < gmm.k(); j++) {
0061       System.out.printf("weight=%f\nmu=%s\nsigma=\n%s\n",
0062         gmm.weights()[j], gmm.gaussians()[j].mu(), gmm.gaussians()[j].sigma());
0063     }
0064     // $example off$
0065 
0066     jsc.stop();
0067   }
0068 }