TF-IDF 引发 AttributeError:"int"对象没有属性"lower",即使数据中没有整数



我正在尝试使用sklearnTfidfVectorizer对数据集执行TF-IDF转换。

我收到以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-212-ded21402d527> in <module>
----> 1 noise_tf = tfidf.fit(noise_data)
~AppDataLocalContinuumanaconda3libsite-packagessklearnfeature_extractiontext.py in fit(self, raw_documents, y)
1629         """
1630         self._check_params()
-> 1631         X = super().fit_transform(raw_documents)
1632         self._tfidf.fit(X)
1633         return self
~AppDataLocalContinuumanaconda3libsite-packagessklearnfeature_extractiontext.py in fit_transform(self, raw_documents, y)
1056 
1057         vocabulary, X = self._count_vocab(raw_documents,
-> 1058                                           self.fixed_vocabulary_)
1059 
1060         if self.binary:
~AppDataLocalContinuumanaconda3libsite-packagessklearnfeature_extractiontext.py in _count_vocab(self, raw_documents, fixed_vocab)
968         for doc in raw_documents:
969             feature_counter = {}
--> 970             for feature in analyze(doc):
971                 try:
972                     feature_idx = vocabulary[feature]
~AppDataLocalContinuumanaconda3libsite-packagessklearnfeature_extractiontext.py in <lambda>(doc)
350                                                tokenize)
351             return lambda doc: self._word_ngrams(
--> 352                 tokenize(preprocess(self.decode(doc))), stop_words)
353 
354         else:
~AppDataLocalContinuumanaconda3libsite-packagessklearnfeature_extractiontext.py in <lambda>(x)
254 
255         if self.lowercase:
--> 256             return lambda x: strip_accents(x.lower())
257         else:
258             return strip_accents
AttributeError: 'int' object has no attribute 'lower'

我确保数据不包含任何ints:

>> noise_data.apply(lambda x: isinstance(x, int)).sum()
0

然后我只是:

tfidf = TfidfVectorizer(min_df = 5)
noise_tf = tfidf.fit(noise_data)

得到了上述误差。

由于数据中没有int,我希望此代码能够正常工作。

你知道TfidfVectorizer引发这个错误的原因是什么吗?

谢谢!

我找到了这个问题的答案,但由于我在StackOverflow上没有找到类似的主题,我决定把这个问题留给其他人。

TfidfVectorizer似乎也在考虑列名。

我检查了所有列名的类型,那里有int。我将所有列名都强制转换为string,这样就解决了问题。

我不知道为什么TfidfVectorizer试图将.lower()应用于列名,但很明显它确实这样做了。如果这里有人知道这个的目的是什么,我很想听。

最新更新