Skip to content

Commit e433872

Browse files
committed
Updated docs. Added LabeledPointSuite to spark.ml
1 parent 54b7b31 commit e433872

File tree

5 files changed

+80
-7
lines changed

5 files changed

+80
-7
lines changed

mllib/src/main/scala/org/apache/spark/ml/Estimator.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ abstract class Estimator[M <: Model[M]] extends PipelineStage with Params {
3434
* Fits a single model to the input data with optional parameters.
3535
*
3636
* @param dataset input dataset
37-
* @param paramPairs optional list of param pairs (overwrite embedded params)
37+
* @param paramPairs Optional list of param pairs.
38+
* These values override any specified in this Estimator's embedded ParamMap.
3839
* @return fitted model
3940
*/
4041
@varargs
@@ -47,7 +48,8 @@ abstract class Estimator[M <: Model[M]] extends PipelineStage with Params {
4748
* Fits a single model to the input data with provided parameter map.
4849
*
4950
* @param dataset input dataset
50-
* @param paramMap parameter map
51+
* @param paramMap Parameter map.
52+
* These values override any specified in this Estimator's embedded ParamMap.
5153
* @return fitted model
5254
*/
5355
def fit(dataset: DataFrame, paramMap: ParamMap): M
@@ -58,7 +60,8 @@ abstract class Estimator[M <: Model[M]] extends PipelineStage with Params {
5860
* Subclasses could overwrite this to optimize multi-model training.
5961
*
6062
* @param dataset input dataset
61-
* @param paramMaps an array of parameter maps
63+
* @param paramMaps An array of parameter maps.
64+
* These values override any specified in this Estimator's embedded ParamMap.
6265
* @return fitted models, matching the input parameter maps
6366
*/
6467
def fit(dataset: DataFrame, paramMaps: Array[ParamMap]): Seq[M] = {

mllib/src/main/scala/org/apache/spark/ml/classification/LogisticRegression.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ class LogisticRegression extends Classifier[LogisticRegression, LogisticRegressi
6868
def setThreshold(value: Double): this.type = set(threshold, value)
6969
def setScoreCol(value: String): this.type = set(scoreCol, value)
7070

71+
/**
72+
* Same as [[fit()]], but using strong types.
73+
*
74+
* @param dataset Training data. WARNING: This does not yet handle instance weights.
75+
* @param paramMap Parameters for training.
76+
* These values override any specified in this Estimator's embedded ParamMap.
77+
*/
7178
def train(dataset: RDD[LabeledPoint], paramMap: ParamMap): LogisticRegressionModel = {
7279
val oldDataset = dataset.map { case LabeledPoint(label: Double, features: Vector, weight) =>
7380
org.apache.spark.mllib.regression.LabeledPoint(label, features)

mllib/src/main/scala/org/apache/spark/ml/impl/estimator/Predictor.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,11 @@ private[ml] abstract class Predictor[Learner <: Predictor[Learner, M], M <: Pred
9494
}
9595

9696
/**
97-
* Notes to developers:
98-
* - Unlike [[fit()]], this method takes [[paramMap]] which has already been
99-
* combined with the internal paramMap.
100-
* - This should handle caching the dataset if needed.
97+
* Same as [[fit()]], but using strong types.
98+
*
10199
* @param dataset Training data
102100
* @param paramMap Parameters for training.
101+
* These values override any specified in this Estimator's embedded ParamMap.
103102
*/
104103
def train(dataset: RDD[LabeledPoint], paramMap: ParamMap): M
105104
}

mllib/src/main/scala/org/apache/spark/ml/regression/LinearRegression.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ class LinearRegression extends Regressor[LinearRegression, LinearRegressionModel
4545
def setRegParam(value: Double): this.type = set(regParam, value)
4646
def setMaxIter(value: Int): this.type = set(maxIter, value)
4747

48+
/**
49+
* Same as [[fit()]], but using strong types.
50+
*
51+
* @param dataset Training data. WARNING: This does not yet handle instance weights.
52+
* @param paramMap Parameters for training.
53+
* These values override any specified in this Estimator's embedded ParamMap.
54+
*/
4855
def train(dataset: RDD[LabeledPoint], paramMap: ParamMap): LinearRegressionModel = {
4956
val oldDataset = dataset.map { case LabeledPoint(label: Double, features: Vector, weight) =>
5057
org.apache.spark.mllib.regression.LabeledPoint(label, features)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.ml
19+
20+
import org.scalatest.FunSuite
21+
22+
import org.apache.spark.mllib.classification.LogisticRegressionSuite.generateLogisticInput
23+
import org.apache.spark.mllib.linalg.Vectors
24+
import org.apache.spark.mllib.util.MLlibTestSparkContext
25+
import org.apache.spark.sql.{SQLContext, SchemaRDD}
26+
27+
class LabeledPointSuite extends FunSuite with MLlibTestSparkContext {
28+
29+
@transient var sqlContext: SQLContext = _
30+
31+
override def beforeAll(): Unit = {
32+
super.beforeAll()
33+
sqlContext = new SQLContext(sc)
34+
}
35+
36+
test("LabeledPoint default weight 1.0") {
37+
val label = 1.0
38+
val features = Vectors.dense(1.0, 2.0, 3.0)
39+
val lp1 = LabeledPoint(label, features)
40+
val lp2 = LabeledPoint(label, features, weight = 1.0)
41+
assert(lp1 === lp2)
42+
}
43+
44+
test("Create SchemaRDD from RDD[LabeledPoint]") {
45+
val sqlContext = this.sqlContext
46+
import sqlContext._
47+
val arr = Seq(
48+
LabeledPoint(0.0, Vectors.dense(1.0, 2.0, 3.0)),
49+
LabeledPoint(1.0, Vectors.dense(1.1, 2.1, 3.1)),
50+
LabeledPoint(0.0, Vectors.dense(1.2, 2.2, 3.2)),
51+
LabeledPoint(1.0, Vectors.dense(1.3, 2.3, 3.3)))
52+
val rdd = sc.parallelize(arr)
53+
val schemaRDD = rdd.select('label, 'features)
54+
val points = schemaRDD.collect()
55+
assert(points.size === arr.size)
56+
}
57+
}

0 commit comments

Comments
 (0)