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.ml;
0019 
0020 // $example on$
0021 import org.apache.spark.ml.evaluation.RegressionEvaluator;
0022 import org.apache.spark.ml.param.ParamMap;
0023 import org.apache.spark.ml.regression.LinearRegression;
0024 import org.apache.spark.ml.tuning.ParamGridBuilder;
0025 import org.apache.spark.ml.tuning.TrainValidationSplit;
0026 import org.apache.spark.ml.tuning.TrainValidationSplitModel;
0027 import org.apache.spark.sql.Dataset;
0028 import org.apache.spark.sql.Row;
0029 // $example off$
0030 import org.apache.spark.sql.SparkSession;
0031 
0032 /**
0033  * Java example demonstrating model selection using TrainValidationSplit.
0034  *
0035  * Run with
0036  * {{{
0037  * bin/run-example ml.JavaModelSelectionViaTrainValidationSplitExample
0038  * }}}
0039  */
0040 public class JavaModelSelectionViaTrainValidationSplitExample {
0041   public static void main(String[] args) {
0042     SparkSession spark = SparkSession
0043       .builder()
0044       .appName("JavaModelSelectionViaTrainValidationSplitExample")
0045       .getOrCreate();
0046 
0047     // $example on$
0048     Dataset<Row> data = spark.read().format("libsvm")
0049       .load("data/mllib/sample_linear_regression_data.txt");
0050 
0051     // Prepare training and test data.
0052     Dataset<Row>[] splits = data.randomSplit(new double[] {0.9, 0.1}, 12345);
0053     Dataset<Row> training = splits[0];
0054     Dataset<Row> test = splits[1];
0055 
0056     LinearRegression lr = new LinearRegression();
0057 
0058     // We use a ParamGridBuilder to construct a grid of parameters to search over.
0059     // TrainValidationSplit will try all combinations of values and determine best model using
0060     // the evaluator.
0061     ParamMap[] paramGrid = new ParamGridBuilder()
0062       .addGrid(lr.regParam(), new double[] {0.1, 0.01})
0063       .addGrid(lr.fitIntercept())
0064       .addGrid(lr.elasticNetParam(), new double[] {0.0, 0.5, 1.0})
0065       .build();
0066 
0067     // In this case the estimator is simply the linear regression.
0068     // A TrainValidationSplit requires an Estimator, a set of Estimator ParamMaps, and an Evaluator.
0069     TrainValidationSplit trainValidationSplit = new TrainValidationSplit()
0070       .setEstimator(lr)
0071       .setEvaluator(new RegressionEvaluator())
0072       .setEstimatorParamMaps(paramGrid)
0073       .setTrainRatio(0.8)  // 80% for training and the remaining 20% for validation
0074       .setParallelism(2);  // Evaluate up to 2 parameter settings in parallel
0075 
0076     // Run train validation split, and choose the best set of parameters.
0077     TrainValidationSplitModel model = trainValidationSplit.fit(training);
0078 
0079     // Make predictions on test data. model is the model with combination of parameters
0080     // that performed best.
0081     model.transform(test)
0082       .select("features", "label", "prediction")
0083       .show();
0084     // $example off$
0085 
0086     spark.stop();
0087   }
0088 }