m0_69339309 2023-12-04 19:50 采纳率: 20%
浏览 40
已结题

TypeError: Make sure that the iterable only contains strings.

为什么用bertopic建模时会出现这样的错误,
needed是excel里的文本数据

img


TypeError Traceback (most recent call last)
Cell In[20], line 1
----> 1 topics,probabilities=topic_model.fit_transform(needed)

File ~\AppData\Roaming\Python\Python310\site-packages\bertopic_bertopic.py:360, in BERTopic.fit_transform(self, documents, embeddings, images, y)

359 if documents is not None:

--> 360 check_documents_type(documents)
361 check_embeddings_shape(embeddings, documents)
363 doc_ids = range(len(documents)) if documents is not None else range(len(images))

File ~\AppData\Roaming\Python\Python310\site-packages\bertopic_utils.py:37, in check_documents_type(documents)
35 if isinstance(documents, Iterable) and not isinstance(documents, str):
36 if not any([isinstance(doc, str) for doc in documents]):
---> 37 raise TypeError("Make sure that the iterable only contains strings.")
39 else:
40 raise TypeError("Make sure that the documents variable is an iterable containing strings only.")

TypeError: Make sure that the iterable only contains strings.

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-12-04 19:53
    关注

    引自强大而免费的微信小程序:皆我百晓生

    这个错误是因为在使用bertopic进行建模时,传入的文本数据类型不正确。错误提示说要确保传入的迭代对象(iterable)只包含字符串。

    根据你提供的代码和错误信息,可以看出 needed 是需要进行建模的文本数据。你需要确保 needed 只包含字符串类型的元素。

    你可以尝试在使用 needed 前检查数据类型,并对其进行处理,例如将其它类型转换为字符串类型。可以使用以下代码来检查并转换数据类型:

    needed = [str(data) for data in needed]
    

    这将确保 needed 中的所有元素都是字符串类型。然后,再将其传递给 bertopic.fit_transform() 方法进行建模。

    希望能帮到你!

    评论 编辑记录

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月4日
  • 创建了问题 12月4日

悬赏问题

  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?
  • ¥15 metamask如何添加TRON自定义网络