Search code examples
pythongensimldatopic-modeling

Genism Module attribute error for wrappers


I am going to find the optimal number of topics for LDA. To do this, I used GENSIM as follows :

def compute_coherence_values(dictionary, corpus, texts, limit, start=2, step=3):
    coherence_values = []
    model_list = []
    for num_topics in range(start, limit, step):
        model = gensim.models.wrappers.LdaMallet(mallet_path, corpus=corpus, num_topics=num_topics, id2word=id2word)
        model_list.append(model)
        coherencemodel = CoherenceModel(model=model, texts=texts, dictionary=dictionary, coherence='c_v')
        coherence_values.append(coherencemodel.get_coherence())

    return model_list, coherence_values

    

But I have an attribute error: I used spyder.

AttributeError: module 'gensim.models' has no attribute 'wrappers'

Solution

  • The latest major Gensim release, 4.0, removed the wrappers of other library algorithms. Per the "Migrating from Gensim 3.x to 4" wiki page:

    15. Removed third party wrappers

    These wrappers of 3rd party libraries required too much effort. There were no volunteers to maintain and support them properly in Gensim.

    If your work depends on any of the modules below, feel free to copy it out of Gensim 3.8.3 (the last release where they appear), and extend & maintain the wrapper yourself.

    The removed submodules are:

    - gensim.models.wrappers.dtmmodel
    - gensim.models.wrappers.ldamallet
    - gensim.models.wrappers.ldavowpalwabbit
    - gensim.models.wrappers.varembed
    - gensim.models.wrappers.wordrank
    - gensim.sklearn_api.atmodel
    - gensim.sklearn_api.d2vmodel
    - gensim.sklearn_api.ftmodel
    - gensim.sklearn_api.hdp
    - gensim.sklearn_api.ldamodel
    - gensim.sklearn_api.ldaseqmodel
    - gensim.sklearn_api.lsimodel
    - gensim.sklearn_api.phrases
    - gensim.sklearn_api.rpmodel
    - gensim.sklearn_api.text2bow
    - gensim.sklearn_api.tfidf
    - gensim.sklearn_api.w2vmodel
    - gensim.viz
    

    If you desperately needed the old support, you could also consider installing & using the older Gensim. (For example, via pip, pip install gensim==3.8.3.) But in general, the latest version will be best-supported.