Here is a minimal example of what I'm talking about:
import numpy as np
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import TfidfVectorizer
data = fetch_20newsgroups()
x = data.data
vec = TfidfVectorizer(min_df=0.01, max_df=0.5)
mat = vec.fit_transform(x).astype('bool')
vec.set_params(binary=True)
print(np.array_equal(mat, vec.fit_transform(x)))
This prints False. What is the underlying difference between setting binary=True and setting all nonzero values to True?
EDIT: As answered by @juanpa.arrivillaga, TfidfVectorizer(binary=True) still does the inverse document frequency calculation. However, I also noticed CountVectorizer(binary=True) doesn't produce the same output as .astype('bool') either. Below is an example:
In [1]: import numpy as np
...: from sklearn.datasets import fetch_20newsgroups
...: from sklearn.feature_extraction.text import CountVectorizer
...:
...: data = fetch_20newsgroups()
...: x = data.data
...:
...: vec = CountVectorizer(min_df=0.01, max_df=0.5)
...: a = vec.fit_transform(x).astype('bool')
...:
...: vec.set_params(binary=True)
...: b = vec.fit_transform(x).astype('bool')
...: print(np.array_equal(a, b))
...:
False
In [2]: a
Out[2]:
<11314x2141 sparse matrix of type '<class 'numpy.bool_'>'
with 950068 stored elements in Compressed Sparse Row format>
In [3]: b
Out[3]:
<11314x2141 sparse matrix of type '<class 'numpy.bool_'>'
with 950068 stored elements in Compressed Sparse Row format>
The dimensions and dtype are the same, which leads me to believe the content of those matrices are different. Just by eyeballing the output of print(a) and print(b), they look the same.