Word2Vec dont_match函数引发Numpy警告

时间:2019-06-14 07:59:36

标签: python numpy word2vec

我正在使用word2vec。当我使用nott_match函数时,它显示警告。谁能帮忙:

venv / lib / python3.6 / site-packages / gensim / models / keyedvectors.py:876:FutureWarning:要堆栈的数组必须作为“序列”类型传递,例如列表或元组。从NumPy 1.16开始,不支持诸如生成器之类的非序列可迭代对象,将来会引发错误。   vectors = vstack(self.word_vec(word,use_norm = True)for used_words中的单词).astype(REAL)

代码:

if len(words) > 1:
    print(type(words))
    test = model.wv.doesnt_match(words)
    return test
else:
    return words

2 个答案:

答案 0 :(得分:0)

vstack()应该以列表作为参数 因此,请更改以下代码以避开FutureWarning

from
vectors = vstack(self.word_vec(word, use_norm=True) for word in used_words).astype(REAL)
to
vectors = vstack(list(self.word_vec(word, use_norm=True) for word in used_words)).astype(REAL)

答案 1 :(得分:0)

这是用于抑制 FutureWarning 的上下文管理器。希望他们能在它坏掉之前在 gensim 包中修复它。

import warnings
with warnings.catch_warnings():
    warnings.simplefilter(action='ignore', category=FutureWarning)
    w2v_model.wv.doesnt_match(['the', 'and', 'or'])

# Output: 'or'

w2v_model.wv.doesnt_match(['the', 'and', 'or'])

# Output: 
# C:\Installs\Python\lib\site-packages\gensim\models\keyedvectors.py:877: 
# FutureWarning: arrays to stack must be passed as a "sequence" type such as list or 
# tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 
# 1.16 and will raise an error in the future.
# vectors = vstack(self.word_vec(word, use_norm=True) for word in used_words).astype(REAL)
# or