pandas - SentenceTransformers 在 pandas 系列上抛出 KeyError

标签 pandas dataframe sentence-transformers

我使用以下简化代码:

from sentence_transformers import SentenceTransformer
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')

embeddings = model.encode(sentences)

其中sentences是一个pandas Series,包含我想要转换的句子。

然后我收到以下错误回溯

embeddings = model.encode(sentences)
File "/anaconda/envs/topics/lib/python3.8/site-packages/sentence_transformers/SentenceTransformer.py", line 157, in encode
sentences_sorted = [sentences[idx] for idx in length_sorted_idx]
File "/anaconda/envs/topics/lib/python3.8/site-packages/sentence_transformers/SentenceTransformer.py", line 157, in <listcomp>
sentences_sorted = [sentences[idx] for idx in length_sorted_idx]
File "/anaconda/envs/topics/lib/python3.8/site-packages/pandas/core/series.py", line 942, in 
__getitem__
return self._get_value(key)
File "/anaconda/envs/topics/lib/python3.8/site-packages/pandas/core/series.py", line 1051, in 
_get_value
loc = self.index.get_loc(label)
File "/anaconda/envs/topics/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3363, in get_loc
raise KeyError(key) from err
KeyError: 144

最佳答案

实际的解决方案是将 pandas Series 转换为 numpy 数组:

sentences_array = sentences.to_numpy()

关于pandas - SentenceTransformers 在 pandas 系列上抛出 KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73154063/

相关文章:

python - 将列名转换为python中的变量

python - 在 pandas df 中查找 timedelta 对象的均值和标准差

python - 如何在此 pandas 数据框上正确使用数据透视表?

python - 从 Pandas 中给定的单元格值获取行

R如何有选择地对数据框进行排序?

python - 如何将 R 数据帧导入 Pandas?

bert-language-model - ReadError : file could not be opened successfully. 但我不确定 tar 文件存储在哪里来解决此问题

python - 如何在数据框上应用散点图和折线图进行比特币情绪分析?