Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/7388#discussion_r37314104
--- Diff:
mllib/src/main/scala/org/apache/spark/ml/feature/CountVectorizer.scala ---
@@ -0,0 +1,235 @@
+/*
+ * 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.ml.feature
+
+import org.apache.spark.annotation.Experimental
+import org.apache.spark.broadcast.Broadcast
+import org.apache.spark.ml.param._
+import org.apache.spark.ml.param.shared.{HasInputCol, HasOutputCol}
+import org.apache.spark.ml.util.{Identifiable, SchemaUtils}
+import org.apache.spark.ml.{Estimator, Model}
+import org.apache.spark.mllib.linalg.{VectorUDT, Vectors}
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.types._
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.util.collection.OpenHashMap
+
+/**
+ * Params for [[CountVectorizer]] and [[CountVectorizerModel]].
+ */
+private[feature] trait CountVectorizerParams extends Params with
HasInputCol with HasOutputCol {
+
+ /**
+ * Max size of the vocabulary.
+ * CountVectorizer will build a vocabulary that only considers the top
+ * vocabSize terms ordered by term frequency across the corpus.
+ *
+ * Default: 2^18^
+ * @group param
+ */
+ val vocabSize: IntParam =
+ new IntParam(this, "vocabSize", "max size of the vocabulary",
ParamValidators.gt(0))
+
+ /** @group getParam */
+ def getVocabSize: Int = $(vocabSize)
+
+ /**
+ * Specifies the minimum number of different documents a term must
appear in to be included
+ * in the vocabulary.
+ * If this is an integer >= 1, this specifies the number of documents
the term must appear in;
+ * if this is a double in [0,1), then this specifies the fraction of
documents.
+ *
+ * Default: 1
+ * @group param
+ */
+ val minDF: DoubleParam = new DoubleParam(this, "minDF", "Specifies the
minimum number of" +
+ " different documents a term must appear in to be included in the
vocabulary." +
+ " If this is an integer >= 1, this specifies the number of documents
the term must" +
+ " appear in; if this is a double in [0,1), then this specifies the
fraction of documents.",
+ ParamValidators.gtEq(0.0))
+
+ /** @group getParam */
+ def getMinDF: Double = $(minDF)
+
+ /** Validates and transforms the input schema. */
+ protected def validateAndTransformSchema(schema: StructType): StructType
= {
+ SchemaUtils.checkColumnType(schema, $(inputCol), new
ArrayType(StringType, true))
+ SchemaUtils.appendColumn(schema, $(outputCol), new VectorUDT)
+ }
+
+ /**
+ * Filter to ignore rare words in a document. For each document, terms
with
+ * frequency/count less than the given threshold are ignored.
+ * If this is an integer >= 1, then this specifies a count (of times the
term must appear
+ * in the document);
+ * if this is a double in [0,1), then this specifies a fraction (out of
the document's token
+ * count).
+ *
+ * Note that the parameter is only used in transform of
[[CountVectorizerModel]] and does not
+ * affect fitting.
+ *
+ * Default: 1
+ * @group param
+ */
+ val minTF: DoubleParam = new DoubleParam(this, "minTF", "Filter to
ignore rare words in" +
+ " a document. For each document, terms with frequency/count less than
the given threshold are" +
+ " ignored. If this is an integer >= 1, then this specifies a count (of
times the term must" +
+ " appear in the document); if this is a double in [0,1), then this
specifies a fraction (out" +
+ " of the document's token count). Note that the parameter is only used
in transform of" +
+ " CountVectorizerModel and does not affect fitting.",
ParamValidators.gtEq(0.0))
+
+ setDefault(minTF -> 1)
+
+ /** @group getParam */
+ def getMinTF: Double = $(minTF)
+}
+
+/**
+ * :: Experimental ::
+ * Extracts a vocabulary from document collections and generates a
[[CountVectorizerModel]].
+ */
+@Experimental
+class CountVectorizer(override val uid: String)
+ extends Estimator[CountVectorizerModel] with CountVectorizerParams {
+
+ def this() = this(Identifiable.randomUID("cntVec"))
+
+ /** @group setParam */
+ def setInputCol(value: String): this.type = set(inputCol, value)
+
+ /** @group setParam */
+ def setOutputCol(value: String): this.type = set(outputCol, value)
+
+ /** @group setParam */
+ def setVocabSize(value: Int): this.type = set(vocabSize, value)
+
+ /** @group setParam */
+ def setMinDF(value: Double): this.type = set(minDF, value)
+
+ /** @group setParam */
+ def setMinTF(value: Double): this.type = set(minTF, value)
+
+ setDefault(vocabSize -> (1 << 18), minDF -> 1)
+
+ override def fit(dataset: DataFrame): CountVectorizerModel = {
+ transformSchema(dataset.schema, logging = true)
+ val vocSize = $(vocabSize)
+ val input = dataset.select($(inputCol)).map(_.getAs[Seq[String]](0))
+ val minDf: Long = if ($(minDF) >= 1.0) {
+ $(minDF).toLong
--- End diff --
If a user set `minDF` to `5.5`, the expected `minDF` should be `6`. I don't
think the user would expect anything else. Btw, a simpler implementation is to
keep `minDf` as a `Double`:
~~~scala
val minDf = if ($(minDF) >= 1.0) {
$(minDF)
} else {
$(minDF) * input.cache().count()
}
~~~
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]