Gensim update to W2vec:AttributeError: 'int' object has no attribute 'index'

Gensim update to W2vec:AttributeError: 'int' object has no attribute 'index'

下面是我的代码,下面是我遇到的错误,但我不明白为什么会这样。请分享您的想法:我在这里查看 https://github.com/RaRe-Technologies/gensim/wiki/Migrating-from-Gensim-3.x-to-4 但我无法弄清楚

word_vec_unpack_idx = [(word, idx.index) for word, idx in \
                   word_vec.wv.key_to_index.items()]
# unpacking vecs tpo create singulrized dataframe 
tokens, indexes = zip(*word_vec_unpack)

word_vec_df = pd.DataFrame(word_vec.wv.syn0[indexes, :], index=tokens)

tokenized_array = np.array(tokenized)
model_array = np.array([word_vec_df.loc[doc].mean(axis=0) for doc in tokenized_array])
model_df = pd.DataFrame(model_array)
# manually adding the label 
model_df["label"] = df_final["Classification"]

display(model_df.head())

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-64-8de619ecbc5b> in <module>
----> 1 word_vec_unpack = [(word, idx.index) for word, idx in \
      2                    word_vec.wv.key_to_index.items()]
      3 # unpacking vecs tpo create singulrized dataframe
      4 tokens, indexes = zip(*word_vec_unpack)
      5 

<ipython-input-64-8de619ecbc5b> in <listcomp>(.0)
----> 1 word_vec_unpack = [(word, idx.index) for word, idx in \
      2                    word_vec.wv.key_to_index.items()]
      3 # unpacking vecs tpo create singulrized dataframe
      4 tokens, indexes = zip(*word_vec_unpack)
      5 

AttributeError: 'int' object has no attribute 'index'

我分解了代码并删除了 idx 所以代码的第一部分是: 现在错误消失了。

word_vec_unpack = [(word, index) for word, index in \
                   word_vec.wv.key_to_index.items()]
# unpacking vecs tpo create singulrized dataframe 
tokens, indexes = zip(*word_vec_unpack)

现在代码的第二部分出现另一个错误

word_vec_df = pd.DataFrame(word_vec.wv.syn0[indexes, :], index=tokens)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-80-a185de0b1b16> in <module>
----> 1 word_vec_df = pd.DataFrame(word_vec.wv.syn0[indexes, :], index=tokens)

AttributeError: 'KeyedVectors' object has no attribute 'syn0'

尝试使用 word_vec.wv.vectors 而不是 word_vec.wv.syn0。那是保存原始向量的数组。

KeyedVectors 有一段时间没有真正的 syn0 但如果它在您的代码第一次制作时可能具有向后兼容性别名。)