AttributeError: 'KMeans' object has no attribute '_n_threads'

Hello,

I created a KMeans model on some dataset, pickled it and then loaded it in the Streamlit app that I made. When I try to use this model in my app I get the following error:

AttributeError: ‘KMeans’ object has no attribute ‘_n_threads’

When I searched this issue online I reached this stackoverflow question: python - 'KMeans' object has no attribute '_n_threads' - Stack Overflow, which made me realize this error might happen because of a difference between sklearn versions. The sklearn version on my machine is 0.22.1 whereas the sklearn that I added to the requirements.txt file for Streamlit isn’t pinned to a specific version. So I tried pinning it down to 0.22.1 but then I got this error:

ERROR: Could not find a version that satisfies the requirement sklearn==0.22.1 (from versions: 0.0)
ERROR: No matching distribution found for sklearn==0.22.1
[manager] installer returned a non-zero exit code

I’ve also previously had ‘n_jobs=-1’ as a parameter in the pickled KMeans object and I know they mention in the documentation of sklearn.kmeans that this feature is deprecated in the newer version of sklearn. But since then I’ve tried to re-train my model without using n_jobs, pickled it, uploaded it to my Github, but I still get the same error above.

How do I solve it?

My Github where the app can be found: data_science/mushroom_project at main · idansul/data_science · GitHub
My Streamlit app: https://share.streamlit.io/idansul/data_science/main/mushroom_project/web_app.py

Hi @Idan, welcome to the Streamlit community!! :tada: :partying_face:

While you import sklearn in your scripts, the module’s name on PyPi is actually scikit-learn. It’s really not obvious and a common mistake.

You can fix it by replacing sklearn with scikit-learn or the specific version scikit-learn==0.22.1 in your requirements.txt file. Let us know if this helps!

Happy Streamlit-ing! :balloon:
Snehan

2 Likes

I followed your suggestion and it works now. Thank you!

2 Likes

Thank! This worked, one question, does this mean that scikit-learn will no longer support this?