【问题标题】:Error related to 'list' object with no attribute 'lower'与没有属性“lower”的“list”对象相关的错误
【发布时间】:2019-12-16 09:21:39
【问题描述】:

我正在做多个集群项目。在我当前的集群项目中,我收到与 tfidf_vectorizer 代码相关的错误。

这是我导入的文件:

description_1 = open('description1.txt', 
encoding="utf8").read().lower().split('\n')
description_2 = open('description2.txt', 
encoding="utf8").read().lower().split('\n')
description_3 = open('description3.txt', 
encoding="utf8").read().lower().split('\n')
description_4 = open('description4.txt', 
encoding="utf8").read().lower().split('\n')
description_5 = open('description5.txt', 
encoding="utf8").read().lower().split('\n')
description_6 = open('description6.txt', 
encoding="utf8").read().lower().split('\n')
description_7 = open('description7.txt', 
encoding="utf8").read().lower().split('\n')

然后我合并了文件:

descriptions_on = (description_1, description_2, description_3, 
description_4, description_5, description_6, description_7)

descriptions = []

for i in range(len(descriptions_on)):
    item = descriptions_on[i]
    descriptions.append(item)

问题出在这几行代码中

from sklearn.feature_extraction.text import TfidfVectorizer
from spacy.lang.fr.stop_words import STOP_WORDS as fr_stop
from spacy.lang.en.stop_words import STOP_WORDS as en_stop
#from warnings import filterwarnings
#filterwarnings('ignore')

final_stopwords_list = list(fr_stop) + list(en_stop)

tfidf_vectorizer = TfidfVectorizer(max_df=0.90, max_features=200000,
                             min_df=0.10, stop_words=final_stopwords_list,
                             use_idf=True, tokenizer=tokenize_and_stem, 
ngram_range=(1,3))


%time tfidf_matrix = tfidf_vectorizer.fit_transform(descriptions)

tokenizer=tokenize_and_stem 与已创建的函数“tokenize_and_stem”相关,但未包含在此问题的代码列表中,因为它是不恰当的。

这是我从上面的代码中得到的错误消息:

AttributeError                            Traceback (most recent call 
last)
<timed exec> in <module>

D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py in 
fit_transform(self, raw_documents, y)
   1611         """
   1612         self._check_params()
->     1613         X = super(TfidfVectorizer, 
self).fit_transform(raw_documents)
   1614         self._tfidf.fit(X)
   1615         # X is already a transformed view of raw_documents so

 D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py in 
fit_transform(self, raw_documents, y)
   1029 
   1030         vocabulary, X = self._count_vocab(raw_documents,
 ->1031                                           self.fixed_vocabulary_)
   1032 
   1033         if self.binary:

D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py in 
_count_vocab(self, raw_documents, fixed_vocab)
    941         for doc in raw_documents:
    942             feature_counter = {}
--> 943             for feature in analyze(doc):
    944                 try:
    945                     feature_idx = vocabulary[feature]

D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py in 
<lambda>(doc)
    327                                                tokenize)
    328             return lambda doc: self._word_ngrams(
--> 329                 tokenize(preprocess(self.decode(doc))), 
stop_words)
    330 
    331         else:

 D:\Anaconda\lib\site-packages\sklearn\feature_extraction\text.py in 
<lambda>(x)
    255 
    256         if self.lowercase:
--> 257             return lambda x: strip_accents(x.lower())
    258         else:
    259             return strip_accents

AttributeError: 'list' object has no attribute 'lower'

显然,我希望代码可以运行而不会出现任何错误。我已经尝试了多种方法来尝试解决这个问题。我什至阅读了 6 到 7 篇在堆栈上有相同问题的帖子,但每个帖子在某种程度上都与我的不同......

任何帮助将不胜感激..谢谢!

编辑:

print(descriptions)

结果:

[['\ufeff于 1991 年在蒙特利尔成立。dex 是负担得起的奢侈品的真正先驱。', 'sashana sanderson 的客座帖子,位于大学内的加勒比媒体与传播学院 (carimac) 新闻系二年级学生西印度群岛...', 'diamentis 为个性化医疗提供了一种简单可靠的解决方案,而且更加精确,而且 diamentis 正在开发精神卫生保健领域的第一个诊断工具,这将使临床医生更加...', 'didacte授权培训供应商。在几分钟内在基于云的门户上创建在线课程。”、“di-o-matic:发现您最喜欢的 cg 角色背后的技术”、“proship facilite le recrutement des Talents et la preparation du spectacle qui éblouira vos invités, tout en assurant la gestion de la totalité des problèmes administratifs et contractuels.', 'dk-spec participe au congrès de montréal sur le bois, du 20 au 22 mars, au fairmont le reine élisabeth。 venez nous rencontrer!', 'do networks limited 旨在成为具有丰富光通信线路专业知识的企业客户的一站式服务供应商,do networks limited 整个团队致力于成为......', "douglas advisors inc. douglas advisors est une firme d'ingénieurs-conseils fonée en 1999 spécialisée en structure de bâtiments.", 'cooper 目前是 dream、dream office reit、dream global reit、dream Industrial reit 和 el Financial Corporation Limited 的董事会成员。小姐。页。 jane gavan 是 Dream 资产管理部门的总裁,在房地产行业拥有 30 多年的经验。', "dromadaire géo-innovations est une Firme spécialisée en géomatique.au service de l'environnement, l'entreprise use la géo-localisation pour faire l ...", "dubo électrique ltée est l'expert-conseil spécialiste des produits électriques, de l'éclairage, d'automatisation et de contrôle au Québec, c'est pourquoi traiter avec dubo électrique ltée comporte ...", '邓迪可持续技术 (dst) 致力于开发和商业化用于处理材料的环境友好型技术

【问题讨论】:

  • 您可以尝试打印descriptions 并向我们展示样品吗?
  • @UpasanaMittal 绝对,我已经编辑了帖子......结果是打印(描述)的一部分。完整的输出内容太长,无法发布。
  • 看起来这是列表列表而不是文本列表,并且 tfidf 期待文本列表
  • 我基本上有7个文档,就像文章开头贴的一样,每个有100行文字。
  • 如果将行转换为该文档的段落可以吗?

标签: python-3.x cluster-analysis attributeerror tfidfvectorizer


【解决方案1】:

您的文档是lists 行,而不是字符串。

因此它们没有适合标记器的格式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-19
    • 2020-08-21
    • 2016-01-30
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-14
    相关资源
    最近更新 更多