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 // $example on$
0023 import java.util.Arrays;
0024 
0025 import org.apache.spark.api.java.JavaRDD;
0026 import org.apache.spark.mllib.linalg.Vector;
0027 import org.apache.spark.mllib.linalg.Vectors;
0028 import org.apache.spark.mllib.stat.MultivariateStatisticalSummary;
0029 import org.apache.spark.mllib.stat.Statistics;
0030 // $example off$
0031 
0032 public class JavaSummaryStatisticsExample {
0033   public static void main(String[] args) {
0034 
0035     SparkConf conf = new SparkConf().setAppName("JavaSummaryStatisticsExample");
0036     JavaSparkContext jsc = new JavaSparkContext(conf);
0037 
0038     // $example on$
0039     JavaRDD<Vector> mat = jsc.parallelize(
0040       Arrays.asList(
0041         Vectors.dense(1.0, 10.0, 100.0),
0042         Vectors.dense(2.0, 20.0, 200.0),
0043         Vectors.dense(3.0, 30.0, 300.0)
0044       )
0045     ); // an RDD of Vectors
0046 
0047     // Compute column summary statistics.
0048     MultivariateStatisticalSummary summary = Statistics.colStats(mat.rdd());
0049     System.out.println(summary.mean());  // a dense vector containing the mean value for each column
0050     System.out.println(summary.variance());  // column-wise variance
0051     System.out.println(summary.numNonzeros());  // number of nonzeros in each column
0052     // $example off$
0053 
0054     jsc.stop();
0055   }
0056 }