1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.spark.examples.mllib; 19 20 // $example on$ 21 import java.util.LinkedList; 22 // $example off$ 23 24 import org.apache.spark.SparkConf; 25 import org.apache.spark.SparkContext; 26 // $example on$ 27 import org.apache.spark.api.java.JavaRDD; 28 import org.apache.spark.api.java.JavaSparkContext; 29 import org.apache.spark.mllib.linalg.Matrix; 30 import org.apache.spark.mllib.linalg.Vector; 31 import org.apache.spark.mllib.linalg.Vectors; 32 import org.apache.spark.mllib.linalg.distributed.RowMatrix; 33 // $example off$ 34 35 /** 36 * Example for compute principal components on a 'RowMatrix'. 37 */ 38 public class JavaPCAExample { main(String[] args)39 public static void main(String[] args) { 40 SparkConf conf = new SparkConf().setAppName("PCA Example"); 41 SparkContext sc = new SparkContext(conf); 42 43 // $example on$ 44 double[][] array = {{1.12, 2.05, 3.12}, {5.56, 6.28, 8.94}, {10.2, 8.0, 20.5}}; 45 LinkedList<Vector> rowsList = new LinkedList<>(); 46 for (int i = 0; i < array.length; i++) { 47 Vector currentRow = Vectors.dense(array[i]); 48 rowsList.add(currentRow); 49 } 50 JavaRDD<Vector> rows = JavaSparkContext.fromSparkContext(sc).parallelize(rowsList); 51 52 // Create a RowMatrix from JavaRDD<Vector>. 53 RowMatrix mat = new RowMatrix(rows.rdd()); 54 55 // Compute the top 3 principal components. 56 Matrix pc = mat.computePrincipalComponents(3); 57 RowMatrix projected = mat.multiply(pc); 58 // $example off$ 59 Vector[] collectPartitions = (Vector[])projected.rows().collect(); 60 System.out.println("Projected vector of principal component:"); 61 for (Vector vector : collectPartitions) { 62 System.out.println("\t" + vector); 63 } 64 } 65 } 66