cannot import name 'haversine_distances' from 'sklearn.metrics.pairwise' - python

import pysal as ps
I'm tring to import pysal but I get the following:
cannot import name 'haversine_distances' from 'sklearn.metrics.pairwise'
So I tried:
from sklearn.metrics.pairwise import haversine_distances
and I get the same message.
Any suggestions?

The problem may be that your version of scikit-learn is out of date. Try uninstalling and reinstalling scikit-learn from the terminal like so:
conda uninstall scikit-learn
Confirm, and wait for the packages to be uninstalled. Then, do conda install scikit-learn and conda install pysal to reinstall the packages. You'll also need to reinstall any other packages that rely on scikit-learn as well.
I had the same issue, and this fixed the problem for me.

About scikit learn's Haversine, you have to update your scikit-learn to latest version. If you check on scikitlearn website you will find out that this module is implemented in version 0.22.1:
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.haversine_distances.html

Related

Error: cannot import name 'SpearmanRConstantInputWarning' from 'scipy.stats'

I'm getting an error when importing the skbio package on Google Colab. The error message is related to SpearmanRConstantInputWarning of the scipy.stats package. What should I do to solve this problem?
I've tried to uninstall and install skbio and scipy, but it has not worked.
Seems to be some issue with the version. If you run
pip install scikit-bio==0.5.6
it shouldn't show that problem, at least it worked when I tried for 0.5.6 and 0.5.5 in Colab.
I had a similar problem, but could not downgrade to scikit-bio==0.5.6 as it conflicted with my Python version (3.10). An alternative workaround is downgrading scipy:
conda install -c conda-forge scipy=1.8
Presumably which package to downgrade will depend on your circumstances.
(Would have added as a comment to 3991santiago's answer but I do not have the reputation)

Fail to import MPLClassifier from SKlearn

I am trying to run MPL Neural Network using the scikit-learn library running on Jupyter Notebook. However, I have been trying to import the MPLClassifier but failed. I also have tried solution from here, SKlearn import MLPClassifier fails. I followed the steps and installed everything but could not import the module from SKlearn which are,
Installed the SKlearn using pip install scikit-neuralnetwork
Installed the mingw package using conda install mingw libpython and conda install -c free mingw, as the package is not available when using https://jmeubank.github.io/tdm-gcc/
Checked its availability using conda list
It is giving the following error,
cannot import name 'MPLClassifier' from 'sklearn.neural_network'
May I get help on other alternatives to import the module?

sklearn.feature_selection.mutual_info_regression not found

I have been trying to utilise mutual_info_regression method from sklearn, I have updated sklearn to latest build which is 0.24.1 and when I checked the source code inside my conda env path there is folder and files for feature_selection.mutual_info_regression, but when I try to import it in my Jupiter notebook it throws this error ImportError: cannot import name 'mutual_info_regression' from 'sklearn.model_selection' (/opt/anaconda3/envs/<my_env>/lib/python3.8/site-packages/sklearn/model_selection/__init__.py)
I tried restarting kernel as well, but it is still not working, has anyone else faced this issue? Im using macOS 11.2.1 and conda 4.8.3 with Python3
Thanks
I found the solution,
I just had to restart my terminal and then it started working for some reason.
I hope this helps anyone facing such problem in future
Thanks SO!
import sklearn
print(sklearn.__version__)
Check your sklearn version sklearn.model_selection is only available for version 0.18.1
Then try this in Jupyter Notebook cell
from sklearn.feature_selection import mutual_info_regression
If any of the above doesn't work try these three steps
1- pip uninstall sklearn
2- pip uninstall scikit-learn
3- pip install sklearn

XGBoostError: sklearn needs to be installed in order to use this module ( GCP Datalab)

I am trying to use Xgboost in GCP datalab. I have already installed sklearn but I keep getting the error :
" XGBoostError: sklearn needs to be installed in order to use this
module"
Below is the code I used:
import sklearn
!pip3 install xgboost
from xgboost.sklearn import XGBClassifier
model = XGBClassifier()
I have tried using Python v 2.7 instead, but no luck...does anyone know how to solve this issue in GCP Datalab?
I also faced the same issue, on python 3.7 32bit on ipython.
Solution: Uninstall the xgboost package by pip uninstall xgboost on terminal/cmd. Cross-check on the your console if you cannot import it. Now again install xgboost pip install xgboost or pip install xgboost-0.81-cp37-cp37m-win32.whl, given that you have already installed sklearn, it will work on newer console session.
xgboost wheel Link: https://pypi.org/project/xgboost/#files
I got the same error with a more complicated project, after releasing a new version suddenly it failed.
luckily in my case, I had docker images for each version, and was able to use pip freeze to see what changed.
In both version I used xgboost==0.81
In the version that worked I had scikit-learn==0.21.3 and in the new version it was scikit-learn==0.22
surprisingly enough, that's now what caused the issue. I've tried to uninstall xgboost like it was suggested here and reverted scikit-learn to the version is was originally on, and still no luck.
what did cause the issue was an update of numpy from 1.17.4 to 1.18.0.
reverting it solved it for me (not sure why)
this was python 3.6 on ubuntu
For me un- then re-installing first sklearn and then xgboost did the trick

ImportError: No module named naive_bayes

I just installed sklearn, my program runs no problem when I import it into the code. However, whenever I try to access the naive_bayes module, I get this error:
ImportError: No module named naive_bayes
Here's how I'm importing it:
from sklearn.naive_bayes import GaussianNB
Not sure where I'm going wrong, any help is much appreciated!
In the spirit of "turn it off, and turn it back on again" solutions, and given the fact that you're getting a Module has no attribute: __version__ when you try and print the scikit-learn version (which should be defined in any self-respecting Python module), I'm going to recommend you uninstall and reinstall scikit-learn:
pip uninstall sklearn
pip install sklearn
Run the following commands, and compare to the output provided, to make sure the package is behaving the way it should:
>>> import sklearn
>>> print(sklearn.__version__)
0.19.0
>>> print(sklearn.__file__)
/usr/local/lib/python3.6/site-packages/sklearn/__init__.py
>>>
The output from printing sklearn.__file__ does not need to match exactly, but it should at least print some location on your computer.
Also, check to make sure your pip matches your python. This depends on what platform you're on, but this mix of version 2 and version 3 sometimes creates a nasty cocktail. Executing pip --version should tell you which version of Python it is tied to.
$ which pip3
/usr/local/bin/pip3
$ pip3 --version
pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6)
$ which python3
/usr/local/bin/python3
You should be OK with Python version 2, so long as things match.
(Soap box moment: move to Python 3 if you don't have a really good reason for sticking with Python 2!)
It seems that the sklearn installation does not include the naive_bayes in your installation. If the sklearn install correctly, it would say something like
ImportError: No module named 'sklearn.naive_bayes2'
However, in your case, the import error shows a bit different output. Please check the version and run the test file.
I had the same problem while installing sklearn and scikit-learn through pip.
I fixed the issue through the following steps
pip uninstall sklearn (if already installed)
pip uninstall scikit-learn( if already installed)
git clone scikit-learn
cd scikit-learn
python setup.py install
Hope this will help you.

Categories