How to resolve "cannot import name '_MissingValues' from 'sklearn.utils._param_validation'" issue when trying to import imblearn?

29.9k views Asked by At

I am trying to import imblearn into my python notebook after installing the required modules. However, I am getting the following error:

The error

Additional info: I am using a virtual environment in Visual Studio Code.

I've made sure that venv was selected as interpreter and as the notebook kernel. I've reloaded the window and restarted the kernel several times. I have also uninstalled and installed imbalanced-learn and scikit-learn several times, with and without "--upgrade". I'm still getting the same error.


Edit: Full traceback of error

{
    "name": "ImportError",
    "message": "cannot import name '_MissingValues' from 'sklearn.utils._param_validation' (c:\\Users\\wen\\OneDrive\\Desktop\\Colab_Notebooks\\.venv\\Lib\\site-packages\\sklearn\\utils\\_param_validation.py)",
    "stack": "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)\nCell \u001b[1;32mIn[1], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mimblearn\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[39m# Data Processing\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mpandas\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mpd\u001b[39;00m\n\nFile \u001b[1;32mc:\\Users\\wen\\OneDrive\\Desktop\\Colab_Notebooks\\.venv\\Lib\\site-packages\\imblearn\\__init__.py:52\u001b[0m\n\u001b[0;32m     48\u001b[0m     sys\u001b[39m.\u001b[39mstderr\u001b[39m.\u001b[39mwrite(\u001b[39m\"\u001b[39m\u001b[39mPartial import of imblearn during the build process.\u001b[39m\u001b[39m\\n\u001b[39;00m\u001b[39m\"\u001b[39m)\n\u001b[0;32m     49\u001b[0m     \u001b[39m# We are not importing the rest of scikit-learn during the build\u001b[39;00m\n\u001b[0;32m     50\u001b[0m     \u001b[39m# process, as it may not be compiled yet\u001b[39;00m\n\u001b[0;32m     51\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m---> 52\u001b[0m     \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m \u001b[39mimport\u001b[39;00m (\n\u001b[0;32m     53\u001b[0m         combine,\n\u001b[0;32m     54\u001b[0m         ensemble,\n\u001b[0;32m     55\u001b[0m         exceptions,\n\u001b[0;32m     56\u001b[0m         metrics,\n\u001b[0;32m     57\u001b[0m         over_sampling,\n\u001b[0;32m     58\u001b[0m         pipeline,\n\u001b[0;32m     59\u001b[0m         tensorflow,\n\u001b[0;32m     60\u001b[0m         under_sampling,\n\u001b[0;32m     61\u001b[0m         utils,\n\u001b[0;32m     62\u001b[0m     )\n\u001b[0;32m     63\u001b[0m     \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m_version\u001b[39;00m \u001b[39mimport\u001b[39;00m __version__\n\u001b[0;32m     64\u001b[0m     \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mbase\u001b[39;00m \u001b[39mimport\u001b[39;00m FunctionSampler\n\nFile \u001b[1;32mc:\\Users\\wen\\OneDrive\\Desktop\\Colab_Notebooks\\.venv\\Lib\\site-packages\\imblearn\\combine\\__init__.py:5\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[39m\"\"\"The :mod:`imblearn.combine` provides methods which combine\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[39mover-sampling and under-sampling.\u001b[39;00m\n\u001b[0;32m      3\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m----> 5\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m_smote_enn\u001b[39;00m \u001b[39mimport\u001b[39;00m SMOTEENN\n\u001b[0;32m      6\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m_smote_tomek\u001b[39;00m \u001b[39mimport\u001b[39;00m SMOTETomek\n\u001b[0;32m      8\u001b[0m __all__ \u001b[39m=\u001b[39m [\u001b[39m\"\u001b[39m\u001b[39mSMOTEENN\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mSMOTETomek\u001b[39m\u001b[39m\"\u001b[39m]\n\nFile \u001b[1;32mc:\\Users\\wen\\OneDrive\\Desktop\\Colab_Notebooks\\.venv\\Lib\\site-packages\\imblearn\\combine\\_smote_enn.py:12\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mbase\u001b[39;00m \u001b[39mimport\u001b[39;00m clone\n\u001b[0;32m     10\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m \u001b[39mimport\u001b[39;00m check_X_y\n\u001b[1;32m---> 12\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mbase\u001b[39;00m \u001b[39mimport\u001b[39;00m BaseSampler\n\u001b[0;32m     13\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mover_sampling\u001b[39;00m \u001b[39mimport\u001b[39;00m SMOTE\n\u001b[0;32m     14\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mover_sampling\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mbase\u001b[39;00m \u001b[39mimport\u001b[39;00m BaseOverSampler\n\nFile \u001b[1;32mc:\\Users\\wen\\OneDrive\\Desktop\\Colab_Notebooks\\.venv\\Lib\\site-packages\\imblearn\\base.py:21\u001b[0m\n\u001b[0;32m     18\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mmulticlass\u001b[39;00m \u001b[39mimport\u001b[39;00m check_classification_targets\n\u001b[0;32m     20\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m \u001b[39mimport\u001b[39;00m check_sampling_strategy, check_target_type\n\u001b[1;32m---> 21\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39m_param_validation\u001b[39;00m \u001b[39mimport\u001b[39;00m validate_parameter_constraints\n\u001b[0;32m     22\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39m_validation\u001b[39;00m \u001b[39mimport\u001b[39;00m ArraysTransformer\n\u001b[0;32m     25\u001b[0m \u001b[39mclass\u001b[39;00m \u001b[39mSamplerMixin\u001b[39;00m(BaseEstimator, metaclass\u001b[39m=\u001b[39mABCMeta):\n\nFile \u001b[1;32mc:\\Users\\wen\\OneDrive\\Desktop\\Colab_Notebooks\\.venv\\Lib\\site-packages\\imblearn\\utils\\_param_validation.py:908\u001b[0m\n\u001b[0;32m    906\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39m_param_validation\u001b[39;00m \u001b[39mimport\u001b[39;00m generate_valid_param  \u001b[39m# noqa\u001b[39;00m\n\u001b[0;32m    907\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39m_param_validation\u001b[39;00m \u001b[39mimport\u001b[39;00m validate_parameter_constraints  \u001b[39m# noqa\u001b[39;00m\n\u001b[1;32m--> 908\u001b[0m \u001b[39mfrom\u001b[39;00m \u001b[39msklearn\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mutils\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39m_param_validation\u001b[39;00m \u001b[39mimport\u001b[39;00m (\n\u001b[0;32m    909\u001b[0m     HasMethods,\n\u001b[0;32m    910\u001b[0m     Hidden,\n\u001b[0;32m    911\u001b[0m     Interval,\n\u001b[0;32m    912\u001b[0m     Options,\n\u001b[0;32m    913\u001b[0m     StrOptions,\n\u001b[0;32m    914\u001b[0m     _ArrayLikes,\n\u001b[0;32m    915\u001b[0m     _Booleans,\n\u001b[0;32m    916\u001b[0m     _Callables,\n\u001b[0;32m    917\u001b[0m     _CVObjects,\n\u001b[0;32m    918\u001b[0m     _InstancesOf,\n\u001b[0;32m    919\u001b[0m     _IterablesNotString,\n\u001b[0;32m    920\u001b[0m     _MissingValues,\n\u001b[0;32m    921\u001b[0m     _NoneConstraint,\n\u001b[0;32m    922\u001b[0m     _PandasNAConstraint,\n\u001b[0;32m    923\u001b[0m     _RandomStates,\n\u001b[0;32m    924\u001b[0m     _SparseMatrices,\n\u001b[0;32m    925\u001b[0m     _VerboseHelper,\n\u001b[0;32m    926\u001b[0m     make_constraint,\n\u001b[0;32m    927\u001b[0m     validate_params,\n\u001b[0;32m    928\u001b[0m )\n\n\u001b[1;31mImportError\u001b[0m: cannot import name '_MissingValues' from 'sklearn.utils._param_validation' (c:\\Users\\wen\\OneDrive\\Desktop\\Colab_Notebooks\\.venv\\Lib\\site-packages\\sklearn\\utils\\_param_validation.py)"
} 

The versions of the modules are as follows:

scikit-learn 1.3.0

imblearn 0.0

imbalanced-learn 0.10.1

9

There are 9 answers

1
Piessa On BEST ANSWER

I was having the same issue, downgrading to scikit-learn 1.2.2 fixed it for me

0
Prit Mayani On

I can reproduce the above issue. Nonetheless, after I restart My Kernal(In Pycharm Jupiter), I find the issue is resolved

2
RandomDev On

I updated my scikit-learn to 1.3.0 and it started raising this error. I think there is a bug with the latest version or the name might have been changed. Try downgrading the version of scikit-learn to 1.2.X, It will solve the problem.

0
asmgx On

I did this and fixed it

pip uninstall scikit-learn

then i used

pip install scikit-learn==1.2.2
0
Mayur Patil Prince On

I am also facing same error while importing below :

from imblearn.combine import SMOTETomek
from imblearn.under_sampling import NearMiss

I just downgrade the version of scikit-learn

Steps to resolve above issue :

  1. pip uninstall scikit-learn
  2. pip install scikit-learn==1.2.2

If does not work then restart kernel.

0
Eqab Al-Droubi On

"git clone https://github.com/scikit-learn-contrib/imbalanced-learn.git cd imbalanced-learn" Use this command to clone this directory from Github then go to imblearn directory on your local machine and replace it with the one you find in the package you have just downloaded

the problem is that in sklearn the name is MissingValues meanwhile in imblearn param_validation was _MissingValues which fixed in the new package of imblearn .

1
hussein mo On

You can fix this by upgrading imblearn to a recent version.

See https://github.com/scikit-learn-contrib/imbalanced-learn/issues/995 for background information on the issue

1
Divik Shrivastava On

For those who came across this issue on Jupyter notebook, here's how I solved it

  1. !pip uninstall scikit-learn --yes
  2. !pip uninstall imblearn --yes
  3. !pip install scikit-learn==1.2.2
  4. !pip install imblearn
  5. Restart anaconda navigator It should work
0
olasammy On

If you get this error via Jupyter Notebook installed alongside Anaconda navigation, you must install the imbalanced-learn library via the Conda package manager. Therefore, do the following:

pip uninstall imblearn --yes

Then

conda install -c conda-forge imbalanced-learn

This should solve the problem for you.