【问题标题】:How can I fixed DeprecationWarning: Call to deprecated error如何修复 DeprecationWarning: Call to deprecated 错误
【发布时间】:2021-05-11 22:42:37
【问题描述】:

在使用 Word2Vec 时,我遇到了这样的错误:

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: Call to deprecated __getitem__ (方法将在 4.0.0 中移除,使用 self.wv。 getitem() 代替)。 这与 ipykernel 包是分开的,因此我们可以避免导入直到

为什么会出现此错误,我该如何解决?

import nltk
nltk.download('punkt')
sentences=sent_tokenize(text)
sentences
nltk.download('stopwords')
sentences_clean=[re.sub(r'[^\w\s]','',sentence.lower()) for sentence in sentences] #noktalama kaldır , küçült 
stop_words = stopwords.words('english')
sentence_tokens=[[words for words in sentence.split(' ') if words not in stop_words] for sentence in sentences_clean] #stop_words'leri kaldır.
sentence_tokens
#SÖZCÜK YERLEŞTİRME
w2v=Word2Vec(sentence_tokens,size=1,min_count=1,iter=1000)
sentence_embeddings=[[w2v[word][0] for word in words] for words in sentence_tokens]
max_len=max([len(tokens) for tokens in sentence_tokens]) #Bir cümlenin max uzunluğunu hesaplama
sentence_embeddings=[np.pad(embedding,(0,max_len-len(embedding)),'constant') for embedding in sentence_embeddings] #Padding işlemi.Bütün cümleleri aynı boyuta getirebilmke için yaplır
#print(sentence_embeddings) #Kelimelerin vektör uzayındaki halleri bulunur 

【问题讨论】:

    标签: nlp extract stanford-nlp word2vec summarization


    【解决方案1】:

    一般来说,您应该在问题中提供完整的错误消息以及调用堆栈(“traceback”)信息,以帮助准确突出显示触发错误的代码行。

    但是,我相信您的错误是由使用w2v[word] 的代码部分触发的。

    DeprecationWarning 通常意味着由于库代码设计的更改,不再推荐您的使用。虽然出现“弃用”警告,但代码可能仍然有效,但可能会在以后的版本中停止工作。

    就您的代码而言,在您使用的 Gensim 版本中,不再建议通过 Word2Vec 模型对象上的直接索引访问 ([…]-subscripting) 来访问词向量本身。相反,这些词向量现在被收集在一个子对象中,类型为 KeyedVectors,它保存在 Word2Vec 对象的 .wv 属性中。

    因此,将 w2v[word] 替换为 w2v.wv[word] 应该可以避免警告。 (而且,从gensim-4.0.0 及更高版本开始,代码可能完全需要工作。)

    【讨论】:

    • 我用你的回答解决了这个问题。谢谢:) :)
    猜你喜欢
    • 2022-06-30
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 2020-11-01
    • 2017-06-17
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多