How can I fixed DeprecationWarning: Call to deprecated error

Viewed 632

While working with Word2Vec, I got an error like this :

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:3: DeprecationWarning: Call to deprecated __getitem__ (Method will be removed in 4.0.0, use self.wv.getitem() instead). This is separate from the ipykernel package so we can avoid doing imports until

Why am I getting this error and how can I fix it?

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 
1 Answers

In general, you should provide in your question the full error message, with call stack ("traceback") info, to help highlight exactly which line of code has triggered the error.

But, I believe your error is being triggered by the part of your code that uses w2v[word].

A DeprecationWarning typically means your usage is no-longer-recommended because of changes in the library code's design. While the 'deprecation' warning occurs, the code likely still works, but may stop working in a later release.

In the case of your code, in the version of Gensim you're using, it's no-longer-recommended to access word-vectors by direct indexed-access ([…]-subscripting) on the Word2Vec model object itself. Instead, those word-vectors are now collected in a subsidiary object, of type KeyedVectors, that's kept in the Word2Vec object's .wv property.

So, replacing w2v[word] with w2v.wv[word] should avoid the warning. (And, is likely necessary for the code to work at all, starting in gensim-4.0.0 & higher versions.)

Related