ImportError: cannot import name 'det_curve' from 'sklearn.metrics' - python

I don't know why I am getting this error, the official document reference
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.det_curve.html#sklearn.metrics.det_curve
Code:
import numpy as np
from sklearn.metrics import det_curve
fpr, fnr, thresholds = det_curve(y_test, y_pred)
print(fpr, fnr, thresholds)
Error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-46-d8d6f0b546ca> in <module>()
10
11 import numpy as np
---> 12 from sklearn.metrics import det_curve
13
14 fpr, fnr, thresholds = det_curve(y_test['cEXT'], y_pred)
ImportError: cannot import name 'det_curve' from 'sklearn.metrics' (/usr/local/lib/python3.7/dist-packages/sklearn/metrics/__init__.py)
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

Same kind of problem happened to me in my Jupyter notebook. I uninstall and re-install both scikit-learn and imblearn. It didn't work. Then restarting the kernel and running again solved the problem.

Looks like some issue with a missing package. Try the following:
pip uninstall -v scikit-learn
pip install -v scikit-learn
This might install the related dependencies along with it.

Related

Cannot import pycaret in google colab

I cant import pycaret in a google colab
Here are all the steps I had taken:
Change python version to 3.8
Installed pip
I then ran
!pip install pycaret
import pycaret
the install works, but then
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-27-fdea18e6876c> in <module>
1 get_ipython().system('pip install pycaret ')
----> 2 import pycaret
ModuleNotFoundError: No module named 'pycaret'
I must be doing something very wrong!
In troubleshooting I also pip installed numpy and pandas which both imported just fine
For importing use this, this is one is for classification:
from pycaret.classification import *
And for regression:
from pycaret.regression import *
For NLP:
from pycaret.nlp import *
!pip install pycaret
should work without any issues. I have used it multiple times on Google Colab. Alternately, you can use
pip install --pre pycaret

ModuleNotFoundError: No module named 'mglearn'

This is a commmon question, but nothing worked for me so far.
I have seen this answer too Python 3 on Mac : ModuleNotFoundError: No module named 'mglearn'
!pip install mglearn
!pip install sklearn
!pip install -U scikit-learn
import mglearn
import sklearn
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-fe22f30497b9> in <module>
----> 1 import mglearn
2 import sklearn
ModuleNotFoundError: No module named 'mglearn'
I checked other questions but failed to solve it. I have macOS Catalina 10.15.7
import sys
!{sys.executable} -m pip install mglearn
Try using this. This assures that you are using the pip version associated with the environment.

cannot import sklearn even though it is installed successfully

I have installed sklearn through pip successfully using this command:
$pip install -U scikit-learn
But I cannot import it.
import sklearn
Result:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>
----> 1 import sklearn
ModuleNotFoundError: No module named 'sklearn'
Please help
You might want to try installing scikit-learn: pip install scikit-learn
Try pip3 install sklearn.
This will install the libraries you need for python 3.

ModuleNotFoundError: No module named 'xgboost.sklearn'

I'm trying to import xgboost into jupyter-notebook but get the following error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-9-a585b270d0df> in <module>
1 import pandas as pd
2 import numpy as np
----> 3 import xgboost
~/.local/lib/python3.6/site-packages/xgboost/__init__.py in <module>
14 from . import tracker # noqa
15 from .tracker import RabitTracker # noqa
---> 16 from . import dask
17 try:
18 from .sklearn import XGBModel, XGBClassifier, XGBRegressor, XGBRanker
~/.local/lib/python3.6/site-packages/xgboost/dask.py in <module>
31 from .training import train as worker_train
32 from .tracker import RabitTracker
---> 33 from .sklearn import XGBModel, XGBClassifierBase, xgboost_model_doc
34
35 # Current status is considered as initial support, many features are
ModuleNotFoundError: No module named 'xgboost.sklearn'
I've downloaded sklearn as well as sci-kit learn and they work fine...
Any idea what the problem is?
You might need to install your packages properly.
For best practice, you'll need to use a conda environment. Check out how it works here: https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html
Once you have created your environment, activate it and then install all the packages you need. Presumably, you'll have to run the command:
conda install -c conda-forge xgboost
pip install -U scikit-learn
To install your machine learning packages.
XGBOOST is in xgboost module: It should be imported as:
`from xgboost import XGBRegressor`

ImportError: cannot import name 'factorial'

I want to use a logit model and trying to import statsmodels library.
My Version: Python 3.6.8
The best suggestion I got is to downgrade scipy but unclear how to and to what version should I downgrade. Please help how to resolve.
https://github.com/statsmodels/statsmodels/issues/5747
import statsmodels.formula.api as smf
ImportError Traceback (most recent call last)
<ipython-input-52-f897a2d817de> in <module>
----> 1 import statsmodels.formula.api as smf
~/anaconda3/envs/py36/lib/python3.6/site-packages/statsmodels/formula/api.py in <module>
13 from statsmodels.robust.robust_linear_model import RLM
14 rlm = RLM.from_formula
---> 15 from statsmodels.discrete.discrete_model import MNLogit
16 mnlogit = MNLogit.from_formula
17 from statsmodels.discrete.discrete_model import Logit
~/anaconda3/envs/py36/lib/python3.6/site-packages/statsmodels/discrete/discrete_model.py in <module>
43
44 from statsmodels.base.l1_slsqp import fit_l1_slsqp
---> 45 from statsmodels.distributions import genpoisson_p
46
47 try:
~/anaconda3/envs/py36/lib/python3.6/site-packages/statsmodels/distributions/__init__.py in <module>
1 from .empirical_distribution import ECDF, monotone_fn_inverter, StepFunction
----> 2 from .edgeworth import ExpandedNormal
3 from .discrete import genpoisson_p, zipoisson, zigenpoisson, zinegbin
~/anaconda3/envs/py36/lib/python3.6/site-packages/statsmodels/distributions/edgeworth.py in <module>
5 import numpy as np
6 from numpy.polynomial.hermite_e import HermiteE
----> 7 from scipy.misc import factorial
8 from scipy.stats import rv_continuous
9 import scipy.special as special
ImportError: cannot import name 'factorial'```
Update: upgrading statsmodels will fix this issue nowadays: pip install statsmodels --upgrade.
From this issue on statsmodels' github repo, the solution appears to be to downgrade SciPy to version 1.2 (current version is 1.3, which you appear to use).
At least for me, SciPy 1.2 has the factorial function in the scipy.misc package.
You can do
python3.6 -m pip install scipy==1.2 --upgrade
Use the --user option with that if you don't have standard install rights.
Perhaps you want to avoid using pip, since you're using Conda. You should be able to pin the version of scipy in Conda as well, but if you don't plan to add any other packages to your environment, just use the pip version.
Of course, downgrading SciPy may cause issues elsewhere, but that's hard to foresee, especially without knowing exactly what other packages and dependencies you have installed; you'll just have to find out. Fingers crossed for not ending up in dependency hell (as you've already on the doorstep).
For the more curious, scipy.misc.factorial has been deprecated since version 1.0; scipy.special.factorial should be used instead.
Importing in version 1.2 does not, however, show any clear warning, nor does using it. This might explain why statsmodels still uses the old import. A fix is on the way for the next statsmodels release.
Thanks #9769953.
pip3 uninstall statsmodels # make sure to remove old versions
pip3 install statsmodels==0.10.0rc2 --pre --user # install release candidate of statsmodels
Restarting the kernel of the jupyter notebook
fixed it for me.
You can check your versions with pip3 list
Summary: copy&run the following in your terminal:
pip3 uninstall statsmodels -y
pip3 install statsmodels==0.10.0rc2 --pre --user
and don't forget to restart the kernel of your jupyter notebook :)
pip install statsmodels --upgrade
did the trick for me
One easy fix I found is editing the .py file. I was getting the same error as the OP while using Dominance analysis. Edited the dominance.py file to have from scipy.special import factorial and it worked. I would think editing the from scipy.misc import factorial line to from scipy.special import factorial in the statsmodel package code in edgeworth.py would do the same job here.

Categories