Skip to content

Commit d0fc60d

Browse files
committed
Merge pull request scikit-learn#3184 from YS-L/tfidfvectorizer_idf
FIX TfidfVectorizer exports idf_ attribute
2 parents 62e4975 + 92add1d commit d0fc60d

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

sklearn/feature_extraction/tests/test_text.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,3 +877,9 @@ def test_tfidfvectorizer_binary():
877877
assert_array_equal(X.ravel(), [1, 1, 1, 0])
878878
X2 = v.transform(['hello world', 'hello hello']).toarray()
879879
assert_array_equal(X2.ravel(), [1, 1, 1, 0])
880+
881+
882+
def test_tfidfvectorizer_export_idf():
883+
vect = TfidfVectorizer(use_idf=True)
884+
vect.fit(JUNK_FOOD_DOCS)
885+
assert_array_almost_equal(vect.idf_, vect._tfidf.idf_)

sklearn/feature_extraction/text.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,12 @@ class TfidfVectorizer(CountVectorizer):
11451145
sublinear_tf : boolean, optional
11461146
Apply sublinear tf scaling, i.e. replace tf with 1 + log(tf).
11471147
1148+
Attributes
1149+
----------
1150+
``idf_`` : array, shape = [n_features], or None
1151+
The learned idf vector (global term weights)
1152+
when ``use_idf`` is set to True, None otherwise.
1153+
11481154
See also
11491155
--------
11501156
CountVectorizer
@@ -1216,6 +1222,10 @@ def sublinear_tf(self):
12161222
def sublinear_tf(self, value):
12171223
self._tfidf.sublinear_tf = value
12181224

1225+
@property
1226+
def idf_(self):
1227+
return self._tfidf.idf_
1228+
12191229
def fit(self, raw_documents, y=None):
12201230
"""Learn a conversion law from documents to array data"""
12211231
X = super(TfidfVectorizer, self).fit_transform(raw_documents)

0 commit comments

Comments
 (0)