BERTopic: ImportError: cannot import name 'UMAP' from 'umap' (unknown location)
I tried tutorial at bertopic website and this is only my code
from sentence_transformers import SentenceTransformer
from umap import UMAP
from hdbscan import HDBSCAN
from sklearn.feature_extraction.text import CountVectorizer
from bertopic.vectorizers import ClassTfidfTransformer
from bertopic import BERTopic
Every packages already installed separately especially the ‘umap-learn’ But got an error
ImportError Traceback (most recent call last)
Cell In [20], line 1
----> 1 from umap import UMAP
2 from hdbscan import HDBSCAN
3 from sklearn.feature_extraction.text import CountVectorizer
ImportError: cannot import name 'UMAP' from 'umap' (unknown location)
Environment Info Pycharm 2021.2.3 Python 3.8
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (5 by maintainers)
My workaround for this is as follows:
I used this hint: https://github.com/resemble-ai/Resemblyzer/issues/6#issuecomment-701387312
Use import umap.umap_ as UMAP instead of from umap import UMAP
Then I had to change the UMAP to UMAP.UMAP in my code and in all pieces of code that came with bertopic:
umap_model = UMAP.UMAP(n_neighbors=15, n_components=5, min_dist=0.0, metric=‘cosine’)
Instead of:
umap_model = UMAP(n_neighbors=15, n_components=5, min_dist=0.0, metric=‘cosine’)
I found a solution at least for my case: despite the fact that I uninstalled
umap-learn
, there was still a umap folder in the package directory ( c:\python310\lib\site-packages ). I manually deleted the umap folder and later I just installed the bertopic. It works now!For anyone stuck like me, I had to manually replace all
from umap import UMAP
and replace it withfrom umap.umap_ import UMAP
in the bertopic python files.umap version: umap-learn==0.5.3 bertopic==0.15.0
python 3.9