You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
CountVectorizer(lowercase=True,strip_accents='unicode') may produce vocab that contains uppercase chars #21207
Some characters are transformed to uppercase chars (eg ™ -> TM), despite lowercase=True.
This then gives warning messages "UserWarning: Upper case characters found in vocabulary while 'lowercase' is True"
(OK, there are worse things happening in the world ;-). Thanks for scikit-learn)
Steps/Code to Reproduce
fromsklearn.feature_extraction.textimportCountVectorizerx= ['This is Problematic™.','THIS IS NOT']
cv=CountVectorizer(
lowercase=True,
strip_accents='unicode',
ngram_range=(1,1)
)
x_v=cv.fit_transform(x)
print(cv.get_feature_names_out()) # Contains "problematicTM"# then you can get a warning# "UserWarning: Upper case characters found in vocabulary while 'lowercase' is True"# if you both create a classifier AND run cv.fit_transform# (no message if you only call one)fromsklearn.svmimportLinearSVCy= [1,0]
xtest= ['This is not']
ytest= [0]
# comment either of the 2 following lines, no warning message displayedclf=LinearSVC(random_state=42).fit(x_v, y)
xtest_v=cv.transform(xtest)
Expected Results
print: ['is' 'not' 'problematictm' 'this']
Actual Results
print: ['is' 'not' 'problematicTM' 'this']
warning message:
/Users/fps/_fps/DeveloperTools/virtualenvs/fps_env/lib/python3.9/site-packages/sklearn/feature_extraction/text.py:1208: UserWarning: Upper case characters found in vocabulary while 'lowercase' is True. These entries will not be matched with any documents
warnings.warn(
Versions
/Users/fps/_fps/DeveloperTools/virtualenvs/fps_env/lib/python3.9/site-packages/setuptools/distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
warnings.warn(
changed the title [-]CountVectorizer(lowercase=True,strip_accents='unicode') may produces vocab that contains uppercase chars[/-][+]CountVectorizer(lowercase=True,strip_accents='unicode') may produce vocab that contains uppercase chars[/+]on Sep 30, 2021
I think that recently we don't raise the warning anymore with the code above. However, we still have the bug described.
I think that we should be keeping backward compatibility.
@thomasjpfan do you think that instead of adding lower_after_strip_accents we could only have a generic keyword behaviour="legacy"/"new" that we could use to deprecate the legacy mode and mention to people to lower the internal dictionary if they still want to reuse the transformer?
Activity
[-]CountVectorizer(lowercase=True,strip_accents='unicode') may produces vocab that contains uppercase chars[/-][+]CountVectorizer(lowercase=True,strip_accents='unicode') may produce vocab that contains uppercase chars[/+]thomasjpfan commentedon Oct 4, 2021
This comes down to the order we preprocess text in
CountVectorizer:scikit-learn/sklearn/feature_extraction/text.py
Lines 70 to 74 in 23afd5d
The accent function is applied after the
lower. I see a two ways forward:strip_accents="unicode". This also breaks backward compatibility because the text will be different.lower_after_strip_accentsparameter, which adds another parameter toTfid*and friends.I am slightly more in favor of 2 since of BC concerns.
glemaitre commentedon Dec 17, 2021
I think that recently we don't raise the warning anymore with the code above. However, we still have the bug described.
I think that we should be keeping backward compatibility.
@thomasjpfan do you think that instead of adding
lower_after_strip_accentswe could only have a generic keywordbehaviour="legacy"/"new"that we could use to deprecate the legacy mode and mention to people to lower the internal dictionary if they still want to reuse the transformer?thomasjpfan commentedon Dec 19, 2021
Would the new behavior be to error if the internal dictionary has upper case characters?
glemaitre commentedon Dec 20, 2021
I was thinking that the new behaviour would implement solution 1.
Merge pull request #6 from trinhcon/dev