spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From yinxusen <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-13012] [Documentation] Replace example ...
Date Wed, 17 Feb 2016 18:10:02 GMT
Github user yinxusen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11053#discussion_r53205371
  
    --- Diff: examples/src/main/java/org/apache/spark/examples/ml/JavaModelSelectionViaCrossValidationExample.java
---
    @@ -0,0 +1,120 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.examples.ml;
    +
    +//$example on$
    +import java.util.Arrays;
    +
    +import org.apache.spark.SparkConf;
    +import org.apache.spark.SparkContext;
    +import org.apache.spark.ml.Pipeline;
    +import org.apache.spark.ml.PipelineStage;
    +import org.apache.spark.ml.classification.LogisticRegression;
    +import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator;
    +import org.apache.spark.ml.feature.HashingTF;
    +import org.apache.spark.ml.feature.Tokenizer;
    +import org.apache.spark.ml.param.ParamMap;
    +import org.apache.spark.ml.tuning.CrossValidator;
    +import org.apache.spark.ml.tuning.CrossValidatorModel;
    +import org.apache.spark.ml.tuning.ParamGridBuilder;
    +import org.apache.spark.sql.DataFrame;
    +import org.apache.spark.sql.Row;
    +import org.apache.spark.sql.SQLContext;
    +//$example off$
    +
    +/**
    + * Java example for Model Selection via Cross Validation.
    + */
    +public class JavaModelSelectionViaCrossValidationExample {
    +  public static void main(String[] args) {
    +    SparkConf conf = new SparkConf()
    +      .setAppName("JavaModelSelectionViaCrossValidationExample");
    +    SparkContext sc = new SparkContext(conf);
    +    SQLContext sqlContext = new SQLContext(sc);
    +
    +    // $example on$
    +    // Prepare training documents, which are labeled.
    +    DataFrame training = sqlContext.createDataFrame(Arrays.asList(
    +      new JavaLabeledDocument(0L, "a b c d e spark", 1.0),
    +      new JavaLabeledDocument(1L, "b d", 0.0),
    +      new JavaLabeledDocument(2L,"spark f g h", 1.0),
    +      new JavaLabeledDocument(3L, "hadoop mapreduce", 0.0),
    +      new JavaLabeledDocument(4L, "b spark who", 1.0),
    +      new JavaLabeledDocument(5L, "g d a y", 0.0),
    +      new JavaLabeledDocument(6L, "spark fly", 1.0),
    +      new JavaLabeledDocument(7L, "was mapreduce", 0.0),
    +      new JavaLabeledDocument(8L, "e spark program", 1.0),
    +      new JavaLabeledDocument(9L, "a e c l", 0.0),
    +      new JavaLabeledDocument(10L, "spark compile", 1.0),
    +      new JavaLabeledDocument(11L, "hadoop software", 0.0)
    +    ), JavaLabeledDocument.class);
    +
    +    // Configure an ML pipeline, which consists of three stages: tokenizer, hashingTF,
and lr.
    +    Tokenizer tokenizer = new Tokenizer()
    +      .setInputCol("text")
    +      .setOutputCol("words");
    +    HashingTF hashingTF = new HashingTF()
    +      .setNumFeatures(1000)
    +      .setInputCol(tokenizer.getOutputCol())
    +      .setOutputCol("features");
    +    LogisticRegression lr = new LogisticRegression()
    +      .setMaxIter(10)
    +      .setRegParam(0.01);
    +    Pipeline pipeline = new Pipeline()
    +      .setStages(new PipelineStage[] { tokenizer, hashingTF, lr });
    +
    +    // We use a ParamGridBuilder to construct a grid of parameters to search over.
    +    // With 3 values for hashingTF.numFeatures and 2 values for lr.regParam,
    +    // this grid will have 3 x 2 = 6 parameter settings for CrossValidator to choose
from.
    +    ParamMap[] paramGrid = new ParamGridBuilder()
    +      .addGrid(hashingTF.numFeatures(), new int[] { 10, 100, 1000 })
    +      .addGrid(lr.regParam(), new double[] { 0.1, 0.01 })
    --- End diff --
    
    ditto


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message