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.classification.LogisticRegression;
0022 import org.apache.spark.ml.classification.OneVsRest;
0023 import org.apache.spark.ml.classification.OneVsRestModel;
0024 import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator;
0025 import org.apache.spark.sql.Dataset;
0026 import org.apache.spark.sql.Row;
0027 // $example off$
0028 import org.apache.spark.sql.SparkSession;
0029 
0030 
0031 /**
0032  * An example of Multiclass to Binary Reduction with One Vs Rest,
0033  * using Logistic Regression as the base classifier.
0034  * Run with
0035  * <pre>
0036  * bin/run-example ml.JavaOneVsRestExample
0037  * </pre>
0038  */
0039 public class JavaOneVsRestExample {
0040   public static void main(String[] args) {
0041     SparkSession spark = SparkSession
0042       .builder()
0043       .appName("JavaOneVsRestExample")
0044       .getOrCreate();
0045 
0046     // $example on$
0047     // load data file.
0048     Dataset<Row> inputData = spark.read().format("libsvm")
0049       .load("data/mllib/sample_multiclass_classification_data.txt");
0050 
0051     // generate the train/test split.
0052     Dataset<Row>[] tmp = inputData.randomSplit(new double[]{0.8, 0.2});
0053     Dataset<Row> train = tmp[0];
0054     Dataset<Row> test = tmp[1];
0055 
0056     // configure the base classifier.
0057     LogisticRegression classifier = new LogisticRegression()
0058       .setMaxIter(10)
0059       .setTol(1E-6)
0060       .setFitIntercept(true);
0061 
0062     // instantiate the One Vs Rest Classifier.
0063     OneVsRest ovr = new OneVsRest().setClassifier(classifier);
0064 
0065     // train the multiclass model.
0066     OneVsRestModel ovrModel = ovr.fit(train);
0067 
0068     // score the model on test data.
0069     Dataset<Row> predictions = ovrModel.transform(test)
0070       .select("prediction", "label");
0071 
0072     // obtain evaluator.
0073     MulticlassClassificationEvaluator evaluator = new MulticlassClassificationEvaluator()
0074             .setMetricName("accuracy");
0075 
0076     // compute the classification error on test data.
0077     double accuracy = evaluator.evaluate(predictions);
0078     System.out.println("Test Error = " + (1 - accuracy));
0079     // $example off$
0080 
0081     spark.stop();
0082   }
0083 
0084 }