Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,21 @@ private[feature] trait CountVectorizerParams extends Params with HasInputCol wit
def getMinDF: Double = $(minDF)

/**
* Specifies the maximum number of different documents a term must appear in to be included
* in the vocabulary.
* If this is an integer greater than or equal to 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.
* Specifies the maximum number of different documents a term could appear in to be included
* in the vocabulary. A term that appears more than the threshold will be ignored. If this is an
* integer greater than or equal to 1, this specifies the maximum number of documents the term
* could appear in; if this is a double in [0,1), then this specifies the maximum fraction of
* documents the term could appear in.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srowen do these doc changes look ok to you? It was a little confusing before saying that the term "must appear" when it's a max value.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, your wording is clearer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @srowen !

*
* Default: (2^64^) - 1
* Default: (2^63^) - 1
* @group param
*/
val maxDF: DoubleParam = new DoubleParam(this, "maxDF", "Specifies the maximum 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.",
" different documents a term could appear in to be included in the vocabulary." +
" A term that appears more than the threshold will be ignored. If this is an integer >= 1," +
" this specifies the maximum number of documents the term could appear in;" +
" if this is a double in [0,1), then this specifies the maximum fraction of" +
" documents the term could appear in.",
ParamValidators.gtEq(0.0))

/** @group getParam */
Expand Down
40 changes: 31 additions & 9 deletions python/pyspark/ml/feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ class _CountVectorizerParams(JavaParams, HasInputCol, HasOutputCol):
" 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.0", typeConverter=TypeConverters.toFloat)
maxDF = Param(
Params._dummy(), "maxDF", "Specifies the maximum number of" +
" different documents a term could appear in to be included in the vocabulary." +
" A term that appears more than the threshold will be ignored. If this is an" +
" integer >= 1, this specifies the maximum number of documents the term could appear in;" +
" if this is a double in [0,1), then this specifies the maximum" +
" fraction of documents the term could appear in." +
" Default (2^63) - 1", typeConverter=TypeConverters.toFloat)
vocabSize = Param(
Params._dummy(), "vocabSize", "max size of the vocabulary. Default 1 << 18.",
typeConverter=TypeConverters.toInt)
Expand All @@ -433,7 +441,7 @@ class _CountVectorizerParams(JavaParams, HasInputCol, HasOutputCol):

def __init__(self, *args):
super(_CountVectorizerParams, self).__init__(*args)
self._setDefault(minTF=1.0, minDF=1.0, vocabSize=1 << 18, binary=False)
self._setDefault(minTF=1.0, minDF=1.0, maxDF=2 ** 63 - 1, vocabSize=1 << 18, binary=False)

@since("1.6.0")
def getMinTF(self):
Expand All @@ -449,6 +457,13 @@ def getMinDF(self):
"""
return self.getOrDefault(self.minDF)

@since("2.4.0")
def getMaxDF(self):
"""
Gets the value of maxDF or its default value.
"""
return self.getOrDefault(self.maxDF)

@since("1.6.0")
def getVocabSize(self):
"""
Expand Down Expand Up @@ -513,11 +528,11 @@ class CountVectorizer(JavaEstimator, _CountVectorizerParams, JavaMLReadable, Jav
"""

@keyword_only
def __init__(self, minTF=1.0, minDF=1.0, vocabSize=1 << 18, binary=False, inputCol=None,
outputCol=None):
def __init__(self, minTF=1.0, minDF=1.0, maxDF=2 ** 63 - 1, vocabSize=1 << 18, binary=False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not crazy about hardcoding a value here since in Scala it is Long.MaxValue, but I'm not sure there is another way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for the comments. Will make changes.

inputCol=None, outputCol=None):
"""
__init__(self, minTF=1.0, minDF=1.0, vocabSize=1 << 18, binary=False, inputCol=None,\
outputCol=None)
__init__(self, minTF=1.0, minDF=1.0, maxDF=2 ** 63 - 1, vocabSize=1 << 18, binary=False,\
inputCol=None,outputCol=None)
"""
super(CountVectorizer, self).__init__()
self._java_obj = self._new_java_obj("org.apache.spark.ml.feature.CountVectorizer",
Expand All @@ -527,11 +542,11 @@ def __init__(self, minTF=1.0, minDF=1.0, vocabSize=1 << 18, binary=False, inputC

@keyword_only
@since("1.6.0")
def setParams(self, minTF=1.0, minDF=1.0, vocabSize=1 << 18, binary=False, inputCol=None,
outputCol=None):
def setParams(self, minTF=1.0, minDF=1.0, maxDF=2 ** 63 - 1, vocabSize=1 << 18, binary=False,
inputCol=None, outputCol=None):
"""
setParams(self, minTF=1.0, minDF=1.0, vocabSize=1 << 18, binary=False, inputCol=None,\
outputCol=None)
setParams(self, minTF=1.0, minDF=1.0, maxDF=2 ** 63 - 1, vocabSize=1 << 18, binary=False,\
inputCol=None, outputCol=None)
Set the params for the CountVectorizer
"""
kwargs = self._input_kwargs
Expand All @@ -551,6 +566,13 @@ def setMinDF(self, value):
"""
return self._set(minDF=value)

@since("2.4.0")
def setMaxDF(self, value):
"""
Sets the value of :py:attr:`maxDF`.
"""
return self._set(maxDF=value)

@since("1.6.0")
def setVocabSize(self, value):
"""
Expand Down
25 changes: 25 additions & 0 deletions python/pyspark/ml/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,31 @@ def test_count_vectorizer_with_binary(self):
feature, expected = r
self.assertEqual(feature, expected)

def test_count_vectorizer_with_maxDF(self):
dataset = self.spark.createDataFrame([
(0, "a b c d".split(' '), SparseVector(3, {0: 1.0, 1: 1.0, 2: 1.0}),),
(1, "a b c".split(' '), SparseVector(3, {0: 1.0, 1: 1.0}),),
(2, "a b".split(' '), SparseVector(3, {0: 1.0}),),
(3, "a".split(' '), SparseVector(3, {}),)], ["id", "words", "expected"])
cv = CountVectorizer(inputCol="words", outputCol="features")
model1 = cv.setMaxDF(3).fit(dataset)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add an assert that the vocabulary is equal to something? I think it would be ['b', 'c' 'd']

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Bryan, Thanks for your comments. I will change these.

self.assertEqual(model1.vocabulary, ['b', 'c', 'd'])

transformedList1 = model1.transform(dataset).select("features", "expected").collect()

for r in transformedList1:
feature, expected = r
self.assertEqual(feature, expected)

model2 = cv.setMaxDF(0.75).fit(dataset)
self.assertEqual(model2.vocabulary, ['b', 'c', 'd'])

transformedList2 = model2.transform(dataset).select("features", "expected").collect()

for r in transformedList2:
feature, expected = r
self.assertEqual(feature, expected)

def test_count_vectorizer_from_vocab(self):
model = CountVectorizerModel.from_vocabulary(["a", "b", "c"], inputCol="words",
outputCol="features", minTF=2)
Expand Down