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 Sat, 20 Feb 2016 00:52:11 GMT
Github user yinxusen commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11053#discussion_r53539031
  
    --- Diff: examples/src/main/scala/org/apache/spark/examples/ml/PipelineExample.scala ---
    @@ -0,0 +1,93 @@
    +/*
    + * 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.
    + */
    +
    +// scalastyle:off println
    +package org.apache.spark.examples.ml
    +
    +import org.apache.spark.{SparkConf, SparkContext}
    +// $example on$
    +import org.apache.spark.ml.{Pipeline, PipelineModel}
    +import org.apache.spark.ml.classification.LogisticRegression
    +import org.apache.spark.ml.feature.{HashingTF, Tokenizer}
    +import org.apache.spark.mllib.linalg.Vector
    +import org.apache.spark.sql.Row
    +// $example off$
    +import org.apache.spark.sql.SQLContext
    +
    +object PipelineExample {
    +
    +  def main(args: Array[String]): Unit = {
    +    val conf = new SparkConf().setAppName("PipelineExample")
    +    val sc = new SparkContext(conf)
    +    val sqlContext = new SQLContext(sc)
    +
    +    // $example on$
    +    // Prepare training documents from a list of (id, text, label) tuples.
    +    val training = sqlContext.createDataFrame(Seq(
    +      (0L, "a b c d e spark", 1.0),
    +      (1L, "b d", 0.0),
    +      (2L, "spark f g h", 1.0),
    +      (3L, "hadoop mapreduce", 0.0)
    +    )).toDF("id", "text", "label")
    +
    +    // Configure an ML pipeline, which consists of three stages: tokenizer, hashingTF,
and lr.
    +    val tokenizer = new Tokenizer()
    +      .setInputCol("text")
    +      .setOutputCol("words")
    +    val hashingTF = new HashingTF()
    +      .setNumFeatures(1000)
    +      .setInputCol(tokenizer.getOutputCol)
    +      .setOutputCol("features")
    +    val lr = new LogisticRegression()
    +      .setMaxIter(10)
    +      .setRegParam(0.01)
    +    val pipeline = new Pipeline()
    +      .setStages(Array(tokenizer, hashingTF, lr))
    +
    +    // Fit the pipeline to training documents.
    +    val model = pipeline.fit(training)
    +
    +    // Now we can optionally save the fitted pipeline to disk
    +    model.save("/tmp/spark-logistic-regression-model")
    --- End diff --
    
    One more thing, it's better to make it overridable: `model.write.overwrite().save("/tmp/spark-logistic-regression-model")`,
otherwise the example can only run once.


---
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