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 org.apache.spark.api.java.JavaRDD;
0024 import org.apache.spark.mllib.feature.ChiSqSelector;
0025 import org.apache.spark.mllib.feature.ChiSqSelectorModel;
0026 import org.apache.spark.mllib.linalg.Vectors;
0027 import org.apache.spark.mllib.regression.LabeledPoint;
0028 import org.apache.spark.mllib.util.MLUtils;
0029 // $example off$
0030 
0031 public class JavaChiSqSelectorExample {
0032   public static void main(String[] args) {
0033 
0034     SparkConf conf = new SparkConf().setAppName("JavaChiSqSelectorExample");
0035     JavaSparkContext jsc = new JavaSparkContext(conf);
0036 
0037     // $example on$
0038     JavaRDD<LabeledPoint> points = MLUtils.loadLibSVMFile(jsc.sc(),
0039       "data/mllib/sample_libsvm_data.txt").toJavaRDD().cache();
0040 
0041     // Discretize data in 16 equal bins since ChiSqSelector requires categorical features
0042     // Although features are doubles, the ChiSqSelector treats each unique value as a category
0043     JavaRDD<LabeledPoint> discretizedData = points.map(lp -> {
0044       double[] discretizedFeatures = new double[lp.features().size()];
0045       for (int i = 0; i < lp.features().size(); ++i) {
0046         discretizedFeatures[i] = Math.floor(lp.features().apply(i) / 16);
0047       }
0048       return new LabeledPoint(lp.label(), Vectors.dense(discretizedFeatures));
0049     });
0050 
0051     // Create ChiSqSelector that will select top 50 of 692 features
0052     ChiSqSelector selector = new ChiSqSelector(50);
0053     // Create ChiSqSelector model (selecting features)
0054     ChiSqSelectorModel transformer = selector.fit(discretizedData.rdd());
0055     // Filter the top 50 features from each feature vector
0056     JavaRDD<LabeledPoint> filteredData = discretizedData.map(lp ->
0057       new LabeledPoint(lp.label(), transformer.transform(lp.features())));
0058     // $example off$
0059 
0060     System.out.println("filtered data: ");
0061     filteredData.foreach(System.out::println);
0062 
0063     jsc.stop();
0064   }
0065 }